oracle 对两列加唯一性束_oracle2约束添加&表复制&拼接

本文主要介绍了Oracle数据库中表约束的设置,包括添加主键、唯一约束、外键约束,以及修改和删除约束的方法。还涉及表结构和数据的复制操作,如复制表结构、复制全部或部分数据。此外,讲解了查询空值和非空值的语句,以及日期与数值的运算规则和四种约束类型。

alter table student3 add constraint uq_name unique(name)

-- 添加主键,约束名可省

alter table student3 add constraint pk_id primary key(id) -- constraint pk_id 可省略

create table Class(

id number(4) constraint pk_id primary key, -- 列级约束,

name varchar2(20),

sex varchar2(2)

);

alter table Class add constraint uq_nam unique(name);

alter table Class modify sex default '男' not null; -- default '男' 及 not null只能用modify添加,(修改),且default在not null前

create table student2(

id number(4),

constraint pk_id2 primary key(id), -- 表级约束

name varchar2(20) not null,

cid number constraint pk_cid references Class(id) -- 列级约束(外键) not null只能用列级约束

);

alter table student2 drop constraint pk_cid; -- 修改表丢掉约束drop constraint 约束名

alter table student2 drop primary key; -- 丢掉主键可以直接drop primary key,不用写主键名,一个表只有一个主键

create table student4 as select * from student3 where 1=2 -- 复制表结构

create table student4 as select * from student3 -- 复制表结构同时复制全部数据

insert into student4 select * from student3 where id=1 -- 复制部分数据

select * from student3 where birthday is null -- 查询为空用的是 字段名 is null 而不是 =null

select * from student3 where birthday is not null -- 查询不为空用的是 字段名 is not null

-- 拼接, ||相当于java里的拼接符+

select '编号'||id,name from student3

select '编号'||id,name||'姓名' from student3

select '编号'||id||name||'姓名' from student3

-- 日期与数值运算的是天数

日期 – 数字 = 日期

日期 + 数字 = 日期

日期 – 日期 = 数字

-- 四种约束类型:

-- 实体完整性(主键约束),域完整性(限制数据类型,检查链接,唯一约束,非空约束),

-- 引用完整性(外键链接),自定义完整性(规则,储存过程,触发器)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值