SQL-DDL (operations on libraries and tables)

DDL operation database

CRUD (addition, deletion, modification and check)

C (Create)-create a database

1. Create a database

CREATE DATABASE 数据库名;

Create database db1
Found that the database db1has been created
Found already joined

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 db1database.
Create db1 again
An error occurs Can't create database 'db1'; database exists,
and then use the second method to create it.
Create without error
Finally, use the second method to create it.db2
Create db2

3. Create a database and specify the character set

CREATE DATABASE 数据库名 CHARACTER SET 字符集;

Specify the character set encoding to gbkcreatedb3
Create db3

R (Retrieve)-View the database

1. View all databases

SHOW DATABASES;

View all databases

2. View the creation information of a specified database

SHOW CREATE DATABASES 指定数据库名称;

Query the definition information of a certain database

U (Update)-modify the name of the database, character set

ALTER DATABASE 数据库名 DEFAULT CHARACTER SET 字符集;

The modified db3character set is determined by gbk->utf-8
Modify the db3 character set

D (Delete)-delete the database

1. Delete the database

DROP DATABASE 数据库名;

Delete the db3 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(); 

View the database in use

2. Use/Switch Database

USE 数据库名;

Switch database to db1

DDL operation table

C (Create)-create a table

CREATE TABLE 表名 (
字段名 1 字段类型 1,
字段名 2 字段类型 2
);

Create a table student in the db1 database to
Create student
display the information in the student table
Information in the student table

R (Retrieve)-query table

1. Query a table in a database

SHOW TABLES;

Query db1
Query db1
query mysql
Query mysql

2. Query table structure

DESC 表名;

Query proc table
Query the proc table
Query host table
Query the host table

D (Delete)-delete table

First copy the same table stu with student
The same table stu as student

1. Delete the table

DROP TABLE 表名;

Delete stu 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
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
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
Change the gender name and type in the stu table

5. Only modify the column type

ALTER TABLE 表名 MODIFY 列名 新的类型;

Modify the sex type in the stu table

Guess you like

Origin blog.csdn.net/weixin_45966880/article/details/114264923