命名习惯 数据库名.MyDB 表名.StuInfo
数据类型
int 整形
datetime 日期 getdate()
float 浮点
bit 布尔值
nvarchar(50) 变长 文本型
nvarchar(MAX) 备注
创建数据库
create database Library
创建表
create table Users
(
UID int primary key,
userName nvarchar(20) not null,
userPwd nvarchar(20) not null
)
查询语句
select * from StuInfo
select Sex,Age from StuInfo
select * from StuInfo where Sex='男' and (Age = 21 or Age=22)
select * from StuInfo where Sex='男' and Age in(21,22)
select * from StuInfo where Age>=10 and Age<=30
select * from StuInfo where Age between 20 and 25
select top 3 StuId,Sex from StuInfo where Sex='男' order by Age desc //desc倒序
select * from StuInfo where StuId like '%a%'
select COUNT(*) as UserCount from StuInfo where Sex='男' //count()统计
select MIN(Age) as MinAge from StuInfo
select MAX(Age) as MaxAge from StuInfo
select Avg(Age) as AvgAge from StuInfo //avg平均值
select * from StuInfo where Age>
(
select Avg(Age) as AvgAge from StuInfo
)
select COUNT(*) as AgeCount,Age from StuInfo Group by Age //分组
having COUNT(*)=1 order by Age
添加语句
insert into StuInfo values('11','吧','女',20,'2005-7-30','','true','eqwe')
insert into StuInfo(StuId,Age) values('qq',11)
删除语句
delete StuInfo where StuId='qq'
更新语句
update StuInfo set Sex='女' where ParenetId=1
联合查询
Left join right join
select * from
(select ParenetId,ROW_NUMBER() over(order by ParenetId) as row from StuInfo) StuInfo
where row between 1 and 20
select MONTH(CredtedTime) as Credtedmoth from StuInfo
select DATEADD(mm,1,getdate())
select datediff(yy,getdate(),'2100')
select StuId,ISNULL(FatherName,'')as FatherName from StuInfo
select StuId,case
when(StuId='aa') then 'UI哦'
when(StuId='11') then 'U存在虚'
end as A
from StuInfo
create proc procCategorySelect
(
@name varchar(200),
@age int
)
as
select * from StuInfo where StuName=@name and age=@age
exec procCategorySelect 'xudads',11
create trigger trigCategoryDelete
ON StuInfo
after delete /* 有三种INSERT,DELETE,UPDATE*/
AS
begin
select * from deleted /*在删除后同时查出删除后的内容*/
end
delete StuInfo where StuId=4
Backup DATABASE MyDB
TO DISK = 'e:\MyDB.bak'