DDL operation database
CRUD (addition, deletion, modification and check)
C (Create)-create a database
1. Create a database
CREATE DATABASE 数据库名;
Found that the database db1
has been created
2. Determine whether the database already exists, create a database if it does not exist
CREATE DATABASE IF NOT EXISTS 数据库名;
First, we use the first method to create another db1
database.
An error occurs Can't create database 'db1'; database exists
,
and then use the second method to create it.
Finally, use the second method to create it.db2
3. Create a database and specify the character set
CREATE DATABASE 数据库名 CHARACTER SET 字符集;
Specify the character set encoding to gbk
createdb3
R (Retrieve)-View the database
1. View all databases
SHOW DATABASES;
2. View the creation information of a specified database
SHOW CREATE DATABASES 指定数据库名称;
U (Update)-modify the name of the database, character set
ALTER DATABASE 数据库名 DEFAULT CHARACTER SET 字符集;
The modified db3
character set is determined by gbk
->utf-8
D (Delete)-delete the database
1. Delete the database
DROP DATABASE 数据库名;
2. Determine whether the database already exists, delete the database if it exists
DROP DATABASE IF EXISTS 数据库名;
The deletion process is the same as above
Use && to switch database
1. View the database in use
SELECT DATABASE();
2. Use/Switch Database
USE 数据库名;
DDL operation table
C (Create)-create a table
CREATE TABLE 表名 (
字段名 1 字段类型 1,
字段名 2 字段类型 2
);
Create a table student in the db1 database to
display the information in the student table
R (Retrieve)-query table
1. Query a table in a database
SHOW TABLES;
Query db1
query mysql
2. Query table structure
DESC 表名;
Query proc table
Query host table
D (Delete)-delete table
First copy the same table stu with student
1. Delete the table
DROP TABLE 表名;
2. Determine whether the table already exists, delete the table if it exists
DROP TABLE IF EXISTS 表名;
U (Update)-modify the table
1. Modify the table name
ALTER TABLE 表名 RENAME TO 新表名;
Rename the table student to stu
2. Modify the character set of the table
ALTER TABLE 表名 character set 字符集;
3. Add a column
ALTER TABLE 表名 ADD 列名 类型;
Add a column of gender in stu
4. Modify the name and type of the column
ALTER TABLE 表名 CHANGE 旧列名 新列名 类型;
Modify the gender name and type in the stu table
5. Only modify the column type
ALTER TABLE 表名 MODIFY 列名 新的类型;