1 创建模式
2 create schema <schema_name> authorization <username> 没有指定schema_name时默认是用户名
3
4 删除模式
5 drop schema <schema_name> <cascade | restrict>
6
7
8 创建表
9 create table student.sc ;定义表中的两个主码以及外码
10 (sno char(7),
11 cno char(4),
12 grade smallint,
13 primary key(sno, cno),
14 foreign key sno references student(sno),
15 foreign key sno references course(cno)
16 ); /*最后一行没有逗号,外码引用的必须是主码*/
17
18 修改表
19 alter table <table_name>
20 [ add <新列名> <数据类型> [完整性约束] ]
21 [ drop [完整性约束名] [列名] ]
22 [ modify <列名> <数据类型> ];
23
24 删除表
25
26 drop table <表名>;
27
28
29 创建索引
30 create [unique] [cluster] index <索引名>
31 on <表名>( <列名> [ <次序> ] [, <列名> [ <次序> ] ] ....);
32
33 删除索引
34 drop index <索引名>
35
36
37 插入元组
38 insert
39 into <表名> [ ( <属性列1> [, <属性列2>...] ) ]
40 values ( <常量1> [, <常量2>] ... )
41
42 insert into sc(sno,cno)
43 values('200215128','1')
44
45 修改元组
46 update <表名>
47 set <列名> = <表达式> [, <列名> = <表达式> ]...
48 [where <条件>];
49
50 update student
51 set sage = 22
52 where sno = '200215121'
53
54 删除元组
55 delete
56 from <表名>
57 [where <条件> ];
58
59 delete
60 from student
61 where sno = '200215121'
62
63
64 使用视图创建语句建视图,通过视图查询数据:
65 create view <视图名> [(<列名>[,<列名>]...)] ;列名要么全部指定,要么全部不指定
66 as
67 <子查询>
68 [with check option];
69
70 drop view <视图名>;
71
72
73 创建用户
74 create user <username> [with] [DBA | RESOURCE | CONNECT];
75 create user zx_root IDENTIFIED by 'xxxxx@localhost';
76
77 删除用户
78 drop user <username>;
79
80
81 授权
82 grant <权限> [,<权限> ]... ;all privileges, select, update, insert, delete
83 on <对象类型> <对象名> [,<对象类型> <对象名>]...
84 to <用户> [,<用户>]... ;public
85 [with grant option];
86
87 grant all privileges
88 on table student, course
89 to u2, u3;
90
91 grant update(sno)
92 on table student
93 to u4;
94
95 grant insert
96 on table sc
97 to u5
98 with grant option
99
100 回收授权
101 revoke <权限>[,<权限>]...
102 on <对象类型> <对象名> [,<对象类型> <对象名>]...
103 from <用户> [,<用户>]...
104
105 revoke select
106 on table sc
107 from public
108
109
110 创建角色
111 create role <rolename>
112
113 给角色授权
114 grant <权限> [,<权限>]...
115 on <对象类型> <对象名>
116 to <角色> [,<角色>]...
117
118 grant <角色1> [,<角色2>]...
119 to <角色3> [,<角色4>]...
120 [with admin option]
121
122 收回角色权限
123 revoke <权限> [,<权限>]...
124 on <对象类型> <对象名>
125 from <角色> [,<角色>]...
126
127 create role r1;
128
129 grant select, update, insert
130 on table student
131 to r1;
132
133 grant r1
134 to 王平,张明
135
136 revoke select
137 on table student
138 from r1;
139
140
141 审计
142
143 audit alert,update
144 on sc;
145
146 noaudit all
147 on sc;
148
149
150 实体完整性
151 primary key(sno,cno);
152
153 参照完整性
154 foreign key sno reference student(sno);
155
156 用户定义完整性
157 create table sc
158 (sno char(7) not null,
159 cno char(4) not null,
160 grade smallint not null,);
161
162 create table dept
163 (deptno number,
164 dname varchar(9) unique,
165 );
166
167 create table student
168 (sno char(7) primary key,
169 ssex char(2) check (ssex in ('男','女')),);
170
171 表级用户定义完整性
172 check (ssex = '女' or sname not like 'ms.%' ); /*男性名字不能以ms.开头*/
173
174 完整性约束命名子句
175 constraint <完整性约束条件名> [primary key短语 | foreign key 短语 | check 短语]
176
177 create stable student
178 (sno number(5)
179 constraint c1 check (sno between 90000 and 99999),
180 sname varchar(20)
181 constraint c2 not null,
182 sage number(3)
183 constraint c3 check (sage <30),
184 ssex varchar(2)
185 constraint c4 check (ssex in ('男','女'),
186 constraint studentKey primary key(sno),
187 );
188
189 alter table student
190 drop constraint c4;
191
192 alter table student
193 add constraint c4 check (ssex in ('男','女'));
194
195 域中完整性限制
196 create domain genderdomain char(2)
197 constraint gd check (value in ('男','女'));
198
199 alter domain genderdomain
200 add constraint gd check (value in ('1','0'));
201
202 alter domain genderdomain
203 drop constraint gd;
204
205
206
207
208 查询
209 seletct [ all | distinct ] <目标列表达式> [, <目标列表达式> ]...
210 from <表名或视图名> [, <表名或视图名> ]...
211 [ where <条件表达式> ]
212 [ group by <列名1> [ having <条件表达式> ] ]
213 [ order by <列名2> [ asc | desc ] ]
214
215 表单查询
216
217 select sname name,2015-sage year
218 from student
219 where sno = 200215121
220
221 select sname, sdept, sage
222 from student
223 where sage between 20 and 23 /*not between and*/
224
225 select sname, ssex
226 from student
227 where sdept in ('cs','ma','is') /*not in*/
228
229 select *
230 from student
231 where sno like '2002%21' /*%任意多个字符,_单个字符, [ escape '\' ] 表示'\'为换码字符,not like */
232
233 select sno,cno
234 from sc
235 where grade is null /*is not*/
236
237 select sno,grade
238 from sc
239 where cno = '3'
240 order by grade desc,sno
241
242 select cno, count( distinct sno )
243 from sc
244 group by cno
245
246 连接查询,嵌套查询
247
248 select sname
249 from student
250 where sdept = 'cs' and sage < 20 /*or, not*/
251
252 select first.cno, second.cpno
253 from course first, course second
254 where first.cpno = second.cno /*<>是不等于*/
255
256 select sname
257 from student
258 where sno in
259 (select sno
260 from sc
261 where cno = '2' );
262
263 select sno,cno
264 from sc x
265 where grade >=
266 (select avg(grade)
267 from sc y
268 where y.sno = x.sno);
269
270 select sname,sage
271 from student
272 where sage < any
273 (select sage
274 from student
275 where sdept = 'cs'); /*all*/
276
277 select sname
278 from student
279 where not exists
280 (select *
281 from course
282 where not exists
283 (select *
284 from sc
285 where sno = student.sno
286 and cno = course.cno )); /*not exists 没有*/
287
288 集合查询:
289
290 select *
291 from student
292 where sdept='cs'
293 union
294 select *
295 from student
296 where sage<=19;
297
298 select *
299 from student
300 where sdept='cs'
301 intersect
302 select *
303 from student
304 where sage<=19;
305
306 select *
307 from student
308 where sdept='cs'
309 except
310 select *
311 from student
312 where sage<=19;
313
314
315
316 数据类型
317 char(n) 长度为n的定长字符串
318 varchar(n) 最大长度为n的可变字符串
319 int 长整形,可写作integer
320 smallint 短整形
321 numberic(p,d) 定点数,有p位数字(不包括符号,小数点)组成,小数点后有d位小数
322 real 取决于机器精度的浮点数
323 double precision 取决于机器精度的双精度浮点数
324 float(n) 浮点数,精度至少为n为数字
325 date 日期,YYYY-MM-DD
326 time 时间,HH:MM:SS
327
328
329
330 小问题
331
332
333 "=" 两边可以没有空格
334
335
336
337 实例:
338
339
340 create DataBase SpjDB
341 on (name=spj_dat,
342 filename='D:\Sql_Server\spj_data.mdf',
343 size=10mb)
344 log
345 on (name=spj_log,
346 filename='D:\Sql_Server\spj_log.ldf',
347 size=3mb)
348
349
350 Create table S
351 (SNO char(4) primary key,
352 SNAME char(10),
353 STATUS smallint,
354 CITY char(12))
355
356 insert into s
357 values('S1','精益','20','天津');
358
359 insert into s
360 values('S2','盛锡','10','北京');
361
362
363 Create table p
364 (PNO char(4) primary key,
365 PNAME char(10),
366 COLOR char(4),
367 WEIGHT smallint)
368
369 insert into P
370 values('p1','螺母','红','12');
371 insert into P
372 values('p2','螺栓','绿','17');
373 insert into P
374
375
376 Create table J
377 (JNO char(4) primary key,
378 PNAME char(10),
379 CITY char(10))
380
381 insert into J
382 values('J1','三建','北京');
383 insert into J
384 values('J2','一汽','长春');
385 insert into J
386
387
388 Create table SPJ
389 (SNO char(2),
390 PNO char(2),
391 JNO char(2),
392 QTY smallint)
393
394
395 insert into SPJ
396 values('S1','P1','J1','200');
397 insert into SPJ
398 values('S1','P1','J3','100');
399