当前位置:网站首页>oracle -- 表操作
oracle -- 表操作
2022-06-25 21:50:00 【攀登程序猿】
语法:
CREATE TABLE schema_name.table_name (
column_1 data_type column_constraint,
column_2 data_type column_constraint,
...
table_constraint
);
案例1:直接创建表,不设置约束
CREATE TABLE tbl_students (
stu_num CHAR(10) NOT NULL,
stu_name VARCHAR2(10) NOT NULL,
stu_sex CHAR(4) NOT NULL,
stu_age NUMBER(3),
stu_tel NUMBER(11)
)
// 给学生表新增一列
ALTER TABLE tbl_students ADD stu_email VARCHAR2(20);
// 新增
ALTER TABLE tbl_students ADD stu_email VARCHAR2(20);
// 修改列,仅支持修改类型和约束
ALTER TABLE tbl_students MODIFY stu_email VARCHAR2(50)
// 删除列
ALTER TABLE tbl_students DROP COLUMN stu_email
// 删除表
DROP TABLE PERSONS;注:alter不能和本身冲突,比如性别保存男,3个字符,再修改会2个字符就会报错
> ORA-01441: cannot decrease column length because some value is too big
案例2:主键
主键 数据表中的一个或多个字段,用于唯一表示数据表中的一条数据
主键所在字段唯一且不为空,分为单列主键和多列主键
单列主键:
创建成功后,查看设计表时,可以看到键
// 在创建表时创建主键
CREATE TABLE tbl_students (
stu_num CHAR(10) primary key,
stu_name VARCHAR2(10) NOT NULL,
stu_sex CHAR(2) NOT NULL,
stu_age NUMBER(3),
stu_tel NUMBER(11)
)
CREATE TABLE tbl_students (
stu_num CHAR(10),
stu_name VARCHAR2(10) NOT NULL,
stu_sex CHAR(2) NOT NULL,
stu_age NUMBER(3),
stu_tel NUMBER(11),
primary key(stu_num)
)
// 为列添加约束
ALTER TABLE tbl_students ADD CONSTRAINTS pk_student primary key(stu_num)
注:添加主键的修改不能和本身冲突,当表stu_num未标注为not null时,如果数据没有null,也是可以添加约束的,但是如果数据有null或者重复,则会报错,造成添加失败
> ORA-01449: column contains NULL values; cannot alter to NOT NULL
ORA-02437: cannot validate (WATERBOSSFACTORY.PK_STUDENT) - primary key violated(用重复的值)
案例3:联合主键
使用两个或两个以上的字段作为主键
// 创建联合主键
CREATE TABLE tbl_grads(
course_id char(3),
course_name VARCHAR2(50) ,
stu_num CHAR(10),
stu_name VARCHAR2(10),
score NUMBER(3),
primary key (course_id,stu_num)
)
// 下面的会报错, ORA-02260: table can have only one primary key
CREATE TABLE tbl_grads(
course_id char(3) primary key,
course_name VARCHAR2(50) ,
stu_num CHAR(10),
stu_name VARCHAR2(10),
score NUMBER(3) primary key
)
ALTER TABLE tbl_grads ADD CONSTRAINTS pk_grads primary key(course_id,stu_num)联合主键仅

注:
(1)只要有一个为空或者联合主键所在的字段都重复,则插入数据或者alter添加主键就会失败
ORA-01449: column contains NULL values; cannot alter to NOT NULL
> ORA-02437: cannot validate (WATERBOSSFACTORY.PK_GRADS) - primary key violated 两字段都重复
案例4:外键约束
主外键关联,即限定外键字段的值必须来自与其它数据表中的关联字段,一般是主键

// 创建表时指定外键 如果外键关联的表不存在,则会报错 ORA-00942: table or view does not exist
CREATE TABLE tbl_students(
stu_num CHAR(10) primary key,
stu_name VARCHAR2(10) NOT NULL,
stu_sex CHAR(4) NOT NULL,
stu_age NUMBER(3),
stu_tel NUMBER(11),
stu_cid CHAR(3) not NULL,
CONSTRAINT fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade
)
// on delete cascade代表删除班级表的主键时,学生表的相关数据也会删除
// 添加外键约束
ALTER TABLE tbl_students ADD CONSTRAINTS fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade;
// 删除外键约束
ALTER TABLE TBL_STUDENTS DROP CONstraints fk_student_classes;
注:
(1)两个表的删除逻辑是必须先删除班级表,然后再删除学生表,因为学生表的外键指向了班级表的主键
(2)设置了on delete cascade后,在删除班级表中的数据时,会同时删除掉班级的学生
案例5:CHECK约束
是检查约束,用于限定每一列能够输入的值,以保证数据的正确性
// 添加check约束
CREATE TABLE tbl_students(
stu_num CHAR(10) primary key,
stu_name VARCHAR2(10) NOT NULL,
stu_sex CHAR(4) NOT NULL,
stu_age NUMBER(3),
stu_tel NUMBER(11),
stu_cid CHAR(3) not NULL,
CONSTRAINT fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade,
CONSTRAINT ck_student_sex CHECK(stu_sex='男' or stu_sex='女'),
CONSTRAINT ck_student_age CHECK(stu_age BETWEEN 6 AND 30)
)
ALTER TABLE TBL_STUDENTS DROP constraints ck_student_age;
ALTER TABLE tbl_students add constraints ck_student_age CHECK(stu_age BETWEEN 6 AND 30);添加好check约束后如下:

