MySQL series-DDL statement
Operation and Maintenance YouthO&M Youth
Series of articles description
MySQL series articles include software installation, specific use, backup and recovery, etc., which are mainly used to record personal study notes. The main MySQL version used is 5.7.28, and the server system version is CentOS 7.5. This chapter is for database DDL statements.
DDL syntax
DDL (Data Definition Language) is mainly used for database and table management. The main syntax is drop, alter, and create.
DDL CREATE
create is mainly used to create a database or table.
- Create database
syntax: create database database name charset character set;
create database yunweidb charset utf8mb4;
- View all databases
show databases;
- View create database statement
show create database yunweidb;
- Create table
syntax: create table database. Table name (column name 1 data type, column name 2 data type...) engine=engine charset=charset;
create table yunweidb.t1(id int,sname varchar(20)) engine=innodb charset='utf8mb4';
- View table structure
desc yunweidb.t1;
- View all tables in the database
use yunweidb;
show tables;
- View table statement
show create table t1;
DDL DROP
DROP is mainly used to delete a database or table.
- Delete database
syntax: drop database database name;
drop database yunweidb;
- Delete table
syntax: drop table table name;
drop table t1;
DDL alter
alter is mainly used to modify databases and tables.
MySQL中,DDL语句对表进行创建、删除、修改表等DDL操作时,是需要锁元数据表的,锁定时,所有对该表修改类的命令都无法正常运行,所以在对于大表、业务较繁忙的表进行线上DDL操作时,要谨慎。
- Modify the database
Syntax: alter database database table name attribute;
alter database yunweidb charset utf8;
-
View table structure
- Add column
Syntax: alter table table name add column column name data type attribute;
alter table t1 add column phone char(11);
-
View table structure
- Modify column
Syntax: alter table table name modify column name attribute;
alter table t1 modify phone bigint;
- View table structure
注意
修改列时,应该把原先的属性加上,以免属性丢失
- Delete column
Syntax: alter table table name drop column name;
alter table t1 drop sname;
- View structure