T-SQL 查询、修改数据表

T-SQL修改表数据

 

INSERT语句

语法:

INSERT
[TOP(expression) [PERCENT]]
[INTO]
  { <object> | rowset_function_limited
    [ WITH ( <Table_Hint_Limited> [ ...n ] ) ] /*指定表提示*/
    | view_name } /*视图名*/
  {
    [(column_list) ] /*指定列名*/
    [<OUTPUT Clause> ]
    {VALUES /*指定列名的取值*/
      {DEFAULT | NULL | expression}[1…n]) /*列值的构成形式*/  
        |derived_table|execute_statement } } /*结果集*/
    |DEFAULT VALUES /*所有列均取默认值*/

 

例子:

 1 /*插入单个元组*/
 2 /*向student表中插入一个学生记录(‘200’,‘曾雷’,‘女’,‘1978-2-3’,‘05001’)*/
 3 USE test            
 4 INSERT INTO student_1 
 5 VALUES(100,'曾雷','','1995-2-3',20)
 6 
 7 /*查询student表,查看结果*/
 8 select * from student_1
 9 -----------------------------------------------------------------------------------
10 
11 
12 /*向student表中插入一个学生记录(‘201’,‘孙浩’,‘男’,‘1977-8-4’,NULL)*/
13 INSERT INTO student_1(sno,sname,ssex,sbirthday,sage) 
14 VALUES(200,'孙浩','','1996-8-4',null)
15 
16 select * from student_1
17 delete from student_1 where sno=200
18 
19 INSERT INTO student_1(sno,sname,ssex,sbirthday) 
20 VALUES(200,'孙浩','','1996-8-4')
21 -----------------------------------------------------------------------------------
22 
23 
24 /*插入元组集合*/
25 /*将student_1表中的相关数据插入到student表中*/
26 select * from student
27 select * from student_1
28 
29 insert into student(sno,sname,ssex,sage)
30 select sno,sname,ssex,sage 
31 from student_1 
32 
33 
34 /*向student表中添加两个新生*/
35 INSERT INTO student_1(sno,sname,ssex,sbirthday,sage) 
36 VALUES(300,'王明','','1996-8-4',19),(400,'赵强','','1996-4-1',19)
37 -----------------------------------------------------------------------------------
38 
39 
40 /*向自增列添加数据*/
41 create table testidentity(
42 id int identity,
43 words varchar(10))
44 
45 insert into testidentity values('a')  --标识列不指定
46 insert into testidentity values('b')  --指定除了标识列外的列
47 
48 set IDENTITY_INSERT testidentity on   
49 insert into testidentity(id,words) values(10,'c')  --指定标识列
50 
51 set IDENTITY_INSERT testidentity off  
52 insert into testidentity values('d')
53 
54 select * from testidentity

 

 

UPDATE语句

语法:

UPDATE{table_name|view_name}
SET column_name = {expression | DEFAULT | NULL}[1…n]
where where_clause

例子:

 1 /*将sc表中的成绩小于60的加5。*/
 2 UPDATE sc
 3 SET grade=grade+5
 4 WHERE grade<70
 5 
 6 /*将张三选修1号课程的成绩置零。*/
 7 UPDATE sc
 8 SET grade=0
 9 WHERE cno=1 and sno in
10   (select sno from student where sname='张三')
11   
12   
13 /*将学号为1的学生的姓名改为张三十,年龄改小2岁。*/
14 UPDATE student
15 SET sname='张三十',sage=sage-2   --同时更新多列
16 WHERE sno=1
17 
18 select * from student
19 -----------------------------------------------------------------------------------
20 
21 /*使用top表达式*/
22 UPDATE top(2) student
23 SET sage=sage-2  
24 
25 UPDATE top(50) percent student
26 SET sage=sage-2  

 

DELETE语句

语法:

DELETE table_name
WHERE search_condition

例子:

 1 /*删除student表中学号为200的记录*/
 2 select * from student
 3 select * from sc
 4 
 5 DELETE student  
 6 WHERE sno='200'
 7 
 8 /*删除张三的选修1号课程的选课记录*/
 9 DELETE sc  
10 WHERE cno=1 and sno in 
11     (select sno from student where sname='张三')
12 -----------------------------------------------------------------------------------
13 
14 /*TRANCATE*/
15 /*TRUNCATE TABLE  table_name*/
16 /*一次删除表中所有数据,即清空表,
17   但表的结构及约束保持不变,且该操作不记录日志,无法恢复,使用时必须慬慎。*/
18   
19   
20 /*删除student_1表中的记录*/
21 TRUNCATE TABLE  student_1
22 
23 select * from student_1

 

 

T-SQL查询数据

 

