Oracle_SQL(4) DDL 表和约束

数据库对象分为占存储空间的对象和不占存储存储空间的对象。
占存储空间的对象主要包括:表、索引等。
select distinct segment_type from dba_segments order by segment_type;
不占存储空间的对象主要包括:视图、序列、函数、存储过程、包、触发器等。
select distinct object_type from dba_objects order by object_type;

DDL是用来创建(create)、替换(replace)、更改(alter)、
删除(drop)、重命名(rename)、截断(truncate)数据库对象的语句。
本节重点讲解表,索引、视图、序列、函数、存储过程、包、触发器等对象在后续plsql课程中逐个讲解。

一、表(Table )
数据库中的表与我们日常生活中使用的表格类似,
它也是由行(Row) 和列(Column)组成的。
列由同类的信息组成,每列又称为一个字段,每列的标题称为字段名。
行包括了若干列信息项。
一行数据称为一个或一条记录,它表达有一定意义的信息组合。
一个数据库表由一条或多条记录组成,没有记录的表称为空表。
每个表中通常都有一个主关键字,用于唯一地确定一条记录。
1.示例
view dept;
create table DEPT
(
deptno NUMBER(2) not null,
dname VARCHAR2(14),
loc VARCHAR2(13)
);
在此基础上创建我们的学生表student
create table student
(
sid number(10) not null,
sname varchar2(20),
birth_date date
);

2.创建表语法
create table 表名
(
列名 数据类型(长度) 约束,
...
);

3.数据类型
3.1 数值类型number
总长40位,其中一位用来表示正负,一位用来表示小数点,
数字的最长38位。
整数number(n)
小数number(m,n) m是总长度,n是小数位数,(m-n)是整数位数
3.2 字符类型varchar2
可变长字符串,总长4000个字节。
3.3 日期类型date
有效范围公元前4712年1月1日,到公元后9999年12月31日
3.4 大文本CLOB
A character large object containing single-byte characters.
Both fixed-width and variable-width character sets are
supported, both using the CHAR database character set.
Maximum size is 4 gigabytes.
3.5 二进制大文件BLOB
A binary large object. Maximum size is 4 gigabytes.
3.6 早期的数值类型
LONG 长整形 Character data of variable length up to 2 gigabytes, or 2^31 -1 bytes.
3.7 早期的字符类型
CHAR(size) Fixed-length character data of length size bytes. Maximum size is 2000 bytes. Default and minimum size is 1 byte.
NCHAR(size) Fixed-length character data of length size characters. Maximum size is determined by the national character set definition, with an upper limit of 2000 bytes. Default and minimum size is 1 character.
3.8 早期的日期类型
TIMESTAMP Year, month, and day values of date, as well as hour, minute, and second values of time
3.9 早期的二进制大文件
RAW(size) Raw binary data of length size bytes. Maximum size is 2000 bytes. You must specify size for a RAW value.
LONG RAW Raw binary data of variable length up to 2 gigabytes.

4.约束
4.1 非空约束not null
新增或修改记录时该列的值不允许为空
4.2 check约束check(sal>0)
新增或修改记录时该列的值需要满足check设定的条件
4.3 主键约束primary key(自带非空约束)
新增或修改记录时该列的值不能与其他记录的该列值重复
4.4 唯一键约束unique(不自带非空约束)
新增或修改记录时该列的值不能与其他记录的该列值重复
4.5 外键约束references dept (deptno)
新增或修改记录时该列的值需要在父表的主键列中存在
主键约束和唯一约束的区别:
主键约束自带非空约束;唯一键约束不自带非空约束。
主键一般是顺序号、有序列生成,无实际意义,
唯一键一般是实体记录的关键属性,有实际意义。
主键约束不能保证现实中同一个实体在系统中录入两次导致的重复;
唯一键约束可以保证现实中同一个实体在系统中只能录入一次。

