Sqlserver常用基础语句
1. char/nchar,varchar/nvarchar
char(10) 只能放五个中文,定长,如果只放一个字节,后面就是九个空格(一个中文两个字节)
nchar(10) 放十个中文,定长
varchar(10)放五个中文,变长,如果只放一个字节,就只放一个字节
nvarchar(10)放十个中文,变长。。。。。。。
2.创建一个数据库,在数据库里面创建表,以及添加表里面的项
create database Library
go
use Library
go
create table Users
(
UID int primary key,
userName nvarchar(20) not null,
userPwd nvarchar(20) not null
)
creat table Books
(
BID int primary key,
bookName nvarchar(20) not null,
price int not null,
authors nvarchar(15),
)
3.select语句用法
select*from Products--查询出Products表里面的所有信息
select ProductID,ProductName from Products--查询出Products表里面所有的ProductID,ProductName
select ProductID,ProductName from Products where ProductID=1--查询出Products表里面ProductID=1的所有ProductID和ProductName
use pubs
go
select address from authors where au_id='172-32-1176'--先导入指定的数据库pubs,查询出authors表里面,au_id为172-32-1176的address
select productID,productName from products order by productID asc--默认是asc(可以不写)从小到大排序,desc是从大到小
select *from products where UnitPrice>20--查询出products表里面UnitPrice>20的所有信息
select* from Emloyees where LastName='King' and City='London'
use pubs
go
select* from employee where fname='Paul' and job_id=5
select*from Products where ProductID in(4,5,6)--查询出Products表中ProductID为4,5,6的所有信息
select*from Products where UnitPrice<10 and UnitPrice<30 order by UnitPrice--查询出Products表中UnitPrice<10并且UnitPrice<30的所有信息,并按照UnitPrice的大小由小到大排序
select*from Products where UnitPrice between 10 and 30 order by UnitPrice--上面的另外一种写法
select * from Employees where FirstName like 'A%'--查询出Employees中FirstName里面第一个字母是A的所有人信息
select*from Employees where FirstName like '%A%'--查询出Employees中FirstName里面中间有A的所有人信息
select*from Employees where FirstName like '%A'--查询出Employees中FirstName里面最后一个字母是A的所有人信息
select count(*) from Employees--查询出Employees表中的所有记录数
select min(Unitprice)from Products--查询出Products表中Unitprice的最小值
select max(Unitprice)from Products--查询出Products表中Unitprice的最大值
select avg(Unitprice)from Products----查询出Products表中Unitprice的平均值
select sum(Unitprice)from Products----查询出Products表中Unitprice的总和
select top 5* from Products--查询出前五条的记录信息
select * from Products where Unitprice> --有子查询,查找出比平均值高的商品信息
(
select avg(Unitprice) from Products
)
4.insert /update
insert into BOOK(bookName,bookPrice,bookAuthors)
values('C#基础','35','ji')
insert into BOOK(bookName,bookPrice,bookAuthors)
values('VB基础','12','ni')
insert into BOOK(bookName,bookPrice,bookAuthors)
values('C基础','35','das')
update BOOK set bookPrice=30 where bookName='C#基础'--修改BOOK表,让bookName为C#基础的项中的bookPrice=30
delete BOOK where bookName='C#基础'
5.有一个student表(学号,姓名,系名,课程名,成绩),查询至少修了四门课程的学生学号,姓名以及平均成绩的SQL语句。
select stu,sname,avg(sorce)
from students
group by stu,sname
having count(*)>=4
6.
select distinct [name] from Category //查出Category 中不重复的name
select count(distinct name) from Category //查出Category 中不重复的name的数量
7.连接
inner join on, left join on, right join on区别:
表A记录如下:
aID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115
表B记录如下:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408
实验如下:
1.left join
sql语句如下:
select * from A
left join B
on A.aID = B.bID
结果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
5 a20050115 NULL NULL
(所影响的行数为 5 行)
结果说明:
left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.
换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID).
B表记录不足的地方均为NULL.
2.right join
sql语句如下:
select * from A
right join B
on A.aID = B.bID
结果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
NULL NULL 8 2006032408
(所影响的行数为 5 行)
结果说明:
仔细观察一下,就会发现,和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.
3.inner join
sql语句如下:
select * from A
innerjoin B
on A.aID = B.bID
结果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
结果说明:
很明显,这里只显示出了 A.aID = B.bID的记录.这说明inner join并不以谁为基础,它只显示符合条件的记录.
8. 用这种查询语句就可以查出指定区间记录的数据了
select * from
(select id, row_number() over(order by id desc) as row from [entry])Entry
where row between 1 and 5000
9.给表添加一字段
alter table Category
add
age int null
删除表:
drop table Users
10.约束
1.UNIQUE约束,与PK的区别:
一张表只能有一个PK,但可以有多个UNIQUE约束
PK约束定义主键不能为空,但UNIQUE可以为空值
方法:点"管理索引和键"---"添加"---"类型"选择"唯一键"---"列"添加进多个UNIQUE--关闭保存
它的作用是,当那多个unique同时都相同的时候,就会报错,实现了实体的完整性。
也可以用SQL语言来实现:
声明UNIQE约束
//单列处理
creat table T2
(
ID int primary key identity(1,1),
Ename nvarchar(20) unique
)
//组合列处理
creat table T3
(
ID int primary key identity(1,1)
Ename nvachar(20),
Ebir datetime
unique(Ename,Ebir)
)
2.check约束,它的作用是,约束一个键值,实现域完整性
方法:
"管理check约束"---"添加"---"常规"表达式---写入你要约束的键值的表达式比如:laborage>=2000 and laborage<=5000
---"关闭"保存
这样,在输入laborage的值的时候,如果不是大于等于2000小于等于5000就会报错
也可以用SQL语言实现
creat table T1
(
工资 money not null check(工资 between 2000 and 4000)
)
11.选择出年月日
select year(getdate()) //获取当前的年
select month(getdate()) //获取当前的年
select day(getdate()) //获取当前的年
select * from employ where month(birthday)=8 // 打印出8月份过生日的员工的所有信息
select * from employ where year(getdate())-year(birthday)>25// year(getdate())为当前年份,打印出年龄大于25岁的员工的所有信息
select * from employ where year(birthday)=2008 and month(birthday)=8 and day(birthday)=12 //打印出生日为2008-8-12日的所有员工信息
select dateadd(yy,100,getdate())//当天加上100年的时间,getdate()也可以换成具体的某一天比如写成:'2108/12/31'
select dateadd(mm,1,getdate())//当天加上1个月的时间
select dateadd(dd,100,getdate())//当天加上100天的时间
select datediff(yy,getdate(),'2108/12/31')//当天距离2108/12/31还有多少年
select datediff(mm,getdate(),'2108/12/31')
select datediff(dd,getdate(),'2108/12/31')
Month
mm、m
Dayofyear
dy、y
Day
dd、d
Week
wk、ww
Weekday
dw、w
Hour
Hh
Minute
mi、n
Second
ss、s
Millisecond
Ms
12.isnull
select title,content,isnull(categoryID,0) from news //为null的categoryID用0显示出来
13.case用法
//查找categoryID=13的state,并对state进行判断
select state,case
when(state=1)then '待审'
when(state=2)then '已审'
end as pro_state
from category where categoryID=13
//查找出低级的多少个,中级的多少个,高级的多少个
select Count(*) as [Count] from category
group by
case
when categoryID<15 then '低级'
when categoryID between 15 and 20 then '中级'
else '高级'
end
//查出category 中的CategoryID,name以及判断每个categoryID 的大小
select CategoryID,name,
case
when categoryID<15 then '低级'
when categoryID between 15 and 20 then '中级'
else '高级'
end as categoryRange from category
13.
Select c.*, IsNull(cr.Count,0) as [Count]
From Category c
Left Join
(
select cr.CategoryID,Count(*) as [Count] from CategoryRelative cr
right Join Entry e ON cr.ChildID = e.ID and e.state <> 2 and e.IsActive = 1
where cr.blogid = 8785
Group by cr.CategoryID
) cr ON c.CategoryID = cr.CategoryID
Where BlogID = 8785 and CategoryType = 1
14.exists
//查出Category 表中categoryID不等于n.categoryID的c.categoryID,c.[name]
select c.categoryID,c.[name] from Category c left join News n on c.categoryID=n.categoryID where n.categoryID is null
select categoryID,[name] from category where not exists
(
select * from News where category.categoryID=News.categoryID
)
*当News 表中categoryID无大量重复时第一种性能无疑是最好的,反之,第二种性能好。
14.cast和convert
convert包含了cast的所有功能,而且 convert还能进行日期转换,但是cast是ansi兼容的, convert则不是
select 'ID'+ cast(newsID as varchar) from News
ID5
ID8
ID9
ID10
ID11
ID13
ID14
Sqlserver常用基础语句的更多相关文章
- mysql常用基础语句学习
常用sql语句 查询: SELECT 列名(或者*,表示所有列) FROM 表名 WHERE 筛选条件; FROM 表名:顾名思义,就是从表名指定的这张表格中: WHERE 筛选条件:意思是" ...
- SQL 常用基础语句
1.SQL SELECT 语句 语法:SELECT 列名称 FROM 表名称 2.SQL SELECT DISTINCT 语句 语法:SELECT DISTINCT 列名 ...
- Oracle常用基础语句(杂)
打开服务 WIN + R services.msc 登录 --方法1 --WIN + R --CMD sqlplus / as sysdba --方法2,常用 --WIN + R --CMD --&q ...
- sqlserver 常用sql语句
SELECT COUNT(*) FROM WeixinUser SELECT COUNT(*) FROM WeixinUser WHERE datediff(day, CreateTime,getda ...
- 转: sqlserver常用sql语句,更改字段,建立唯一键,多个字段去重复等
[sql] view plain copy print?在CODE上查看代码片派生到我的代码片 --修改字段类型: --alter table 表名 alter column 待修改字段名 待修改字段 ...
- sqlserver常用sql语句,更改字段,建立唯一键,多个字段去重复等
--修改字段类型: --alter table 表名 alter column 待修改字段名 待修改字段类型 alter table users alter column userid varchar ...
- sqlserver常用简单语句
1.增 插入内容 insert into <表名> (列1,列2,列3) values ('值1','值2','值3') 检索出的内容插入到另外一张表 insert into <表名 ...
- SqlServer常用语句
首先,写这个的原因是我其实sql语句不太行,总觉得自己写得很乱,好像也没有系统学习过,借此复习和与大家探讨 No.1 关于查询时间区间是否重叠的sql语句 问题是这样:插入之前,想查询同User是否其 ...
- 第二节:SQLServer导出-重置sa密码-常用sql语句
1.SQLServer导出: 点击要导出数据库----->右键(任务)----->生成脚本----->下一步----->下一步(高级)要编写脚本的数据类型---选择架构和数据 ...
随机推荐
- 选择排序<C#>
目标:对数组(列表等任意有序容器)进行排序 方法:对列表进行遍历,选出最大的 之后将这个数储存起来,对剩下的数再选择最大的,之后再对剩下数做同样的操作 直至结束即可. 代码如下: public ...
- C++的qsort函数
void qsort(void * base,int nelem,int width,int (*fcmp)(const void*,const void *) 1.待排序数组首地址 2.数组中待排序 ...
- DAY2:数据类型Python3.6
数字 1.int(整型) 2. long(长整型) 3.float(浮点型) 4.complex(复数) 布尔值 1.真或假 1或0表示 字符串 知识补充 字符串转二进制 mes = "北 ...
- c语言——单链表分拆——头插法创建链表,尾插法生成链表
#if 1 #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; ...
- linux定时清理日志
服务器硬盘较小,需要自动删除日志 1.编写find命令 首先编写需要删除文件的sh #删除50天前的日志 find */logs -mtime +50 -exec rm -f {} \; #注意目录 ...
- Request.ServerVariables参数说明
Request.ServerVariables["SERVER_NAME"] '获取服务器IP Request.ServerVariables["HTTP_REFERER ...
- while循环 格式化输出 运算符 编码
一.while循环 1.基本结构 while 条件: 循环体 流程: 判断条件是否为真. 如果真, 执行代码块. 然后再次判断条件是否为真 .如果真继续执行代码块.... ...
- Proxy代理模式(结构型)
一:描述: 为其他对象提供一种代理,来控制对这个对象的访问.如当操作某个对象很复杂时,我们可以建个代理来操作复杂对象,并开放一些简单的接口/方法来让我们比较简单的间接操作,也可在代理层实现一些自己的业 ...
- easyui-tree-url-param
远古写法 url后面加参数?param1=1¶m2=2 动态添加 onBeforeLoad: function (node, param) { param.param1= 1, par ...
- Android KitKat Immersive Mode使用
写了一个方法,在onCreate和onResume中调用即可,4.4以上可用. private void openImmersiveMode() { if (android.os.Build.VERS ...