建好表后再添加约束也行,一些修改约束实例:alter table student add constraint pk_student primary key(IID); alter table student modify(IID default 0); alter table student drop primary key; alter table student drop constraint pk_student;
--1、在创建表的时候就建立 create table Customers(CustomerId char(8) primary key,Name varchar2(30) not null, --姓名不能为空Address varchar2(150),Email varchar2(100) unique,--必须唯一Gender char(2) default('男') check(Gender in('男','女')),CardId char(18)); ); --2、添加约束alter table Customers modify Address not null;--not null约束alter table Customers add constraint Address unique(CardId);--唯一约束alter table Customers add constraint CK_Address check(Address in ('合肥');--check约束 --3、删除约束 alter table 表名 drop constraint 约束名称; alter table 表名 drop primary key; --这是因为如果在两表存在主从关系,那么在删除主表主键约束时,必须带上cascade选项。 alter table 表名 drop primary key cascade;
你所提到的是约束里面的非空约束。举个例子: Create table person( Name varchar(30) NOT NULL ); 还有一楼想要的是主键约束,主键约束是不能重复,也不能为空的。比如: Create table person ( Pid number primary key );
room_id CHAR(6),
room_name VARCHAR2(20),
room_type VARCHAR2(10),
room_device VARCHAR2(20),
room_size number(3,0)
);
/CREATE TABLE object_classroom_info OF classroom_info_typ;
在这种情况下,room_id要设置为关键字,如何实现?
create table t_test
(
部门 VARCHAR2(50) not null,
姓名 VARCHAR2(50) not null, --姓名不能为空
权重 NUMBER check (权重>=10) --插入或修改时,权重必须大于等于10
)
--或者
create table t_test
(
部门 VARCHAR2(50),
姓名 VARCHAR2(50),
权重 NUMBER check (权重>=10)
)
ALTER TABLE t_test
MODIFY (姓名 NOT NULL);
http://www.cnblogs.com/Ronger/archive/2011/10/10/2205515.html
alter table student modify(IID default 0);
alter table student drop primary key;
alter table student drop constraint pk_student;
create table Customers(CustomerId char(8) primary key,Name varchar2(30) not null, --姓名不能为空Address varchar2(150),Email varchar2(100) unique,--必须唯一Gender char(2) default('男') check(Gender in('男','女')),CardId char(18));
);
--2、添加约束alter table Customers modify Address not null;--not null约束alter table Customers add constraint Address unique(CardId);--唯一约束alter table Customers add constraint CK_Address check(Address in ('合肥');--check约束
--3、删除约束
alter table 表名 drop constraint 约束名称;
alter table 表名 drop primary key;
--这是因为如果在两表存在主从关系,那么在删除主表主键约束时,必须带上cascade选项。
alter table 表名 drop primary key cascade;
Create table person(
Name varchar(30) NOT NULL
);
还有一楼想要的是主键约束,主键约束是不能重复,也不能为空的。比如:
Create table person
(
Pid number primary key
);