5.较完整建表语句
create table EMP_4
(
empno NUMBER(4) primary key,
ename VARCHAR2(10) not null unique,
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE default sysdate,
sal NUMBER(7,2) default 0 not null check(sal>=0),
comm NUMBER(7,2),
deptno NUMBER(2) references DEPT (DEPTNO)
);
insert into emp4 (empno) values (null);
insert into emp4 (empno) values (1);
insert into emp4 (empno,ename,sal,deptno) values (1,'aaa',1000,10);
insert into emp4 (empno,ename,sal,deptno) values (1,'aaa',1000,10);
insert into emp4 (empno,ename,sal,deptno) values (2,'aaa',1000,10);
insert into emp4 (empno,ename,sal,deptno) values (2,'aab',1000,9);
insert into emp4 (empno,ename,sal,deptno) values (2,'aab',-1000,10);
insert into emp4 (empno,ename,sal,deptno) values (2,'aab',1000,10);

6.列的默认值default
新增或修改记录时如果该列的值为null,则将默认值赋给该列。
insert into emp4 (empno,ename,deptno) values (3,'aac',10);

7.删除表语法
drop table 表名 [CASCADE|RESTRICT];
drop table emp4;
CASCADE 删除表会将表中数据,表的相关视图和约束等一并删除;
RESTRICT 在没有视图和约束引用该表时,该表才能被删除,默认为RESTRICT。
--删除dept表,并将emp表到dept表的外键一起删除
drop table dept cascade constraints;

8.表重命名
语法:alter table 原表名 rename to 新表名;
alter table emp4 rename to emp1;

9.修改表结构
9.1 增加列
语法:alter table 表名 add (列名 列类型 列约束);
alter table emp1 add (phone number(11));
--alter table emp1 add (phone number(11),address varchar2(100));
9.2 删除列
alter table 表名 drop column 列名;
alter table emp1 drop column phone;
9.3 修改列
语法:alter table 表名 modify 列名 列类型;
alter table emp1 modify phone number(15); --列类型可以由小改大
alter table emp1 modify phone number(9); --没有数据时,列类型可以由大改小
alter table emp1 modify phone not null; --给列增加非空约束
9.4 增加主键约束
/*create table EMP2
(
empno NUMBER(4) not null,
ename VARCHAR2(10) not null,
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
); */
语法:alter table 表名 add constraint 约束名 primary key (列名,...);
alter table EMP2 add constraint PK_EMP2 primary key (EMPNO);
9.5 增加外键约束
语法:alter table 表名 add constraint 约束名 foreign key (列名,...)
references 父表名 (列名,...);
alter table EMP2 add constraint FK_EMP2_DEPTNO
foreign key (DEPTNO) references DEPT (DEPTNO);
9.6 增加唯一约束
语法:alter table 表名 add constraint 约束名 unique (列名,...);
alter table EMP2 add constraint UK_EMP2 unique (ename);
9.7 增加非空约束
alter table emp2 modify ename not null;
9.8 增加check约束
语法:alter table 表名 add constraint 约束名 check (表达式);
alter table EMP2 add constraint CHECK_EMP2_ename check (ename is not null);
alter table EMP2 add constraint CHECK_EMP2_SAL check (sal>=0);
9.9 约束的删除、启用、失效操作
语法:
alter table 表名 drop/disable/enable constraint 约束名;
举例:
alter table emp1 drop constraint CHECK_EMP1_SAL;
alter table emp1 disable constraint FK_EMP1_DEPTNO;
alter table emp1 enable constraint FK_EMP1_DEPTNO;

修改语法:alter table <tab> modify constraint 约束名 [ ENABLE | DISABLE ] [ VALIDATE | NOVALIDATE ] ;
alter table emp1 modify constraint CHECK_EMP1_ENAME disable;
alter table emp1 modify constraint FK_EMP1_DEPTNO disable;
alter table emp1 modify constraint PK_EMP1 disable;
alter table emp1 modify constraint UK_EMP1 disable;

10.截断(truncate)表
语法:truncate table 表名;
truncate table emp1;
11.复制表
create table emp2 as
select empno,ename,sal,job,mgr,deptno from emp where deptno=20;

猜你喜欢

转载自www.cnblogs.com/BradMiller/p/9279520.html