reate database SCRuse scrgo--create table s(sid int identity(1000,5) primary key,sname varchar(50) null,sage int null,ssex char(2) check(ssex='男' or ssex='女'))create table c(cid int identity (10000,100) ,cname varchar(50) null,cteacher varchar(50) null,primary key (cid))create table sc(sid int ,cid int ,cgrade int null check(between 0 and 100),foreign key (sid) references s(sid),foreign key (cid) references c(cid))insert into C(cname,cteacher)values('C#','Teacher Liu')insert into C(cname,cteacher)values('ASP','Teacher ')select top 1 * from Cselect * from Cupdate Cset cname = 'SQL'where cid = 10000delete top(1) Cdrop table Cdrop database SCRtruncate table Cinsert into s(sname ,sage ,ssex)values('宋斌',23,'男')select * from Sselect * from Cselect * from scdelete top(1) SUPDATE CSET CTEACHER = 'TEACHER5'WHERE CID = 10800 UPDATE CSET CTEACHER = 'TEACHER6'WHERE CID = 10900 UPDATE CSET CTEACHER = 'TEACHER8'WHERE CID = 11100 -- 检索出课程号为10700的学生的姓名和学号-- 1 )join select s.sname,s.sidfrom s join scon s.sid = sc.sidwhere sc.cid = 10700-- 联接查询 相当于一个 FULL JOINselect s.sname,s.sidfrom s ,scwhere s.sid = sc.sidand sc.cid = 10700-- 2)select 嵌套查询或子查询select sname,sidfrom swhere sid =(select sidfrom scwhere cid = 10700)select sname,sidfrom swhere sid in --in 结构为:(集合1) in (集合2) 表示集合1中的每个元素均在集合2中(select sidfrom scwhere cid = 10700)select sname,sidfrom swhere 10700 in(select cidfrom scwhere sid = s.sid)select sname,sidfrom swhere 10700 = all(select cidfrom scwhere sid = s.sid)select sname,sidfrom swhere exists(select sidfrom scwhere cid = 10700and sid = s.sid)select sname,sidfrom swhere not exists(select sidfrom scwhere cid = 10700and sid = s.sid)SELECT SNAME,SIDFROM SWHERE SID <> (SELECT SIDFROM SCWHERE CID = 10700)-- 检索选修课程名为 SQL 的学生的学号和姓名select sid,snamefrom swhere sid in ( select sid from sc where cid in ( select cid from c where cname = 'SQL' ) )select s.sid ,s.snamefrom s,c,scwhere s.sid = sc.sid and c.cid = sc.cidand c.cname = 'sql'select *from s,c,sc-- 检索任课教师为 teacher liu 的学生的学号和姓名-- 检索不学 C# 课程的学生的姓名和年龄select sname,sagefrom swhere sid not in( select sid from sc where cid not in ( select cid from c where cname = 'C#' ))-- 检索学习全部课程的学生姓名和性别select sname ,ssexfrom swhere not exists( select * from c where not exists ( select * from sc where sc.sid = s.sid and sc.cid = c.cid ))CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ] [ WITH <view_attribute> [ ,...n ] ] AS select_statement [ ; ][ WITH CHECK OPTION ] <view_attribute> ::= { [ ENCRYPTION ] [ SCHEMABINDING ] [ VIEW_METADATA ] }create view 视图名 (列名) asselect 查询语句 select s.sid ,s.snamefrom s,c,scwhere s.sid = sc.sid and c.cid = sc.cidand c.cname = 'sql'create view scrview(sid,cid,sname,cname,cgrade)as select s.sid,c.cid,s.sname,c.cname,sc.cgrade from s,sc,cwhere s.sid = sc.sidand c.cid = sc.cidselect sid,sname from scrview where cname = 'sql'drop view scrviewcreate view scrview(sid,sname)as select sid,snamefrom swhere sid in ( select sid from sc where cid in ( select cid from c where cname = 'SQL' ) )select * from scrview--== 索引的语法CREATE [ UNIQUE ] INDEX index_name ON <object> ( column_name [ ASC | DESC ] [ ,...n ] ) create [unique] index 索引名 on 基本表名 (列名)--create index sidindexon s(sid)create unique index snameon s(sname)select * from sinsert s(sname)values('lize')drop index 表名.索引名drop index s.sidindexALTER TABLE table_name{ [ ALTER COLUMN column_name {DROP DEFAULT | SET DEFAULT constant_expression | IDENTITY [ ( seed , increment ) ] } | ADD { < column_definition > | < table_constraint > } [ ,...n ] | DROP { [ CONSTRAINT ] constraint_name | COLUMN column }] }< column_definition > ::= { column_name data_type } [ [ DEFAULT constant_expression ] | IDENTITY [ ( seed , increment ) ] ] [ROWGUIDCOL] [ < column_constraint > ] [ ...n ] ]< column_constraint > ::= [ NULL | NOT NULL ] [ CONSTRAINT constraint_name ] { | { PRIMARY KEY | UNIQUE } | REFERENCES ref_table [ (ref_column) ] [ ON DELETE { CASCADE | NO ACTION } ] [ ON UPDATE { CASCADE | NO ACTION } ] }< table_constraint > ::= [ CONSTRAINT constraint_name ] { [ { PRIMARY KEY | UNIQUE } { ( column [ ,...n ] ) } | FOREIGN KEY ( column [ ,...n ] ) REFERENCES ref_table [ (ref_column [ ,...n ] ) ] [ ON DELETE { CASCADE | NO ACTION } ] [ ON UPDATE { CASCADE | NO ACTION } ] }-- 给一个表增加一个新列alter table 基本表名 add 列名 类型 [约束] -- 给一个表删除列alter table 基本表名 drop 列名 [cascade/restrict]--alter table sadd saddr nvarchar(100)select * from salter table sdrop column saddrcascade-- ALTER TABLE doc_exy ALTER COLUMN column_a DECIMAL (5, 2) ;alter table calter column cname nvarchar(100)