创建表头
create TABLE manager as SELECT *from emp WHERE 1=0
创建表
create TABLE manager as SELECT *from emp
插入数据
1. INSERT into emp_back SELECT *from emp
2. INSERT into emp_back values (7711,'zhangsan','teacher',7902,'1988-05-23',5300,400,20))
修改数据
UPDATE MANAGER set sal=sal+500 WHERE deptno IN (SELECT deptno FROM dept WHERE loc in('NEW YORK','CHICAGO'))
删除数据
1. DELETE from dept WHERE deptno=60
2. DELETE from manager where deptno=(SELECT deptno from DEPT WHERE dname='sales')
使用相关子查询删除
3. DELETE from dept WHERE 0=(SELECT count(*) from emp WHERE DEPT.DEPTNO=deptno)
4. DELETE from manager WHERE mgr=7566
5. DELETE from manager WHERE deptno=(SELECT deptno from dept WHERE loc='NEW YORK')
6. DELETE from manager outerTab WHERE sal >(SELECT avg(sal) from manager WHERE outerTab.deptno=deptno )
约束
主键约束(),用来确保表中的某一列的所用数据必须唯一,且不能包括空值,一个表中只能有一个主键约束
外键约束(FOREIGN KEY),主键表的某些主键值被外键引用时,这些主键值不允许修改。
唯一约束(UNIQUE),定义唯一约束的列称为唯一键,唯一约束的列可以有空值,除非在此列上定义 NOT NULL 约束
非空约束
数据字典:
- 和约束相关的数据字典
- USER_CONSTRAINTS:查看表中的所有约束,
- USER_CONS_COLUMNS:查看与约束相关的列名,对于由系统指定名字的约束特别有用
- 约束类型有C:CHECK, P:primary KEY,R:FOREIGN key