建表并修改表约束
来源:互联网 发布:软件许可授权模板 编辑:程序博客网 时间:2024/06/05 11:59
主键约束 primary key
not null
check
unique 唯一约束
create table student( --学生表
xh number(4) constraint pk_stu primary key, --学号主键
xm varchar2(10) constraint nn_stu not null, --姓名不能为空
sex char(2) constraint ck_stu_sex check (sex in ('男','女')), --性别
birthday date constraint uq_bir unique, --日期
sal number(7,2) constraint ck_sal check (sal between 500 and 1000)--奖学金 sal >=500 and sal <=1000
);
<2>建立约束的同时给约束指定名字,便于删除
create table cla( --班级表
id number(2) constraint pk_cla primary key, --班级编号
cname varchar2(20) constraint nn_cla not null --班级名字
);
create table stu( --学生表
xh number(4) constraint pk_stu primary key, --学号是主键
xm varchar2(20) constraint nn_stu not null, --姓名非空
age number(2) constraint ck_stu check (age between 10 and 90),--年龄在10到90之间(10<= age <=90 )
birthday date,
shenfenzheng number(18) constraint uq_stu unique, --身份证唯一
classid number(2) constraint fk_stu references cla(id) -- 班级编号外键
--(引用的一定是另外表的主键或唯一性约束的字段)
);
3)建完表后加约束
加约束
加主键
alter table student add constraint pk_stu
primary key (xh);
加非空
alter table student modify (xm not null);
检查约束
alter table student add check(sex in ('男','女'));
alter table student add constraint ck_sal check(sal between 500 and 1000));
添加 主键
alter table cla add constraint pk_cla
primary key (id);
加 not null
alter table cla modify
(cname not null);
学生表student:
create table student(
xh number(4) ,
xm varchar2(20) ,
age number(2),
birthday date,
shenfenzheng number(18),
classid number(2) references cla(id)
);
加外键约束
alter table student add constraint fk_stu
foreign key (classid) references cla(id);
加主键
alter table student add constraint pk_stu
primary key (xh);
加not null
alter table student modify(xm not null);
加检查约束
alter table student add constraint cc_age
check (age >= 10 and age <=90);
加唯一约束
alter table student add constraint
uq_sfz unique(shenfenzheng);
加外键约束
alter table student add constraint
fk_stu foreign key (classid)
references cla(id);
not null
check
unique 唯一约束
create table student( --学生表
xh number(4) constraint pk_stu primary key, --学号主键
xm varchar2(10) constraint nn_stu not null, --姓名不能为空
sex char(2) constraint ck_stu_sex check (sex in ('男','女')), --性别
birthday date constraint uq_bir unique, --日期
sal number(7,2) constraint ck_sal check (sal between 500 and 1000)--奖学金 sal >=500 and sal <=1000
);
<2>建立约束的同时给约束指定名字,便于删除
create table cla( --班级表
id number(2) constraint pk_cla primary key, --班级编号
cname varchar2(20) constraint nn_cla not null --班级名字
);
create table stu( --学生表
xh number(4) constraint pk_stu primary key, --学号是主键
xm varchar2(20) constraint nn_stu not null, --姓名非空
age number(2) constraint ck_stu check (age between 10 and 90),--年龄在10到90之间(10<= age <=90 )
birthday date,
shenfenzheng number(18) constraint uq_stu unique, --身份证唯一
classid number(2) constraint fk_stu references cla(id) -- 班级编号外键
--(引用的一定是另外表的主键或唯一性约束的字段)
);
3)建完表后加约束
加约束
加主键
alter table student add constraint pk_stu
primary key (xh);
加非空
alter table student modify (xm not null);
检查约束
alter table student add check(sex in ('男','女'));
alter table student add constraint ck_sal check(sal between 500 and 1000));
添加 主键
alter table cla add constraint pk_cla
primary key (id);
加 not null
alter table cla modify
(cname not null);
学生表student:
create table student(
xh number(4) ,
xm varchar2(20) ,
age number(2),
birthday date,
shenfenzheng number(18),
classid number(2) references cla(id)
);
加外键约束
alter table student add constraint fk_stu
foreign key (classid) references cla(id);
加主键
alter table student add constraint pk_stu
primary key (xh);
加not null
alter table student modify(xm not null);
加检查约束
alter table student add constraint cc_age
check (age >= 10 and age <=90);
加唯一约束
alter table student add constraint
uq_sfz unique(shenfenzheng);
加外键约束
alter table student add constraint
fk_stu foreign key (classid)
references cla(id);
- 建表并修改表约束
- SQL Server 约束 修改表
- MySQL表级约束与列级约束、修改数据表
- Oracle11g简单建表(约束)、查询、修改脚本
- 创建表_修改表_约束
- oracle 创建表、修改表、添加约束
- MySQL创建、修改表、表的约束
- oracle 创建表约束,修改,删除
- MySQL修改表的外键约束
- 修改表和约束(alter语句)
- MySQL中的表、字段的约束修改
- Mysql修改约束,列信息,表信息
- oracle建表约束
- MySQL入门——修改数据表4:添加主键约束、显示表结构、添加唯一约束、添加外键约束
- oracle 表约束的添加、修改以及约束的禁用启用
- oracle 表约束的添加、修改以及约束的禁用启用
- oracle 表约束的添加、修改以及约束的禁用启用
- MySQL入门——修改数据表4:添加主键约束、显示表结构、添加唯一约束
- 最短路的几种常用算法
- ARP欺骗小游戏
- xp下运行gclient.bat出现错误 [-] SHELL.APPLICATION 80070002: Failed to open zip file.
- input输入框为只读灰色
- SVM入门(八)松弛变量
- 建表并修改表约束
- android广播发送与接收演示DEMO
- android学习笔记(八)SharedPreferences、editor
- hadoop节点热删除(动态删除)
- vmwar下linux挂载光驱失败
- ASCII码对照表
- 有点意思的C/C++问题及解答:16-20
- 面试
- 收集的Chrome学习资料