当前位置:网站首页>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#使用S7.net连接西门子S1200PLC,C#直接连接西门子PLC

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

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

Before we learn about high-performance computing, let's take a look at its history

SQL第四练:字符串处理函数

Abbexa 8-OHdG CLIA kit solution

Introduction to abbexa bacterial genome DNA Kit

Abbexa AML1 DNA binding ELISA Kit instructions

KDD2022 | 基于对抗性知识蒸馏的深度图神经网络压缩

php的exec函数
随机推荐
Abbexa acrylamide peg NHS instructions
datagrip 报错 “The specified database user/password combination is rejected...”的解决方法
Before we learn about high-performance computing, let's take a look at its history
AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
ICML2022 | Sharp-MAML:锐度感知的模型无关元学习
Part 7: Lesson 2 general skills of consultants - how to install and uninstall SAP ERP system client
Visio 转为高质量PDF
Latex error: file ‘xxx.sty‘ not found
Array remove duplicates from an array
SQL Server查询区分大小写
C语言qsort()函数的使用(详解)
笔记(四)- 多线程
Are you still writing the TS type code
Understanding of related concepts of target detection
2022 - 06 - 09 rk817 PMU Battery Temperature Detection
(十一)TableView
C程序实例1--个人通讯录管理系统
Array move 0
C#使用S7.net连接西门子S1200PLC,C#直接连接西门子PLC