MySQL table-related operations and field-related operations (examples)
1. Create a teacher table in the school database
Carry out table definition operations according to the following requirements:
(1) First create the database school.
create database if not exists school default character set utf8mb4 collate utf8mb4_general_ci;
(2) Use database school.
mysql> use school;
(3) Create a teacher table.
create table teacher(
id int not null unique auto_increment comment '编号',
num int not null unique comment '教工号',
name varchar(20) not null comment '姓名',
sex varchar(4) not null comment '性别',
birthday datetime comment '出生日期',
address varchar(50) comment '地址',
primary key(id))engine=InnoDB default charset=utf8mb4 collate =utf8mb4_general_ci;
(4) Check the built table.
mysql> show tables;
(5) View the basic structure of the table.
method one:
mysql> describe teacher;
or
mysql> desc teacher;
Method Two:
mysql> show columns from teacher;
(6) View the detailed structure of the table
mysql> show create table teacher;
(7) Change the data type of the name field of the teacher table to VARCHAR(30).
mysql> ALTER TABLE teacher modify column name varchar(30) not null;
(8) Change the position of the birthday field to behind the name field and verify the result.
mysql>alter table teacher modify column birthday datetime after name;
(9) Rename the num field to t_id and verify the result.
mysql>alter table teacher change num d_id int not null;
(10) Delete the address field of the teacher table and verify the result.
mysql> alter table teacher drop column adress;
(11) Add a field named wages to the teacher table, with the data type being FLOAT.
mysql>alter table teacher add column wages float;
(12) Change the data type of the wagers field to DECIMAL(8,2) and verify the result.
mysql>alter table teacher modify column wages DECIMAL(8,2);
(13) Rename the teacher table to teacherInfo and verify the result.
mysql>alter table teacher rename teacherInfo;
(14) Change the storage engine of the teacher table to the MyISAM type and verify the result.
mysql> alter table teacherinfo engine=MyISAM;
mysql> show table status from school where name='teacherinfo';
2. Summary
1. Table operations
-
Query all tables in the current database:
SHOW TABLES;
-
Query table structure:
DESC 表名;
show columns from 表名;
-
Query the table creation statement of the specified table:
SHOW CREATE TABLE 表名;
-
Create table:
CREATE TABLE 表名(
字段1 字段1类型 [COMMENT 字段1注释],
字段2 字段2类型 [COMMENT 字段2注释],
字段3 字段3类型 [COMMENT 字段3注释],
...
字段n 字段n类型 [COMMENT 字段n注释]
)[ COMMENT 表注释 ];
Note: There is no comma after the last field
-
Modify table name:
ALTER TABLE 表名 RENAME 新表名
-
Delete table:
DROP TABLE [IF EXISTS] 表名;
-
Drop the table and recreate it:
TRUNCATE TABLE 表名;
2. Field operations
-
Add fields:
ALTER TABLE 表名 ADD 字段名 类型(长度) [COMMENT 注释] [约束];
Example:ALTER TABLE emp ADD nickname varchar(20) COMMENT '昵称';
-
Modify data type:
ALTER TABLE 表名 MODIFY column 字段名 新数据类型(长度);
-
Modify the field name and field type:
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型(长度) [COMMENT 注释] [约束];
Example: Modify the nickname field of the emp table to username, and the type is varchar(30)
ALTER TABLE emp CHANGE nickname username varchar(30) COMMENT '昵称';
-
Delete fields:
ALTER TABLE 表名 DROP 字段名;