SELECT 语句语法:

SELECT select_list
[INTO new_table]
FROM table_source
[WHERE search_condition]
[GROUP BY group_by_expression]
[HAVING search_condition]
[ORDER BY order_expression [ASC | DESC]]

 

简单查询例子:

  1 /*查询列*/
  2 
  3 /*查询student表中所有记录的sname、ssex和sage列*/
  4 SELECT sname,ssex,sage
  5 FROM student
  6 
  7 /*查询有选课记录的课程cno*/
  8 select distinct cno   --避免重复项
  9 from sc
 10 
 11 /*查询有85分以上成绩的课程cno*/
 12 SELECT DISTINCT cno
 13 FROM  sc 
 14 WHERE grade>85
 15 
 16 
 17 /*查询student表的所有记录*/
 18 SELECT *
 19 FROM student
 20 
 21 SELECT sno as '学号',sname as '姓名',ssex as '性别'    
 22 FROM student
 23 
 24 
 25 /*返回部分结果top*/
 26 select top(1) * from student
 27 select top(1) * from student where ssex=''
 28 select top(1) with ties *  from student order by sage
 29 select top(50) percent * from student
 30 
 31 
 32 /*计算列*/
 33 select sno,sname,2015-sage as '出生年月' from student
 34 select sno,cno,grade*1.1 from sc
 35 -----------------------------------------------------
 36 
 37 
 38 /*选择查询
 39 
 40 查询sc表中成绩大于60的所有记录*/
 41 SELECT *
 42 FROM sc
 43 WHERE grade>60
 44 
 45 
 46 /*查询sc表中1号课程成绩大于60的所有记录*/
 47 SELECT *
 48 FROM sc
 49 WHERE cno=2 and grade>60
 50 
 51 
 52 /*查询score表中成绩在60~80之间的所有记录*/
 53 SELECT *
 54 FROM sc
 55 WHERE grade between 60 and 80
 56 
 57 
 58 /*查询sc表中成绩为85、86或88的记录*/
 59 SELECT *
 60 FROM sc
 61 WHERE grade in(85,86,88)
 62 
 63 
 64 /*字符串匹配*/
 65 
 66 /* % 匹配任意字符
 67    _ 匹配单个字符
 68   [] 匹配括号中的任意一个字符
 69   [^]或[!]匹配没有出现在括号中的单个字符
 70    escape换码字符  */
 71 
 72 select * from student
 73 
 74 /*查询student表中姓张的或性别为“女”的学生记录*/
 75 SELECT *
 76 FROM student
 77 WHERE sname like '张%'or ssex=''
 78 
 79 /*查询student表中姓李的学生*/
 80 SELECT *
 81 FROM student
 82 WHERE sname like '李%'
 83 
 84 SELECT *
 85 FROM student
 86 WHERE sname like '[李]%'
 87 
 88 SELECT *
 89 FROM student
 90 WHERE sname like '李_'
 91 
 92 SELECT *
 93 FROM student
 94 WHERE sname like '[^李]%'
 95 
 96 SELECT *
 97 FROM student
 98 WHERE sname not like '[李]%'
 99 
100 SELECT *
101 FROM student
102 WHERE sname like '%[四]%'
103 
104 
105 /*查询sc表中没成绩的记录*/
106 SELECT *
107 FROM sc
108 WHERE grade is null
109 
110 SELECT *
111 FROM sc
112 WHERE grade is not null
113 
114 
115 /*查询结果排序*/
116 SELECT *
117 FROM sc
118 order by grade 
119 
120 SELECT *
121 FROM sc
122 order by cno,grade desc
123 
124 
125 /*分组查询*/
126 /*group by group_by_expression[with rollup|cube]*
127   having search_condition
128   with rollup 只返回第一个分组条件制定的列的统计行;
129   而with cube除返回group by制定的列外,还返回按组统计的行*/
130 
131 SELECT cno,AVG(grade)
132 FROM sc
133 group by cno
134 
135 SELECT cno,AVG(grade)
136 FROM sc
137 group by cno
138 having AVG(grade)>60
139 
140 SELECT cno,tno,AVG(grade)
141 FROM sc
142 group by cno,tno
143 
144 SELECT cno,tno,AVG(grade)
145 FROM sc
146 group by cno,tno with rollup
147 
148 select AVG(grade)
149 from sc

 

