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