1 if exists(select * from sysdatabases where name='TestSchool') 2 drop database TestSchool 3 go 4 自動建立文件夾 5 exec sp_configure 'show advanced options',1 6 go 7 RECONFIGURE 8 go 9 exec sp_configure 'xp_cmdshell',1 10 go 11 RECONFIGURE 12 go 13 exec xp_cmdshell 'mkdir d:\qqaa\vv\cc' 14 15 create database TestSchool 16 on primary 17 ( 18 name='TestSchool_data',邏輯名稱 19 size=3MB, 初始大小 20 FileGrowth=10%,每次增加按總大小的10%增加 21 maxSize=1000mb,最大容量 22 FileName='d:\qqaa\vv\cc\TestSchool_data.mdf' 23 ), 24 ( 25 name='TestSchool_data1',邏輯名稱 26 size=3MB, 初始大小 27 FileGrowth=10%,每次增加按總大小的10%增加 28 maxSize=1000mb,最大容量 29 FileName='d:\project\TestSchool_data1.ndf' 30 ) 31 log on 32 ( 33 name='TestSchool_log',邏輯名稱 34 size=3MB, 初始大小 35 FileGrowth=10%,每次增加按總大小的10%增加 36 maxSize=1000mb,最大容量 37 FileName='d:\qqaa\vv\cc\TestSchool_log.ldf' 38 ), 39 ( 40 name='TestSchool_log1',邏輯名稱 41 size=3MB, 初始大小 42 FileGrowth=10%,每次增加按總大小的10%增加 43 maxSize=1000mb,最大容量 44 FileName='d:\qqaa\vv\cc\TestSchool_log1.ldf' 45 )
1 use TestSchool 2 go 3 if exists(select * from sysobjects where name='Teacher') 4 drop table Teacher 5 go 6 create table Teacher 7 ( 8 Id int primary key identity(1,1), 主鍵是非空惟一 9 Name nvarchar(50) not null, not null不爲空 10 Gender bit not null default(1) , 11 Age int not null check(age>0 and age<=100), 12 Salary money, 能夠爲null能夠寫null,或者不寫也默認是能夠爲null 13 Birthday datetime not null 14 )
1 if exists(select * from sysobjects where name='PK_id') 2 alter table teacher drop constraint PK_id 3 alter table teacher 4 add constraint PK_id primary key(id)
alter table teaher
add constraint UQ_name unique(name)
alter table teacher add constraint DF_Birthday default('1999-9-9') for birthday
alter table teacher with nocheck 不檢查現有數據
add constraint FK_teacher_subjectId foreign key(subjectid) references subject(id)
on delete no action
select LEN('abcdefg') select DataLength('中華人民共和國')
select LEN(Char) from chartest select DataLength(Char) from chartest
select LEN(VarChar) from chartest select DataLength(VarChar) from chartest
select LEN(NChar) from chartest select DataLength(NChar) from chartest
select LEN(NVarChar) from chartest select DataLength(NVarChar) from chartest
insert into Student values('123455667','張三','男',4,'110',N'廣州','1990-1-1','aa@bb.com')
insert into Student values('123455667','張三','男',4,null,'廣州','1990-1-1',default)
insert into Student(LoginPwd,StudentName,Gender,GradeId,Birthday) values('123455667','張三','男',4,'1990-1-1')
insert into Student(LoginPwd,StudentName,Gender,GradeId) values('123455667','張三','男',4)
insert into Student(LoginPwd,StudentName,Gender,GradeId,Birthday) values('123456','張三','男',5,'1999-9-9')
insert into Student(LoginPwd,StudentName,Gender,GradeId,Birthday) values('123456','張三',男,'2','1999-9-9')
insert into Student(LoginPwd,StudentName,Gender,GradeId,Birthday) values('123456','張三','男','2',1999-9-9)
update Student set Gender='男' where StudentNo=1
update Student set Phone=119 where StudentName='qq' and Gender='男'
update Student set LoginPwd='aaaaaa' ,Gender='男',GradeId=1,Address='東莞' where StudentNo=6
update Student set Birthday+=30
update Student set Phone='110' where Phone is null update Student set Address=DEFAULT where StudentNo=5 update Student set Phone='NULL' where StudentNo=3 update Student set Address='我在廣州' where Address=default
delete from Student where Gender='男' delete from Student
truncate table student
select * from Student where Sex='女' and StudentName like '林%' select * from Student where Sex='女' and StudentName like '林__'
select * from Student where StudentNo in (13,15,18,19) select * from Student where StudentNo like '[11-14]'
select StudentNo,StudentName,ISNULL(Email,'沒有填寫') from Student
select * from Student order by sex desc,StudentNo desc
select ClassId, COUNT(*) num from Student group by ClassId order by num desc select top 2 ClassId, COUNT(*) num from Student group by ClassId order by num desc
select ClassId, SUM(ClassHour) from Subject where ClassId is not null group by ClassId select StudentNo,AVG(StudentResult) from Result group by StudentNo select SubjectId,AVG(StudentResult) from Result group by SubjectId
查詢全部學員信息 select * from Student 指定查詢的列 select StudentNo,StudentName,Gender,Address from Student 指定查詢的列及查詢的條件 select StudentNo,StudentName,Gender,Address from Student where Gender='女' and Address='廣州' 設置虛擬結果集中的列名 select StudentNo as 學號,StudentName 姓名,性別=Gender,Address from Student where Gender='女' and Address='廣州' 添加常量列 select StudentNo as 學號,StudentName 姓名,性別=Gender,Address, 國籍='中國' from Student where Gender='女' and Address='廣州'
select top 10 percent StudentNo as 學號,StudentName 姓名,性別=sex,Address from Student order by StudentName
select distinct 性別=sex,Address from Student select distinct sex from Student
select COUNT(sex) from Student
select MAX(BornDate) from Student select min(BornDate) from Student
select MAX(sex) from Student nv select min(sex) from Studentnan
select MAX(StudentResult) from Result select MIN(StudentResult) from Result select sum(StudentResult) from Result where StudentNo=2 select avg(StudentResult) from Result where StudentNo=2
select sum(BornDate) from Student where StudentNo=2 select avg(BornDate) from Student where StudentNo=2
select sum(StudentName) from Student where StudentNo=2 select avg(StudentName) from Student where StudentNo=2