高级查询例子:

  1 /*嵌套查询*/
  2 use test
  3 
  4 
  5 /*使用IN或NOT IN*/
  6 select sname
  7 from student
  8 where sno in
  9     (select sno 
 10      from sc
 11      where cno=2)
 12 
 13 select sname
 14 from student
 15 where sno not in
 16     (select sno 
 17      from sc
 18      where cno=2)
 19      
 20      
 21 
 22 /*比较运算符的子查询*/
 23 
 24 select sno,grade
 25 from sc sc1
 26 where sc1.cno=1 and 
 27     sc1.grade=(select sc2.grade 
 28                 from sc sc2 
 29                 where sc2.cno=1 and sc2.sno=1)
 30                 
 31 select * from sc
 32 
 33 select sno,grade
 34 from sc sc1
 35 where sc1.cno=1 and 
 36     sc1.grade>(select sc2.grade 
 37                 from sc sc2 
 38                 where sc2.cno=1 and sc2.sno=1)
 39                 
 40 select sno
 41 from sc 
 42 where cno=1 and 
 43       grade>all(select grade from sc where sno=1)
 44       
 45 select sno
 46 from sc 
 47 where cno=1 and 
 48       grade>(select max(grade) from sc where sno=1)
 49             
 50 
 51 select student.sno,sname,cno,grade
 52 from sc as a,student
 53 where student.sno=a.sno and  --不相关子查询
 54     grade>(select avg(grade)
 55            from sc b
 56            where b.cno=a.cno)  
 57 
 58 
 59 /*exists*/
 60 SELECT sname
 61 FROM student 
 62 WHERE EXISTS
 63  (SELECT *
 64   FROM sc
 65   WHERE student.sno=sc.sno and sc.cno=2)
 66   
 67 SELECT sname
 68 FROM student 
 69 WHERE not EXISTS
 70  (SELECT *
 71   FROM sc
 72   WHERE student.sno=sc.sno and sc.cno=2)
 73 
 74 
 75 /*多层嵌套查询
 76 查询最高分的学生姓名*/
 77 
 78 select *
 79 from student
 80 where not exists
 81     (select * 
 82      from course
 83      where not exists
 84         (select * 
 85          from sc
 86          where sc.sno=student.sno and sc.cno=course.cno))
 87 
 88 
 89 select sname
 90 from student
 91 where sno in
 92     (select sno
 93      from sc
 94      where  grade=
 95         (select max(grade)
 96         from sc))
 97 go
 98 
 99 select * from sc
100 select * from student
101 
102 
103 /*DELETE、UPDATE和INSERT语句中的子查询*/
104 ------------------------------------------------------------
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 /*联接查询*/
115 use test
116 
117 /*查询学生的姓名,选课号及成绩*/
118 
119 select sname,cno,grade
120 from student,sc 
121 where student.sno=sc.sno
122 
123 select sname,cname,grade 
124 from student,sc,course  --多表连接
125 where student.sno=sc.sno and sc.cno=course.cno
126 
127 select sname,cno,grade
128 from student inner join sc on(student.sno=sc.sno) --内连接
129 
130 
131 select student.sname,sc.cno,sc.grade
132 from student left join sc on (student.sno=sc.sno)  --左向外连接
133 
134 
135 select student.sname,sc.cno,sc.grade
136 from student right join sc on (student.sno=sc.sno)   --右向外连接
137 
138 
139 select student.sname,sc.cno,sc.grade
140 from student full outer join sc on (student.sno=sc.sno)  --完全外部连接
141 
142 
143 select student.sno,sc.sno,sc.cno,sc.grade  
144 from student cross join sc                  --交叉连接
145 
146 select student.sno,sc.sno,sc.cno,sc.grade  
147 from student cross join sc                  --带限定条件的交叉连接
148 where student.sno<2
149 
150 
151 select c1.cno,c2.cname
152 from course c1,course c2    --自连接
153 where c1.cpno=c2.cno
154 --------------------------------------------------------------------------
155 
156 
157 
158 /*无法使用ntext、text或image列上直接连接表,
159 但是使用substring函数在ntext、text或image列上间接联接表,如*/
160 
161 select *
162 from student join sc
163 on substring(student.mytext,1,2)=substring(sc.mytext,1,2)
164 ------------------------------
165 
166 
167 
168 
169 /*使用UNION运算符组合多个结果
170 查询所有作者和客户的号码和名称*/
171 
172 select sno,sname from student where sno=1
173 union
174 select sno,sname from student where sno=2
175 go
176 ---------------------------
177 
178 
179  /*在查询的基础上创建新表
180 将查询得到的学生学号、姓名、课程和分数输入到新建的表score1中,
181 再显示该新表的记录*/
182 
183 select student.sno,avg(grade) as 平均成绩
184 into avggrade    --该表自动生成
185 from student inner join sc on (student.sno=sc.sno)
186 group by student.sno
187 
188 select * from avggrade
189 
190 
191 drop table avggrade

 

你可能感兴趣的:(T-SQL 查询、修改数据表)