1 --创建图书表 2 create table books_lib 3 ( 4 book_id number(10) primary key, --unique¬ null 5 book_name varchar2(50) not null 6 ); 7 alter table books_lib add publisher varchar2(50) null; 8 desc books_lib; 9 --增加check约束 10 alter table books_lib add qty number check(qty>0 and qty<100) unique not null ; 11 insert into books_lib values (1,'Oracle编程','中国出版公司',1000); 12 to_date('1998-07-05','yyyy-mm-dd'); 13 -- 14 create table emp_virtual( 15 empno varchar2(10) primary key, 16 sal number(7,2), 17 comm_rct number(7,2), 18 comm_sal number(7,2) generated always as (sal*comm_rct) virtual 19 ); 20 insert into emp_virtual(empno,sal,comm_rct) values ('BN7369',5000,0.23); 21 select * from emp_virtual; 22 update emp set sal=5000 where empno=7369; 23 delete * from emp_virtual 24 --修改表列 25 alter table books_lib modify publisher varchar2(100); 26 --删除默认值 27 alter table books_lib modify publish_date default null; 28 --删除表列 29 alter table books_lib drop column publish_date; 30 --同时删除publisher和qty列,并删除与其相关的多列约束 31 alter table books_lib drop (publisher,qty) cascade constraint; 32 --部分删除表列 33 删除大量的数据,需要执行很长的时间 34 将drop column替换成set unused 未使用状态 35 alter table boos_lib set unused(isbn) cascade constraint; 36 --删除被标记为unused的列 37 alter table books_lib drop unused columns; 38 --重命名表列 39 alter table books_lib rename column qty to books_qty; 40 --重命名表 41 alter table scott.bookslib rename to booksnewlib; 42 --删除数据表 43 drop table books_lib; 44 --从回收站(闪回恢复区)中恢复表 45 flashback table books_lib to before drop; 46 --立即删除,无法恢复 47 drop table books_lib cascade constraints purge; 48 49 --表注释 50 comment on table student is '学生表'; 51 comment on column student.sno is '学号(主键)'; 52 --查看表的comment 53 select * from all_tab_comments where table_name=upper('student'); 54 --查看列的comment 55 select * from all_col_comments where table_name=upper('student'); 56 57 58 CREATE TABLE STUNDET 59 ( 60 CONSTRAINT FK_BLUE FOREIGN KEY(ID) REFRENCES CLASSid(id) 61 ) 62 63 64 外键约束的作用 65 外键是该表是另一个表之间联接的字段 外键必须为另一个表中的主键 外键的用途是确保数据的完整性。它通常包括以下几种: 实体完整性,确保每个实体是唯一的(通过主键来实施) 66 67 1 域完整性,确保属性值只从一套特定可选的集合里选择 68 2 关联完整性,确保每个外键或是NULL(如果允许的话)或含有与相关主键值相配的值 69 70 有外键约束的表CRUD操作 71 1 增加数据:先插入外表,再插入主表 72 2 删除数据:先删除主表再删除外表, 73 3 有对应记录的值,不能修改当前列; 74 75 create table teacher( 76 tno varchar2(3) not null, 77 tname varchar2(9) not null, 78 tsex varchar2(3) not null, 79 tbirthday date, 80 prof varchar2(9), 81 depart varchar2(15) not null, 82 constraint pk_teacher primary key(tno) 83 ); 84 85 create table course( 86 cno varchar2(5) not null, 87 cname varchar2(15) not null, 88 tno varchar2(3) not null, 89 constraint pk_course primary key(cno) 90 ); 91 create table student( 92 sno varchar2(3) not null, 93 sname varchar2(9) not null, 94 ssex varchar2(3) not null, 95 sbirthday date, 96 sclass varchar2(5), 97 constraint pk_student primary key(sno) 98 ); 99 comment on column student.sno is '学号(主键)'; 100 create table score( 101 sno varchar2(3) not null, 102 cno varchar2(5) not null, 103 degree number(4,1), 104 constraint pk_score primary key(sno,cno)--复合主键 105 ); 106 comment on column score.sno is '学号(主键)'; 107 108 alter table score add constraint fk_sno foreign key(sno) references student(sno); 109 --Student表 110 insert into student(sno,sname,ssex,sbirthday,sclass) values(108,'曾华','男',to_date('1977-09-01','yyyy-mm-dd'),95033); 111 --score表(添加外键后要先填Student,course表中数据去满足外键约束) 112 insert into course(cno,cname,tno) values('3-105','计算机导论',825); 113 114 115 --添加外键 116 alter table course add constraint fk_tno foreign key(tno) references teacher(tno); 117 alter table emp add constraint fk_deptno foregin key(deptno) references dept(deptno); 118 119 已存在数据,如何修改表类型 120 --修改表名,表列 121 create table test( name1 varchar2(10), email varchar2(10)); 122 alter table test1 add email varchar2(20) 123 alter table test1 drop column email 124 alter table test rename to test1 --修改表名 125 alter table test1 rename column name1 to name --修改列名 126 alter table test1 modify name varchar2(10); --修改字段类型 127 --1 修改原字段名 128 alter table test1 rename column name to name1; 129 --2 增加一个与原字段同名字段 130 alter table test1 add name varchar2(10); 131 --3 将原字段的数据增加到新的字段 132 update test1 set name=trim(name1); 133 --4 更新完删除字段 134 alter table test1 drop column name1; 135 136 学生系统建表 137 --建表 138 --student表+注释 139 create table student( 140 sno varchar2(3) not null, 141 sname varchar2(9) not null, 142 ssex varchar2(3) not null, 143 sbirthday date, 144 sclass varchar2(5), 145 constraint pk_student primary key(sno) 146 ); 147 comment on column student.sno is '学号(主键)'; 148 comment on column student.sname is '学生姓名'; 149 comment on column student.ssex is '学生性别'; 150 comment on column student.sbirthday is '学生出生年月日'; 151 comment on column student.sclass is '学生所在班级'; 152 --course表+注释 153 create table course( 154 cno varchar2(5) not null, 155 cname varchar2(15) not null, 156 tno varchar2(3) not null, 157 constraint pk_course primary key(cno) 158 ); 159 comment on column course.cno is '课程编号(主键)'; 160 comment on column course.cname is '课程名称'; 161 comment on column course.tno is '教工编号(外键)'; 162 --score表+注释 163 create table score( 164 sno varchar2(3) not null, 165 cno varchar2(5) not null, 166 degree number(4,1), 167 constraint pk_score primary key(sno,cno) 168 ); 169 comment on column score.sno is '学号(主键)'; 170 comment on column score.cno is '课程编号(主键)'; 171 comment on column score.degree is '成绩'; 172 --teacher表+注释 173 create table teacher( 174 tno varchar2(3) not null, 175 tname varchar2(9) not null, 176 tsex varchar2(3) not null, 177 tbirthday date, 178 prof varchar2(9), 179 depart varchar2(15) not null, 180 constraint pk_teacher primary key(tno) 181 ); 182 comment on column teacher.tno is '教工编号(主键)'; 183 comment on column teacher.tname is '教工姓名'; 184 comment on column teacher.tsex is '教工性别'; 185 comment on column teacher.tbirthday is '教工出生年月'; 186 comment on column teacher.prof is '职称'; 187 comment on column teacher.depart is '教工所在单位'; 188 --添加外键 189 alter table course add constraint fk_tno foreign key(tno) references teacher(tno); 190 alter table score add constraint fk_sno foreign key(sno) references student(sno); 191 alter table score add constraint fk_cno foreign key(cno) references course(cno); 192 --添加数据 193 --Student表 194 insert into student(sno,sname,ssex,sbirthday,sclass) values(108,'曾华','男',to_date('1977-09-01','yyyy-mm-dd'),95033); 195 insert into student(sno,sname,ssex,sbirthday,sclass) values(105,'匡明','男',to_date('1975-10-02','yyyy-mm-dd'),95031); 196 insert into student(sno,sname,ssex,sbirthday,sclass) values(107,'王丽','女',to_date('1976-01-23','yyyy-mm-dd'),95033); 197 insert into student(sno,sname,ssex,sbirthday,sclass) values(101,'李军','男',to_date('1976-02-20','yyyy-mm-dd'),95033); 198 insert into student(sno,sname,ssex,sbirthday,sclass) values(109,'王芳','女',to_date('1975-02-10','yyyy-mm-dd'),95031); 199 insert into student(sno,sname,ssex,sbirthday,sclass) values(103,'陆君','男',to_date('1974-06-03','yyyy-mm-dd'),95031); 200 insert into student(sno,sname,ssex,sbirthday,sclass) values(104,'周晶晶','女',to_date('1974-06-03','yyyy-mm-dd'),95031); 201 --teacher表 202 insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(804,'李诚','男',to_date('1958/12/02','yyyy-mm-dd'),'副教授','计算机系'); 203 insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(856,'张旭','男',to_date('1969/03/12','yyyy-mm-dd'),'讲师','电子工程系'); 204 insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(825,'王萍','女',to_date('1972/05/05','yyyy-mm-dd'),'助教','计算机系'); 205 insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values(831,'刘冰','女',to_date('1977/08/14','yyyy-mm-dd'),'助教','电子工程系'); 206 --course表(添加外键后要先填teacher表中数据去满足外键约束) 207 insert into course(cno,cname,tno) values('3-105','计算机导论',825); 208 insert into course(cno,cname,tno) values('3-245','操作系统',804); 209 insert into course(cno,cname,tno) values('6-166','数字电路',856); 210 insert into course(cno,cname,tno) values('9-888','高等数学',831); 211 --score表(添加外键后要先填Student,course表中数据去满足外键约束) 212 insert into score(sno,cno,degree) values(103,'3-245',86); 213 insert into score(sno,cno,degree) values(105,'3-245',75); 214 insert into score(sno,cno,degree) values(109,'3-245',68); 215 insert into score(sno,cno,degree) values(103,'3-105',92); 216 insert into score(sno,cno,degree) values(105,'3-105',88); 217 insert into score(sno,cno,degree) values(109,'3-105',76); 218 insert into score(sno,cno,degree) values(101,'3-105',64); 219 insert into score(sno,cno,degree) values(107,'3-105',91); 220 insert into score(sno,cno,degree) values(108,'3-105',78); 221 insert into score(sno,cno,degree) values(101,'6-166',85); 222 insert into score(sno,cno,degree) values(107,'6-166',79); 223 insert into score(sno,cno,degree) values(108,'6-166',81); 224 225 226 --创建scott方案下的emp表副本并包含所有数据 227 create table emp_copy as select * from emp; 228 --创建一个架构而不包含任何表数据 229 create table emp_copy as select * from emp where 1=0; 230 select * from emp_copy; 231 --通过改变select 语句,创建不完全相同的类型 232 create table emp_copy_others as select empno, 233 to_char(hiredate,'yyyy-MM-dd') as hiredate from emp; 234 235 -- 在约束中使用函数和布尔 236 create table invoice_check_others 237 ( 238 invoice_name varchar2(20), 239 invoice_type int, 240 invoice_total number(9,2) default 0, 241 constraint invoice_ck CHECK(invoice_total between 1 and 1000), 242 constraint check_invoice_type check(invoice_type in(1,2,3,4)), 243 constraint check_invoice_name check(invoice_name=upper(invoice_name))--必须大写 244 ) 245 246 --查看表中所有的约束 247 select constraint_name,search_condition,status from user_constraints 248 where table_name=upper('invoice_check_others'); 249 250 251 --Merge语句 252 create table dept60_bonuses 253 ( 254 empno number,bonus_amt number 255 ); 256 insert into dept60_bonuses values(7369,0); 257 insert into dept60_bonuses values(7788,2); 258 insert into dept60_bonuses values(7876,3); 259 select empno,sal,ename from emp 260 select * from dept60_bonuses 261 --合并两张表,根据不同的语句删除,更新 262 --合并两张表,根据不同的语句删除,更新 263 merge into dept60_bonuses b 264 using ( select empno,sal,deptno from emp where deptno=20) e 265 on (b.empno=e.empno) 266 when matched then 267 update set b.bonus_amt =e.sal*0.2 268 where b.bonus_amt=0 269 delete where (e.sal>2500) 270 when not matched then 271 insert (b.empno,b.bonus_amt) 272 values (e.empno,e.sal*0.1) 273 where (e.sal<4000);