约束

约束包括:
not null ,unique ,primary key,renferntial,check,

UNIQUE constraints cannot be created on columns
of type CLOB, NCLOB, BLOB, LONG, LONG RAW, or TIMESTAMP WITH TIMEZONE.

不能在上面所说的上面建立UNIQUE,null 不算是,换句话就是说可以有多个NULL。可以利用索引来加强约束。
CREATE TABLE employees
(employee_id NUMBER NOT NULL
,hire_date DATE NOT NULL
,first_name VARCHAR2(42)
,last_name VARCHAR2(42)
,payroll_id VARCHAR2(10)
,CONSTRAINT uniq_payroll_id UNIQUE (payroll_id)
USING INDEX TABLESPACE indx
);
在建立表后 还可以加UNIQUE限制
ALTER TABLE employees ADD
CONSTRAINT uniq_payroll_id UNIQUE (payroll_id)
USING INDEX TABLESPACE indx


primay key
一个表只能有一个primay key,主要定义了primay key,那么同时也就是not null 和unique,强行利用index。
和unique一样,不能在CLOB, NCLOB, BLOB, LONG, LONG RAW, or TIMESTAMP WITH TIMEZONE.上建立

改变约束        
删除
ALTER TABLE employees DROP CONSTRAINT validate_hire_date;
删除主键
ALTER TABLE employees DROP PRIMARY KEY CASCADE;
修改约束名字
ALTER TABLE employees
RENAME CONSTRAINT validate_hire_date TO hire_date_check;

foreign key
ALTER TABLE employees
ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_nbr)
REFERENCES departments(dept_nbr) ON DELETE CASCADE;
ALTER TABLE departments ADD CONSTRAINT
dept_mgr_fk FOREIGN KEY (manager_id) REFERENCES
employees(employee_id) ON DELETE SET NULL;

你可能感兴趣的:(约束)