当前位置:网站首页>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

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 : Insert picture description here

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
 Insert picture description here
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:
 Insert picture description here
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 :
 Insert picture description here

3、 Input show create table Table name ; You can view the information when the table is created :
 Insert picture description here
PS: If this horizontal layout is inconvenient to see , You can also use \G Replace the last semicolon with a more concise typesetting output :
 Insert picture description here

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
 Insert picture description here

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)
原网站

版权声明
本文为[yuelinghou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102045523530.html