当前位置:网站首页>Oracle -- table operation
Oracle -- table operation
2022-06-25 23:04:00 【Climbing procedural ape】
grammar :
CREATE TABLE schema_name.table_name (
column_1 data_type column_constraint,
column_2 data_type column_constraint,
...
table_constraint
);
Case study 1: Create tables directly , Do not set constraints
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)
)
// Add a new column to the student table
ALTER TABLE tbl_students ADD stu_email VARCHAR2(20);
// newly added
ALTER TABLE tbl_students ADD stu_email VARCHAR2(20);
// Modify the column , Only modification of types and constraints is supported
ALTER TABLE tbl_students MODIFY stu_email VARCHAR2(50)
// Delete column
ALTER TABLE tbl_students DROP COLUMN stu_email
// Delete table
DROP TABLE PERSONS;notes :alter Cannot conflict with itself , For example, gender preservation men ,3 Characters , It will be revised again 2 One character will report an error
> ORA-01441: cannot decrease column length because some value is too big
Case study 2: Primary key
Primary key One or more fields in a data table , Used to uniquely represent a piece of data in a data table
The field of the primary key is unique and not empty , It is divided into single column primary keys and multi column primary keys
Single column primary key :
Once created , When viewing the design table , You can see the key
// Create a primary key when creating a table
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)
)
// Add constraints to columns
ALTER TABLE tbl_students ADD CONSTRAINTS pk_student primary key(stu_num)
notes : The modification of adding a primary key cannot conflict with itself , Watch stu_num Not marked as not null when , If the data doesn't null, You can also add constraints , But if the data has null Or repeat , May be an error , Cause adding failure
> ORA-01449: column contains NULL values; cannot alter to NOT NULL
ORA-02437: cannot validate (WATERBOSSFACTORY.PK_STUDENT) - primary key violated( Use duplicate values )
Case study 3: Combined the primary key
Use two or more fields as primary keys
// Create a federated primary key
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)
)
// The following will report an error , 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)The federated primary key only

notes :
(1) As long as there is an empty field or the field where the union primary key is located is repeated , Insert data or alter Adding a primary key will fail
ORA-01449: column contains NULL values; cannot alter to NOT NULL
> ORA-02437: cannot validate (WATERBOSSFACTORY.PK_GRADS) - primary key violated Both fields repeat
Case study 4: Foreign key constraints
Primary foreign key Association , That is, the value of the restricted foreign key field must come from the associated field in other data tables , It's usually a primary key

// Specify foreign keys when creating tables If the table associated with the foreign key does not exist , May be an error 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 When deleting the primary key of the class table , The relevant data in the student table will also be deleted
// Add a foreign key constraint
ALTER TABLE tbl_students ADD CONSTRAINTS fk_student_classes foreign key (stu_cid) references tbl_classes(class_id) on DELETE cascade;
// Delete foreign key constraint
ALTER TABLE TBL_STUDENTS DROP CONstraints fk_student_classes;
notes :
(1) The deletion logic of the two tables is that the class table must be deleted first , Then delete the student table , Because the foreign key of the student table points to the primary key of the class table
(2) Set up on delete cascade after , When deleting the data in the class table , Students in the class will be deleted at the same time
Case study 5:CHECK constraint
Check constraints , Used to limit the values that can be entered in each column , To ensure the correctness of the data
// add to check constraint
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=' male ' or stu_sex=' Woman '),
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);Add good check The constraint is followed by :

When adding data does not satisfy the constraint , Will report a mistake

Case study 6:UNIQUE constraint
Used to limit the uniqueness of a field , There are three ways to add unique keys , One is to add directly in the field unique, One is to use CONSTRANT Appoint , The difference between the two is to add unique The name of the only key is random ,CONSTRANT Is specified .
// The telephone is the only , Add unique constraints
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=' male ' or stu_sex=' Woman '),
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=' male ' or stu_sex=' Woman '),
CONSTRAINT ck_student_age CHECK(stu_age BETWEEN 6 AND 30)
)
ALTER TABLE TBL_STUDENTS DROP constraints uq_student_tel;After adding a unique key, you can view it in the only key

If you add duplicate data , Will report a mistake

Case study 7:NOT NULL
Has been used , I won't repeat
边栏推荐
- 2022-2028 global industrial touch screen industry research and trend analysis report
- Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
- How to use JMeter for interface testing
- Chapter 3 use of requests Library
- Unity technical manual - particle foundation main module attributes - upper
- 等价类,边界值,场景法的使用方法和运用场景
- 2022-2028 global carbon fiber unidirectional tape industry research and trend analysis report
- 最近准备翻译外国优质文章
- 再突破!阿里云进入Gartner云AI开发者服务挑战者象限
- ES6 const constants and array deconstruction
猜你喜欢

如何用jmeter做接口测试

1281_FreeRTOS_vTaskDelayUntil实现分析

2022-2028 global horizontal reciprocating compressor industry research and trend analysis report
![Intimacy - [comfortable exit] - final communication to reduce injury](/img/ab/c02d3b4213e1706e379d26073cbf07.jpg)
Intimacy - [comfortable exit] - final communication to reduce injury

ES6学习-- LET

哪些PHP开源作品值得关注
![[eosio] eos/wax signature error is_ Canonical (c): signature is not canonical](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[eosio] eos/wax signature error is_ Canonical (c): signature is not canonical

Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology

再突破!阿里云进入Gartner云AI开发者服务挑战者象限

Openwrt (VIII) application layer development
随机推荐
哪些PHP开源作品值得关注
2022-2028 global TFT touch screen industry research and trend analysis report
Some reflections on preparing for the Blue Bridge Cup
Initialization process of gstlibav
Wpewebkit debugging MSE playback
Network security project questions of the first Henan vocational skills competition in 2022
How to open a futures account safely at present? Which futures companies are more reliable?
Jz-064- maximum value of sliding window
2022-2028 global web and browser isolation platform industry research and trend analysis report
民航局:到 2025 年我国将初步建成安全、智慧、高效和绿色的航空物流体系
字符串变形(字符串大小写切换和变现)
2022年河南省第一届职业技能大赛网络安全项目试题
APP测试要点
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
Chapter 3 use of requests Library
2022-2028 global open source cloud storage industry research and trend analysis report
C language and the creation and use of database
Es6-- set
Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?