Oracle(四)老师

Alter 修改表
字段重命名
alter table 表名..

字段重命名 alter table 表名 rename column 就列名 to 新列名
增加字段 alter table 表名 add 字段列表(例:a int |(a int,b varchar2(20)))
表重命名 alter table 表名 rename to 新表名
修改字段类型 alter talbe 表名 modify 列表名 列表类型;
修改表中字段精度alter talbe 表名 modify 列表名 列表类型;
删除字段 alter table 表名 drop (列表名);

约束(限制)constraint
primary key 主键
unique      唯一性约束   特点: 唯一,可以为空,可以插入多个空值(候选主键)
check     用户自定义约束 特点:根据用户的要求来进行限制

主键特点;
保证记录的唯一性,不能为空
注意:在表中只可以有一个主键
建表时添加主键
create table students(
id int constraint 名称 primary key, 添加约束的名
id int constraint 名称 unique ,
sex varchar2(20) constraint 名称 check(条件例如 sex=1),
sex varchar2(20) constraint 名称 check(sex='男' or sex='女')
name varchar2(20) not null
(alter table students modify name null)
name varchar2(20) default 1001
(alter table students modify name default null)
name varchar2(20));
建表后添加
alter table students add constraint students_pk primary key(id) ;
alter table students add constraint students_uq unique(id);
alter table students add constraint students_ck check(sex=1);对Sex进行check约束

也可以这么写 例如:
create table students(
id int,
name varchar2(20),
email varchar2(30),
constraint pk_students primary key(id),
constraint un_name     unique(name),
constraint un_email    unique(email));

禁用约束
alter table students disable constraint pk_students;(暂时让这个约束关闭)
alter table students enable constraint pk_studenst;(启动约束)
外键 特点:参照主键中存在的值,可以插入空值,可以插入重复值
表参照值
1、学生表
create table studenst(
id int,
name varchar2(20));
insert into studenst values(1001,tom);
insert into students values(1002,cat);
2、成绩表
create table score(    成绩表要参照学生表
sid int,
kid int,
score int));
 学生表
外键    alter table students add constraint pk_students primary key(id);
 成绩表
 alter table score add constraint fk_sid foreign key(sid) references studenst(id);
要想删除 外键
删除主键 alter table students drop constraint pl_students cascade;(级联 删除约束)

删除主表(被外联的主表)
drop table students cascade constraint;级联删除的事约束 删除 主表  附表存在
drop table 表名 cascade(级联) constraint(约束);

 

删除 外键内容

delete  先删除 主键 的 然后删除外键的
----------------------------------------------
create table students(
id int primary key);
如果想删除主键的话 可以这样
alter table students drop primary key; 主键唯一 可以直接删除这个

----------------------------------------老师----------------------------------------------------------------------------------

复习:
sql:
 DDL:CREATE ALTER DROP
 DML:INSERT UPDATE DELETE SELECT
 DCL:GRANT  REVOKE
 TCL:COMMIT ROLLBACK SAVEPOINT
 
 
1ALTER 修改表

创建
create table 表名(.....)
删除
drop   table 表名;
修改
alter table 表名...

字段重命名       rename column 旧列名 to 新列名
表重命名         rename to 新表名
增加字段         add ....
修改字段类型     modify......
修改表中字段精度 modify.......
删除字段         drop........

约束constraint
primary key 主键
unique      唯一性约束
check       用户定义约束


pirmary key 主键
主键特点:唯一、不能为空
注意:在表中只可以有一个主键

unique 唯一性(候选主键)
特点:唯一,可以插入多个空值


check 用户自定约束
特点:根据用户的要求来进行限制

 

约束
添加方式
建立表时添加
create table students(
id int constraint 名称 primary key ,
name varchar(20))
建立表后添加
alter table students add constraint students_pk primary key(id);

删除约束
alter table 表名 drop constraint 约束名;


not null
default 
只可以在表中字段后添加
修改和删除时使用modify

禁用约束:
alter table 表名 disable constraint 约束名;
启用约束:
alter table 表名 enable constraint 约束名;


外键:foreign key
特点:参照主键中存在的值,可以插入null 可以插入重复的值

表参照值
1 学生表
create table students(
id int ,
name varchar2(20));
insert into students values(1001,'TOM');
insert into students values(1002,'CAT');
2 成绩表
create table score(
sid int,
kid int,
score int);
insert into score values(1001,1,100);
insert into score values(1001,2,80);
insert into score values(1002,1,99);

删除主键约束
 alter table students drop constraint pk_students cascade;
删除主表
 drop table 表名 cascade constraint;
 
主键约束可以直接写primary key 因为表中只有一个主键
删除主键约束
alter table 表名 drop primary key;
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值