当前位置:网站首页>SQL statement - DDL
SQL statement - DDL
2022-04-23 02:57:00 【Senior agent of Sarkozy】
Learn from :b standing Luo Hao jackfrued The teacher's online class
SQL sentence | effect | Examples of use |
---|---|---|
create | Create database / surface | create database ` Database name `; / create table ` Table name ` ( Field description ); |
drop | Delete database / surface | drop database ` Database name `; / drop table ` Table name `; |
use | Select database | use ` Database name `; |
alter | Change database / Table information | alter table ` Table name ` add constraint ` Constraint name ` unique (` Field name `); |
PS:
-
Modify the engine of the table / Changing the initial value of the self increasing constraint is in create table ` Table name ` () Added later .
create table ` Table name `( )engine = innodb auto_increment=2 comment ' Example ';
constraint
Primary key constraint
primary key (` Name `),
Compound primary keys are syntactically correct , But it's very difficult to use when developing , At least two fields are required to uniquely identify a record . Generally not used .
If you choose a timetable , There are course number and student number fields , Combined as a composite primary key, records can be uniquely determined . But usually a new elective course will be created id For example, you can uniquely determine the fields of all course selection records as the primary key .
Self increasing constraint 、 Non empty constraint
Just write after the field description
`col_name` int auto_increment not null comment ' Name ',
The following three can be written when creating a table , You can also write later alter table Table name add constraint...
.
Unique constraint
constraint `uk_col_name` unique (`col_name`),
Similar to primary key constraint , A unique constraint can also set multiple fields , Just write a few more fields in parentheses ( Such as course selection record , A student can't choose courses repeatedly , Student number and course number cannot be repeated )
Check constraint
Checking constraints can help check the validity of data , But relative , There must be a loss of performance .
constraint `ck_col_sex` check (`col_sex`='M' or `col_sex`='F'),
Foreign key constraints
First, add the same format as the primary key of another table to the table that needs to be added with a foreign key ( The name can be different ) Field of .
The performance of foreign keys will also be lost , To check whether the corresponding field in the corresponding table corresponds to . Many companies don't use foreign key constraints , Other methods will be used to determine whether the specific fields of the two tables correspond to .
constraint `fk_col_id` foreign key (` Field names in this table `) references ` Another table name ` (` The primary key name in this table `),
One to many hours , More than one side needs to add foreign key constraints .
版权声明
本文为[Senior agent of Sarkozy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220647385026.html
边栏推荐
- JS relearning
- It turns out that PID was born in the struggle between Lao wangtou and Lao sky
- The problem of removing spaces from strings
- Typescript Learning Guide
- Modify the content of MySQL + PHP drop-down box
- Win view port occupation command line
- Decision tree principle of machine learning
- The express project changes the jade template to art template
- Regular object type conversion tool - Common DOM class
- MySQL复杂查询使用临时表/with as(类似表变量)
猜你喜欢
Q-Learning & Sarsa
Introduction to ACM [TSP problem]
Practice of industrial defect detection project (III) -- Based on FPN_ PCB defect detection of tensorflow
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
Encapsulation of ele table
Huawei machine test question -- deformation of hj53 Yang Hui triangle
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
【Hcip】OSPF常用的6种LSA详解
工业互联网+危化安全生产综合管理平台怎样建
JS learning notes
随机推荐
Mosaic Routing: implement / home / news
Probabilistic model of machine learning
《信息系统项目管理师总结》第六章 项目人力资源管理
Winsock programming interface experiment: implementation of ipconfig
Wepy learning record
Opencv reads webcam video and saves it locally
JSON data text
Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl
Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
Modify the content of MySQL + PHP drop-down box
Typescript Learning Guide
The problem of removing spaces from strings
Shell script learning -- practical case
Jz76 delete duplicate nodes in linked list
Niuke white moon race 5 [problem solving mathematics field]
Niuke white moon race 6 [solution]
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
JS learning notes
Classification of technology selection (2022)