DataTable object

Here is not to do related interpretations, and directly on the sample:

创建一个course表,同时为主键约束列上的唯一性索引设置存储位置和存储参数。
SQL> CREATE TABLE course(
     cno   NUMBER(6) PRIMARY KEY,
     cname CHAR(20) UNIQUE 
     USING INDEX TABLESPACE indx
     STORAGE(INITIAL 64K NEXT 64K)
     ); 
创建一个SC表
SQL> CREATE TABLE  SC(
     sno NUMBER(6) REFERENCES student(sno),
     cno NUMBER(6) REFERENCES course(cno),
     grade NUMBER(5,2),
     CONSTRAINT SC_PK PRIMARY KEY(sno, cno)  
     ); 

说明:该例中定义了三个约束,
      ⑴其中两个是外键约束分别创建在sno列和cno列,均为列级。
      ⑵SC_PK是一个多列联合的主键约束,表级约束。

Define column-level FOREIGN KEY constraint
[CONSTRAINT constraint_name] [FOREIGN KEY]
the REFERENCES ref_table_name (column_name, ...)
define the table-level FOREIGN KEY constraint
[CONSTRAINT constraint_name] FOREIGN KEY (column_name, ...)
the REFERENCES ref_table_name (column_name, ...)
[the ON the DELETE CASCADE | NULL the SET | RESTRICTED];
CASCADE: delete all records from the table.
SET NULL: foreign key constraint column values from the table the associated record is NULL
the RESTRICTED: Delete limited, i.e. if the relevant record exists from the sub-table can not be deleted parent record in the primary table, the default reference.
DEFAULT
If the user does not insert a new row to provide data for the display columns, the system default value assigned to the column.
Syntax
[CONSTRAINT <constraint name>] DEFAULT expression

Add and remove constraints

The syntax to add a constraint:
the ALTER TABLE table_name
the ADD [CONSTRAINT constraint_name]
constraint_type (column1_name, column2_name, ...) [for condition Condition];

SQL> CREATE TABLE player(
     ID     NUMBER(6),
     sno    NUMBER(6),
     sname  VARCHAR2(10),
     sage   NUMBER(6,2),
     resume VARCHAR2(1000)
     );
  添加主键约束到ID列:
  SQL> ALTER TABLE player
  ADD CONSTRAINT P_PK PRIMARY KEY(ID);
  添加惟一性约束到sname列:
  SQL> ALTER TABLE player
  ADD CONSTRAINT P_UK UNIQUE(sname);
  添加检查约束,限制sage取值范围为(20,30):
  SQL> ALTER TABLE player
  ADD CONSTRAINT P_CK CHECK(sage BETWEEN 20 AND 30);
  添加外键约束到sno(主表为student):
  SQL> ALTER TABLE player
  ADD CONSTRAINT P_FK FOREIGN KEY(sno)
  REFERENCES student(sno) ON DELETE CASCADE;
  添加空/非空约束到resume列:
  SQL> ALTER TABLE player MODIFY resume NOT NULL;
  SQL> ALTER TABLE player MODIFY resume NULL;
  
  注意:为表列添加空/非空约束时必须使用MODIFY子句代替ADD子句。

Delete constraints
using ALTER TABLE ... DROP statement removes the constraints that have been defined.
You can delete the name by directly specifying constraints constraint, or specify the content constraint to drop a constraint.

删除指定内容的约束
SQL> ALTER TABLE player DROP UNIQUE(sname);
删除指定名称的约束
SQL> ALTER TABLE player DROP CONSTRAINT P_CK; 

Note: Delete table column space / time constraints must also be non-empty instead of using the MODIFY clause DROP clause.

Published 36 original articles · won praise 26 · views 7576

Guess you like

Origin blog.csdn.net/weixin_43566977/article/details/103665730