此时添加数据不满足约束时,会报错

案例6:UNIQUE约束
用于限定字段的唯一性,唯一键添加方式有三种,一种是在字段直接添加unique,一种是使用CONSTRANT指定,这两种的区别是直接添加unique时唯一键的名称是随机的,CONSTRANT是指定的。
// 电话是唯一的,添加唯一约束
CREATE TABLE tbl_students(
stu_num CHAR(10) primary key,
stu_name VARCHAR2(10) NOT NULL,
stu_sex CHAR(4) NOT NULL,
stu_age NUMBER(3),
stu_tel NUMBER(11) UNIQUE,
stu_cid CHAR(3) not NULL ,
CONSTRAINT fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade,
CONSTRAINT ck_student_sex CHECK(stu_sex='男' or stu_sex='女'),
CONSTRAINT ck_student_age CHECK(stu_age BETWEEN 6 AND 30)
)
CREATE TABLE tbl_students(
stu_num CHAR(10) primary key,
stu_name VARCHAR2(10) NOT NULL,
stu_sex CHAR(4) NOT NULL,
stu_age NUMBER(3),
stu_tel NUMBER(11),
stu_cid CHAR(3) not NULL ,
CONSTRAINT fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade,
CONSTRAINT uq_student_tel UNIQUE(stu_tel),
CONSTRAINT ck_student_sex CHECK(stu_sex='男' or stu_sex='女'),
CONSTRAINT ck_student_age CHECK(stu_age BETWEEN 6 AND 30)
)
ALTER TABLE TBL_STUDENTS DROP constraints uq_student_tel;添加唯一键后可以在唯一键的地方查看

如果添加重复的数据,会报错

案例7:NOT NULL
已经一直使用,不再赘述
边栏推荐
- [WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
- 腾讯《和平精英》新版本将至:新增账号安全保护系统,游戏内违规行为检测升级
- 2022-2028 global DC linear variable differential transformer (LVDT) industry survey and trend analysis report
- Why absolute positioning overlaps
- Market depth analysis and development strategy consulting report of China's fire equipment market 2022-2028
- Global and Chinese oleic acid operation mode and market supply and demand forecast report 2022 ~ 2028
- Unity技术手册 - 粒子发射和生命周期内速度子模块
- Wpewebkit debugging MSE playback
- Analysis of China's tractor manufacturing and operation situation and forecast report of prospect trend 2022-2028
- How to use the find command
猜你喜欢

腾讯《和平精英》新版本将至:新增账号安全保护系统,游戏内违规行为检测升级

Use apiccloud AVM multi terminal component to quickly realize the search function in the app
[invitation letter] on March 4, the platform enabled digital intelligence Innovation -- UFIDA BiP PAAS cloud platform IUAP digital intelligence hundred cities forum · Jinan Station

Yyds dry goods inventory CEPH installation visual dashboard

2022-2028 global carbon fiber unidirectional tape industry research and trend analysis report

Programmer weekly (issue 4): the wealth view of programmers

2022-2028 global proton exchange membrane hydrogen electrolyzer industry survey and trend analysis report

Talk about adapter mode

NARI radar's IPO meeting: it plans to raise nearly 1billion yuan. Bao Xiaojun and his wife are Canadians

Obsidian basic tutorial
随机推荐
Analysis report on market business model and development direction of China mobile operation industry from 2022 to 2028
Diagram of stack frame running process
CVPR2022教程 | 马里兰大学《机器学习遥感处理:农业与粮食安全》教程
Some reflections on preparing for the Blue Bridge Cup
Tlog helps Pangu framework realize microservice link log tracking
AbstractFactory Abstract Factory
腾讯《和平精英》新版本将至:新增账号安全保护系统,游戏内违规行为检测升级
Data governance is easier said than done
Audio orchestrator: orchestrate immersive interactive audio using multiple devices
Travel notes of 2022giao
2022-2028 global extrusion coating and lamination production line industry research and trend analysis report
Hotspot JVM "01" class loading, linking and initialization
Programmer weekly (issue 4): the wealth view of programmers
聊聊Adapter模式
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
HotSpot JVM 「01」类加载、链接和初始化
Cvpr2022 tutorial | machine learning remote sensing processing: agriculture and food security, University of Maryland
你好,请问老师,在支付宝基金开户真的安全吗?
What are the debugging methods for nodejs
Obsidian basic tutorial