当前位置:网站首页>Add, delete, query and modify MySQL table structure (DDL)
Add, delete, query and modify MySQL table structure (DDL)
2022-06-10 22:23:00 【yuelinghou】
List of articles
One . Create table
grammar :
create table if not exists Table name (
field1 datatype,
field2 datatype,
field3 datatype
) charset Character set collate Validation rules engine Storage engine ;
explain :
- field Represents the property name .
- datatype Represents the type of the property .
- Last character set 、 The validation rule and storage engine may not specify , In this case, the data in the database shall prevail .
- You can also add... After the data type of the field
comment ' Description information ', Information used to describe this field , In this way desc It is more readable when viewing the table structure .
give an example :
1、 First create a database , It is found that there is only one data directory named db.opt Library file :
2、 Access to database , Create a page in the library called user_1 Table of , The storage engine uses MyISAM, It is found that there are three more in the data directory of the library Table related files :
PS:user_1 The storage engine of the table is MyISAM , There are three different files in the data entry , Their meanings are as follows :
- user_1.frm: Table structure .
- user_1.MYD: Table data .
- user_1.MYI: Table index .
3、 Different storage engines , The file that creates the table is different . The next step in creating an engine is innodb Table of user_2:
Continue to observe the data directory of the database , Discover to store the engine innodb Created user_2 Table produces two files :
- user_2.frm: Table structure .
- user_2.ibd: Table data and index .
Two . Delete table
grammar :
drop table Table name ;
give an example :
mysql> show tables;
+-------------------+
| Tables_in_ForTest |
+-------------------+
| user_1 |
| user_2 |
+-------------------+
2 rows in set (0.00 sec)
mysql> drop table user_1;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_ForTest |
+-------------------+
| user_2 |
+-------------------+
1 row in set (0.00 sec)
3、 ... and . See the table
1、 Input show tables; To view tables created in a database . Remember table To add s:
mysql> use ForTest;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_ForTest |
+-------------------+
| user_1 |
| user_2 |
+-------------------+
2 rows in set (0.00 sec)
2、 Input desc Table name ; View the structure of a table :
mysql> desc user_1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.01 sec)
Table structure description :
3、 Input show create table Table name ; You can view the information when the table is created :
PS: If this horizontal layout is inconvenient to see , You can also use \G Replace the last semicolon with a more concise typesetting output :
Four . Modify table
1、 Modify the name of the entire table
grammar :alter table Old name of table rename to New name ; among to You can omit it .
give an example : First create a page called employee Table of , Then change the table name to users:
mysql> create table if not exists employee(
-> ame varchar(32),
-> age smallint
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
+-------------------+
| Tables_in_ForTest |
+-------------------+
| employee |
+-------------------+
1 row in set (0.00 sec)
mysql> alter table employee rename to users;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+-------------------+
| Tables_in_ForTest |
+-------------------+
| users |
+-------------------+
2、 Modify the field name of the table
grammar :alter table Table name change Old field name New field name + type ;
Be careful : The new field needs to be fully defined , Remember to add its type .
give an example :
mysql> desc users;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ame | varchar(32) | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> alter table users change ame name varchar(50);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc users;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
3、 Modify the field type of the table
grammar :alter table Table name modify Original field name + Modified type
give an example :
modify age The type of field is int:
mysql> desc users;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> alter table users modify age int;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc users;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
4、 Add a field to the table
grammar :alter table Table name add Field name + type ;
give an example :
Add a new... In the table gender Field , Used to identify the gender of the user :
mysql> desc users;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> alter table users add gender varchar(10);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc users;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
Add :
The newly inserted fields are arranged to the last position of the table by default , If you want to specify the row after a target field , When creating, you can add... At the end after Target field :
5、 Delete a field in the table
grammar :alter table Table name drop Name of the field to be deleted ;
give an example :
Delete... From the table gender Field :
mysql> desc users;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table users drop gender;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc users;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
边栏推荐
- [C] overide can override the virtual method inherited from a higher parent
- C语言-排序中的快速排序(简称快排)
- [1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
- 系统重装以及查询系统性能
- 数组 只出现一次的数字
- Icml2022 | sharp maml: model independent meta learning for sharpness perception
- 记录(二)
- Bitwise and shift operators
- Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
- Visio 转为高质量PDF
猜你喜欢

【phpstorm】 No data sources are configured to run this SQL and provide advanced c

php的exec函数

Icml2022 | sharp maml: model independent meta learning for sharpness perception

C language - quick sorting in sorting
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)

Forward slash "/", backslash "\," escape character "\" and file path separator cannot be clearly remembered

【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas

SoC development environment and hardware development preparation

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

数组 求并集
随机推荐
Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)
【MySQL】錶數據的增删查改(DML)
OC swift hybrid
Error parsing mapper XML
(11) Tableview
Modify frontsortinglayer variable of spritemask
ICML2022 | Sharp-MAML:锐度感知的模型无关元学习
How to view the occupied space of a table in MySQL database
If else is too easy to use? (understanding this article will further improve your logic)
[nk] 牛客月赛51 F-平均题
数组 加一
记录(三)
Cordova plugin /jpush phonegap Aurora push_ Local push_ Message push
Ajout, suppression et modification des données du tableau [MySQL] (DML)
【phpstorm】 No data sources are configured to run this SQL and provide advanced c
Sealem Finance打造Web3去中心化金融平台基础设施
[debug] could not find ref wiht POC XXX
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
How to realize the marketing purpose of small program integral mall