mysql Learn the basic operation of the table of notes

  • 2021-07-06 12:00:05
  • OfStack

Create a table

create table table name

create table if not exists 表名


mysql> create database company;
Query OK, 1 row affected (0.00 sec)
mysql> use company;
Database changed
mysql> create table if not exists t_dept(
  -> deptno int,
  -> dname varchar(20),
  -> loc varchar(40));
Query OK, 0 rows affected (0.20 sec)
mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| t_dept      |
+-------------------+
1 row in set (0.00 sec)
mysql>

Displays all tables under the current library

show tables;


mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| t_dept      |
+-------------------+
1 row in set (0.00 sec)

View the structure of the table

describe 表名

Abbreviation

desc table name


mysql> describe t_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)   | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> desc t_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)   | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

View the details of the table

show create table 表名


mysql> show create table t_dept;
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                            |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_dept | CREATE TABLE `t_dept` (
 `deptno` int(11) DEFAULT NULL,
 `dname` varchar(20) DEFAULT NULL,
 `loc` varchar(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
show create table t_dept \G

mysql> show create table t_dept \G
*************************** 1. row ***************************
    Table: t_dept
Create Table: CREATE TABLE `t_dept` (
 `deptno` int(11) DEFAULT NULL,
 `dname` varchar(20) DEFAULT NULL,
 `loc` varchar(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

Delete table

drop table table name
drop table if exists 表名


mysql> drop table if exists t_dept;
Query OK, 0 rows affected (0.12 sec)
mysql> show tables;
Empty set (0.00 sec)

Modify table name

ALTER TABLE old_table_name RENAME [TO] new_table_name
old_table_name Original table name
new_table_name New Table Name
Modify t_dept to tab_dept


mysql> alter table t_dept rename tab_dept;
Query OK, 0 rows affected (0.09 sec)
mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| tab_dept     |
+-------------------+
1 row in set (0.00 sec)
mysql> desc tab_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)   | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

Adding 1 field to the table defaults to the last
ALTER TABLE table_name ADD 属性名 属性类型

Add 1 field descri varchar for tab_dept (20)


mysql> desc tab_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)   | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table tab_dept add descri varchar(20);
Query OK, 0 rows affected (0.33 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tab_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)   | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
| descri | varchar(20) | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Add 1 field to the first position of the table

ALTER TABLE table_name ADD 属性名 属性类型 first


mysql> alter table tab_dept add id int first;
Query OK, 0 rows affected (0.38 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tab_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id   | int(11)   | YES |   | NULL  |    |
| deptno | int(11)   | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
| descri | varchar(20) | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Adds a field after a specified field in a table

ALTER TABLE table_name ADD Attribute Name Attribute Type AFTER Attribute Name


mysql> alter table tab_dept add comm varchar(20) after dname;
Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tab_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id   | int(11)   | YES |   | NULL  |    |
| deptno | int(11)   | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| comm  | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
| descri | varchar(20) | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

Delete a field

ALTER TABLE table_name DROP Attribute Name


mysql> alter table tab_dept drop comm;
Query OK, 0 rows affected (0.32 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tab_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id   | int(11)   | YES |   | NULL  |    |
| deptno | int(11)   | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
| descri | varchar(20) | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Field Modification-Modify the field data type
ALTER TABLE table_name MODIFY Attribute Name Data Type


mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| t_dept      |
+-------------------+
1 row in set (0.00 sec)
0

Field Modification-Modify the field name

ALTER TABLE table_name CHANGE Old Attribute Name New Attribute Name Old Data Type


mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| t_dept      |
+-------------------+
1 row in set (0.00 sec)
1

Field Modification-Modify both the field name and the data type

ALTER TABLE table_name CHANGE Old Attribute Name New Attribute Name New Data Type


mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| t_dept      |
+-------------------+
1 row in set (0.00 sec)
2

Modification order

ALTER TABLE table_name MODIFY 属性名1 数据类型 FIRST|AFTER 属性名2

2 attributes must exist
Move deptno to Position 1


mysql> alter table tab_dept modify deptno int first;
Query OK, 0 rows affected (0.33 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tab_dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11)   | YES |   | NULL  |    |
| id   | varchar(32) | YES |   | NULL  |    |
| dname | varchar(20) | YES |   | NULL  |    |
| loc  | varchar(40) | YES |   | NULL  |    |
| descri | int(11)   | YES |   | NULL  |    |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Put ID at the end


mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| t_dept      |
+-------------------+
1 row in set (0.00 sec)
4


Related articles: