9.1 数据表概述
9.2 创建数据表
9.2.1 数据表的逻辑结构
列的5种常用数据类型
1、字符类型
a、char类型
b、varchar2类型
2、数值类型
3、日期时间类型
4、LOB类型
用于大型的,未被结构化的数据,如二进制文件,图片文件和其他类型的外部文件
a、BLOB类型:存储二进制对象,包括图像,音频,视频等
b、CLOB类型:存储字符格式的大型对象
c、BFILE类型:存储二进制格式的文件
5、ROWID数据类型
伪列类型
9.2.2 创建一个数据表
SQL> create table students(
2 stuno number(10) not null,
3 stuname varchar2(8),
4 sex char(2),
5 age int,
6 departno varchar2(2) not null,
7 classno varchar2(4) not null,
8 regdate date default sysdate);
Table created.
SQL> desc students;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
SQL> create table students_2
2 as select * from students;
Table created.
SQL> desc students_2;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
9.2.3 数据表的特性
1、存储参数
三个参数:initial,next,minextents,主要设置initial参数,为表指定分配的第一个盘区大小
2、数据块管理参数
(1)PCTFREE和PCTUSED
这两个参数用于控制数据块中空闲空间的使用方法,对于本地化管理的表空间而言,如果使用segment space management子句设置段的管理方式为auto,则无需设置这两个参数,如果表空间的段管理方式为manual,则需要设置。
(2)initrans参数
指定一个数据块所允许的并发事物数目
(3)重做日志参数
nologging和logging参数
(4)缓存参数
cache和nocache
9.3 维护数据表
9.3.1 增加和删除字段
SQL> desc students;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
SQL> alter table students add (province varchar2(10));
Table altered.
SQL> desc students;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
PROVINCE VARCHAR2(10)
删除单列
SQL> alter table students drop colunm province;
alter table students drop colunm province
*
ERROR at line 1:
ORA-00905: missing keyword
SQL> alter table students drop column province;
Table altered.
SQL> desc students;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
删除多列
SQL> alter table students drop (sex,age);
Table altered.
SQL> desc students;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
9.3.2 修改字段
语法格式:
alter table table_name modify column_name column_property
SQL> desc students;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
DEPARTNO NOT NULL
VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
SQL> alter table students modify departno varchar2(4);
Table altered.
SQL> desc students;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
DEPARTNO NOT NULL
VARCHAR2(4)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
9.3.3 重命名表和列
语法格式:
alter table table_old_name rename to table_new_name
alter table table_name rename column column_old_name to column_new_name
SQL> alter table students_2 rename to students_1;
Table altered.
9.3.4 改变表空间和存储参数
1、修改表空间
SQL> col table_name for a20;
SQL> select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
-------------------- --------------------
DEPT USERS
EMP USERS
BONUS USERS
SALGRADE USERS
STUDENTS TBS_TEST_1
STUDENTS_1 TBS_TEST_1
STUDENTS_3 TBS_TEST_1
7 rows selected.
SQL> alter table students_1 move tablespace tbs_test_2;
Table altered.
SQL> select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
-------------------- --------------------
DEPT USERS
EMP USERS
BONUS USERS
SALGRADE USERS
STUDENTS TBS_TEST_1
STUDENTS_3 TBS_TEST_1
STUDENTS_1 TBS_TEST_2
7 rows selected.
2、修改储存参数
主要是指修改数据块参数pctfree和pctused
9.3.5 删除表
语法格式:
drop table table_name [cascade constraints];
SQL> drop table students_3 cascade constraints;
Table dropped.
SQL> select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
-------------------- --------------------
DEPT USERS
EMP USERS
BONUS USERS
SALGRADE USERS
STUDENTS TBS_TEST_1
STUDENTS_1 TBS_TEST_2
6 rows selected.
SQL> col object_name for a30;
SQL> col original_name for a20;
SQL> select object_name,original_name
2 from recyclebin
3 where original_name='STUDENTS_3';
OBJECT_NAME ORIGINAL_NAME
------------------------------ --------------------
BIN$7YshJM6SsALgQPrc4kAUGg==$0 STUDENTS_3
SQL> flashback table students_3 to before drop;
Flashback complete.
SQL> select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
-------------------- --------------------
DEPT USERS
EMP USERS
BONUS USERS
SALGRADE USERS
STUDENTS TBS_TEST_1
STUDENTS_3 TBS_TEST_1
STUDENTS_1 TBS_TEST_2
7 rows selected.
9.3.6 修改表的状态
将表置于read only状态
SQL> select table_name,read_only from user_tables;
TABLE_NAME READ_ONLY
-------------------- ---------
DEPT NO
EMP NO
BONUS NO
SALGRADE NO
STUDENTS NO
STUDENTS_3 NO
STUDENTS_1 NO
7 rows selected.
SQL> alter table students_3 read only;
Table altered.
SQL> select table_name,read_only from user_tables;
TABLE_NAME READ_ONLY
-------------------- ---------
DEPT NO
EMP NO
BONUS NO
SALGRADE NO
STUDENTS NO
STUDENTS_3 YES
STUDENTS_1 NO
7 rows selected.
SQL> alter table students_3 read write;
Table altered.
SQL> select table_name,read_only from user_tables;
TABLE_NAME READ_ONLY
-------------------- ---------
DEPT NO
EMP NO
BONUS NO
SALGRADE NO
STUDENTS NO
STUDENTS_3 NO
STUDENTS_1 NO
7 rows selected.
9.4 数据完整性和约束性
9.4.1 非空约束(not null)
SQL> desc students_3
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
SQL> alter table students_3 modify stuname not null;
Table altered.
SQL> desc students_3;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME
NOT NULL
VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
SQL> alter table students_3 modify stuname null;
Table altered.
SQL> desc students_3;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
9.4.2 主键约束(primary key)
主键约束用于唯一标识每一行记录,在一个表中,最大只能有一个主键约束,主键约束同时也具有非空约束的特性。
如果主键约束由一列组成,称为行级约束
由两个或两个以上列组成,称为表级约束
SQL> alter table students_3
2 add constraint student_pk primary key(stuno);
Table altered.
SQL> col table_name for a10;
SQL> col constraint_name for a30;
SQL> select table_name,constraint_name
2 from user_constraints;
TABLE_NAME CONSTRAINT_NAME
---------- ------------------------------
STUDENTS SYS_C0011172
STUDENTS SYS_C0011171
STUDENTS SYS_C0011173
STUDENTS_1 SYS_C0011174
STUDENTS_1 SYS_C0011176
STUDENTS_3 BIN$7YshJM6PsALgQPrc4kAUGg==$0
STUDENTS_3 BIN$7YshJM6QsALgQPrc4kAUGg==$0
STUDENTS_3 BIN$7YshJM6RsALgQPrc4kAUGg==$0
STUDENTS_1 SYS_C0011175
EMP FK_DEPTNO
DEPT PK_DEPT
TABLE_NAME CONSTRAINT_NAME
---------- ------------------------------
EMP PK_EMP
STUDENTS_3 STUDENT_PK
13 rows selected.
SQL> alter table students_3
2 drop constraint student_pk;
Table altered.
9.4.3 唯一性约束(unique)
唯一性约束强调所在的列不允许有相同的值,但允许空值
SQL> col table_name for a20;
SQL> col tablespace_name for a20;
SQL> select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
-------------------- --------------------
DEPT USERS
EMP USERS
BONUS USERS
SALGRADE USERS
STUDENTS TBS_TEST_1
STUDENTS_3 TBS_TEST_1
STUDENTS_1 TBS_TEST_2
7 rows selected.
SQL> desc students_3;
Name Null? Type
----------------------------------------- -------- ----------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
SQL> alter table students_3 add(
2 phone varchar2(20),
3 email varchar2(30),
4 qq varchar2(20) constraint qq_uk unique);
Table altered.
SQL> desc students_3;
Name Null? Type
----------------------------------------- -------- ----------------------------
STUNO NOT NULL NUMBER(10)
STUNAME VARCHAR2(8)
SEX CHAR(2)
AGE NUMBER(38)
DEPARTNO NOT NULL VARCHAR2(2)
CLASSNO NOT NULL VARCHAR2(4)
REGDATE DATE
PHONE VARCHAR2(20)
EMAIL VARCHAR2(30)
QQ VARCHAR2(20)
SQL> alter table students_3 add constraint email_uk unique(email);
Table altered.
SQL> col table_name for a10;
SQL> col constraint_name for a30;
SQL> col constraint_type for a20;
SQL> select table_name,constraint_name,constraint_type from user_constraints;
TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE
---------- ------------------------------ --------------------
STUDENTS SYS_C0011172 C
STUDENTS SYS_C0011171 C
STUDENTS SYS_C0011173 C
STUDENTS_1 SYS_C0011174 C
STUDENTS_1 SYS_C0011176 C
STUDENTS_3 BIN$7YshJM6PsALgQPrc4kAUGg==$0 C
STUDENTS_3 BIN$7YshJM6QsALgQPrc4kAUGg==$0 C
STUDENTS_3 BIN$7YshJM6RsALgQPrc4kAUGg==$0 C
STUDENTS_1 SYS_C0011175 C
EMP FK_DEPTNO R
DEPT PK_DEPT P
TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE
---------- ------------------------------ --------------------
EMP PK_EMP P
STUDENTS_3 STUDENT_PK P
STUDENTS_3 QQ_UK U
STUDENTS_3 EMAIL_UK U
15 rows selected.
SQL> alter table students_3 drop constraint email_uk;
Table altered.
SQL> select table_name,constraint_name,constraint_type from user_constraints;
TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE
---------- ------------------------------ --------------------
STUDENTS SYS_C0011172 C
STUDENTS SYS_C0011171 C
STUDENTS SYS_C0011173 C
STUDENTS_1 SYS_C0011174 C
STUDENTS_1 SYS_C0011176 C
STUDENTS_3 BIN$7YshJM6PsALgQPrc4kAUGg==$0 C
STUDENTS_3 BIN$7YshJM6QsALgQPrc4kAUGg==$0 C
STUDENTS_3 BIN$7YshJM6RsALgQPrc4kAUGg==$0 C
STUDENTS_1 SYS_C0011175 C
EMP FK_DEPTNO R
DEPT PK_DEPT P
TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE
---------- ------------------------------ --------------------
EMP PK_EMP P
STUDENTS_3 STUDENT_PK P
STUDENTS_3 QQ_UK U
14 rows selected.
9.4.4 外键约束
外键是指“当前表”(即外键表)引用“另外一个表”(即被引用表)的某个列或某几个列,而“另外一个表”中被引用的列必须具有主键约束或者唯一性约束。
SQL> conn scott/tiger
Connected.
SQL> col table_name for a15;
SQL> select table_name from user_tables;
TABLE_NAME
---------------
DEPT
EMP
BONUS
SALGRADE
STUDENTS
STUDENTS_3
STUDENTS_1
7 rows selected.
SQL> create table emp_temp
2 as select * from emp
3 where deptno=30;
Table created.
SQL> alter table emp_temp
2 add constraint temp_deptno_fk foreign key(deptno)
3 references dept(deptno);
Table altered.
SQL> insert into emp_temp(empno,ename,job,deptno)
2 values(9527,'stone','salesman',90);
insert into emp_temp(empno,ename,job,deptno)
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.TEMP_DEPTNO_FK) violated - parent key
not found
定义外键约束时,通过关键字on指定引用行为的类型,当尝试删除被引用表中的一条记录时,通过引用行为可以确定如何处理外键表中的外键列。引用行为的类型包括3种。
a、在定义外键约束时,如果使用了关键字no action,那么当删除被引用表中的被引用列的数据时将违反外键约束,该操作将被禁止执行,这也是外键的“默认引用类型”
b、在定义外键约束时,如果使用了关键字set null,那么当被引用表中被引用列的数据被删除时,外键表中外键列被设置为null,要使这个关键字起作用,外键列必须支持null值。
c、在定义外键约束时,如果使用了cascade关键字,那么当被引用表中被引用列的数据被删除时,外键表中对应的数据也将被删除,这种方式通常称为“级联删除”,它在实际应用程序开发中得到比较广泛应用。
创建表
SQL> create table dept_temp
2 as select * from dept
3 where deptno=30;
Table created.
创建主键约束
SQL> alter table dept_temp
2 add primary key(deptno);
Table altered.
查看主键约束
SQL> select table_name,constraint_name,constraint_type from user_constraints
2 where table_name='DEPT_TEMP';
TABLE_NAME CONSTRAINT_NAME CONSTRAINT
---------- ------------------------------ ----------
DEPT_TEMP SYS_C0011198 P
创建外键约束
SQL> alter table emp_temp
2 add constraint temp_deptno_fk2 foreign key(deptno)
3 references dept_temp on delete cascade;
Table altered.
查看外键约束
SQL> select table_name,constraint_name,constraint_type from user_constraints where table_name='EMP_TEMP';
TABLE_NAME CONSTRAINT_NAME CONSTRAINT
---------- ------------------------------ ----------
EMP_TEMP TEMP_DEPTNO_FK R
EMP_TEMP TEMP_DEPTNO_FK2 R
SQL> select count(*) from emp_temp where deptno=30;
COUNT(*)
----------
6
SQL> delete from dept_temp
2 where deptno=30;
1 row deleted.
SQL> select count(*) from emp_temp where deptno=30;
COUNT(*)
----------
0
删除约束
SQL> alter table emp_temp
2 drop constraint temp_deptno_fk2;
Table altered.
SQL> select table_name,constraint_name,constraint_type from user_constraints where table_name='EMP_TEMP';
TABLE_NAME CONSTRAINT_NAME CONSTRAINT
---------- ------------------------------ ----------
EMP_TEMP TEMP_DEPTNO_FK R
9.4.5 禁用和激活约束
1、在定义约束时禁用
使用disable关键字
SQL> create table student(
2 stucode varchar2(4) not null,
3 stuname varchar2(10) not null,
4 age int constraint age_ck check(age>0 and age<120)
disable,
5 province varchar2(20),
6 schoolname varchar2(50));
Table created.
2、禁用已经存在的约束
SQL> col status for a10;
SQL> select table_name,constraint_name,constraint_type,status
2 from user_constraints
3 where table_name='EMP_TEMP';
TABLE_NAME CONSTRAINT_NAME CONSTRAINT STATUS
---------- ------------------------------ ---------- ----------
EMP_TEMP TEMP_DEPTNO_FK R ENABLED
SQL> alter table emp_temp
2 disable constraint temp_deptno_fk;
Table altered.
SQL> select table_name,constraint_name,constraint_type,status
2 from user_constraints
3 where table_name='EMP_TEMP';
TABLE_NAME CONSTRAINT_NAME CONSTRAINT STATUS
---------- ------------------------------ ---------- ----------
EMP_TEMP TEMP_DEPTNO_FK R DISABLED
激活约束:enable
语法格式:
alter table table_name
enable [novalidate|validate] constraint con_name;
novalidate:表示在激活约束时不验证表中已经存在的数据是否满足约束
validate:要验证
SQL> alter table students_3
2 disable constraint student_pk;
SQL> insert into students_3(stuno,departno,classno)
2 values(1111,11,11);
1 row created.
SQL> insert into students_3(stuno,departno,classno)
2 values(1111,11,11);
1 row created.
SQL> alter table students_3
2 enable constraint student_pk;
alter table students_3
*
ERROR at line 1:
ORA-02437: cannot validate (SCOTT.STUDENT_PK) - primary key violated
9.4.6 删除约束
语法格式:
alter table table_name
drop constraint con_name
SQL> alter table student
2 drop constraint age_ck;
Table altered.