/*
1:约束:constructs:::
定义表时给列的数据定义的值规则
1.1 非空:not null
1.2 唯一:unique
1.3 默认约束:default
1.4 检查约束:check
1.5 主键约束:primary key:特点:非空+唯一+可以被引用
1.6 外键约束:foreign key:特点:可以为空+可以重复+如果有值 此值必须在主表中的主键列中存在
*/
create table tab_23(
id1 int not null,
id2 int unique,
id3 varchar(10) default '呵呵',
age int check(age>0 and age< 100) /*检查约束是写法1*/
);
drop table tab_23;
create table tab_23(
id1 int not null,
id2 int unique,
id3 varchar(10) default '呵呵',
age int,
check(age>0 and age< 100) /*检查约束是写法2*/
);
insert into tab_23 values(null,1,null,11);-- 报错:id1 非空约束
insert into tab_23 values(1,11,null,111);-- 报错:id4 违反检查约束
insert into tab_23 values(1,11,null,10);
-- 只有不指定id3的值时 id3才是有默认值呵呵
insert into tab_23(id1,id2,age) values(1,12,10);
select * from tab_23;
-- 查询表结构
select * from user_tab_columns where TABLE_NAME='TAB_23';
-- 查询表约束
select * from user_constraints where TABLE_NAME='TAB_23';
-- 创建表时设置主键约束方式1
drop table tab_23;
create table tab_23(
id1 int primary key,
name varchar(100)
);
-- 创建表时设置主键约束方式2
create table tab_23(
id1 int,
primary key(id1),
name varchar(100)
);
-- 创建表后添加主键约束方式1
create table tab_23(
id1 int,
name varchar(100)
);
alter table tab_23 add primary key(id1);
-- 创建表后添加主键约束方式2
alter table tab_23 modify id1 int primary key;
-- 删除主键约束
alter table tab_23 drop primary key;
select * from user_constraints where TABLE_NAME='TAB_24';
drop table tab_24;
--创建表时设置外键约束方式1:
create table tab_24(
id11 int,
name varchar(100),
constraint fk_1 foreign key(id11) references tab_23(id1)
);
--创建表时设置外键约束方式2:
create table tab_24(
id11 int references tab_23(id1),
name varchar(100)
);
-- 删除外键约束
alter table tab_24 drop constraint SYS_C0011066;
--创建表后添加外键约束方式1
alter table tab_24 add foreign key(id11) references tab_23(id1);
--创建表后添加外键约束方式2
alter table tab_24 modify id11 int references tab_23(id1);
/*对表的ddl:*/
create table tab_25(
id int,
name varchar(100),
age int,
sex char(3)
);
select * from user_tab_columns where TABLE_NAME='TAB_25';
-- 修改表名
alter table tab_25 rename to tab25;
select * from tabs where TABLE_NAME='TAB_25';
select * from tabs where TABLE_NAME='TAB25';
-- 删除表
drop table tab25;
-- 添加列
alter table tab_25 add address varchar(100);
-- 修改列名
alter table tab_25 rename column address to myAddress ;
-- 删除列
alter table tab_25 drop column address;
-- 修改列类型
alter table tab_25 modify age float;
-- 作业:多表复习
-- 作业:oracle所有内容 文档写下来
oracle05_oracle的约束,constraint(跟着宝哥学java:oracle系列:全网最全):约束constraint
于 2023-03-31 08:57:41 首次发布