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导出: 点击要导出数据库----->右键(任务)----->生成脚本----->下一步----->下一步(高级)要编写脚本的数据类型---选择架构和数据 ...
随机推荐
- java 五十条数据分为一组
public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for(int ...
- React Navigation基本用法
/** * Created by apple on 2018/9/23. */ import React, { Component } from 'react'; import {AppRegistr ...
- Python 多线程的程序不结束多进程的程序不结束的区别
import time from threading import Thread from multiprocessing import Process #守护进程:主进程代码执行运行结束,守护进程随 ...
- type() 和 isinstance()区别
a=111 # type() 返回数据类型 In: type(a) Out: int In: print(type(a)) Out: <class 'int'> # isinstance ...
- nw 引用 sqlite
0.好吧,这对于我这个c 小白来说,真的有点难度. 1.安装Python 2.7.14 https://www.python.org/downloads/ 2.安装最新的nodejs+npm http ...
- Python学习第七课
Python学习第七课 'Alex' "Alex"print('hello'*5) #重复输出字符串 print('hellowold'[2:]) #类似于切片操作:会取出 llo ...
- React native中使用XMLHttpRequest请求数据
一.代码 import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View } from ' ...
- 配置DTP
拓扑一 结果:NO 默认auto(被动)模式 Switch>show interfaces fastEthernet / switchPort Name: Fa0/ Switchport: E ...
- python学习1---列表、矩阵、数组
1.列表与数组区别 numpy数组的所有元素类型是相同的,而列表的元素类型是任意的. 2.numpy数组与矩阵区别 矩阵必须是二维的,数组可以是多维的,matrix是array的一个分支. matri ...
- DoTween之队列
//引用命名空间 using DG.Tweening; // 初始化一个sequence Sequence sequence = DOTween.Sequence(); //添加动画 sequence ...