Article Directory
Preface
The data definition language mainly includes the management of libraries and tables.
1. Library management
Create, modify, delete
2. Table management
Create, modify, delete
Keywords
Create: create
modify: alter
delete: drop
here is to perform the definition operation with the previous data, distinguish between delete and truncate
1. Library management
1. The creation of the library
grammar:
create database 库名;
Case: Creating Books
CREATE DATABASE books;
#CREATE DATABASE if not exists books;如果不存在,就创建
2. Modification of the library
RENAME DATABASE books TO 新库名;
You can change the character set of the library:
ALTER DATABASE books CHARACTER SET gbk;
3. Deletion of the library
DROP DATABASE books;
#DROP DATABASE IF EXISTS books;如果存在就删除
Second, the management of the table
1. Table creation
grammar:
create table 表名(
列名 列的类型【(长度) 约束】,
列名 列的类型【(长度) 约束】,
列名 列的类型【(长度) 约束】,
...
列名 列的类型【(长度) 约束】
)
Case 1: Create a table Books
USE books ;
CREATE TABLE book (
id INT,#编号
bName VARCHAR (20),#图书名
price DOUBLE,#价格
authorId INT,#作者编号
publishDate DATETIME#出版日期
) ;
DESC book ;
Result:
Case 2: Create table author
CREATE TABLE author (
id INT,
au_name VARCHAR (20),
nathion VARCHAR (10)
) ;
DESC author;
result:
2. Modification of the table
Core syntax:
alter table 表名 add(添加)|drop(删除)|modify(修改)|change(改变) column 列名 【列类型 约束】;
①Modify column name
ALTER TABLE book
CHANGE COLUMN publishdate pubDate DATETIME ;
②Modify the type or constraint of the column
ALTER TABLE book
MODIFY COLUMN pubDate TIMESTAMP ;
③Add a new column
ALTER TABLE author
ADD COLUMN annual DOUBLE ;
④Delete column
ALTER TABLE author
DROP COLUMN annual;
⑤ Modify the table name
ALTER TABLE author
RENAME TO book_author ;
3. The deletion of the table
DROP TABLE book_author;
DROP TABLE IF EXISTS book_author;
SHOW TABLES;
General writing:
DROP DATABASE IF EXISTS 旧库名;
CREATE DATABASE 新库名;
DROP TABLE IF EXISTS 旧表名;
CREATE TABLE 表名();
4. Copy of the table
(1) Only copy the structure of the table
CREATE TABLE copy LIKE author ;
(2) Copy table structure + data (all)
CREATE TABLE copy2
SELECT
*
FROM
author ;
(3) Copy only part of the data
CREATE TABLE copy3
SELECT
id,
au_name
FROM
author
WHERE nation='中国' ;
(4) Only copy certain fields (as long as the column structure, no data)
CREATE TABLE copy4
SELECT
id,
au_name
FROM
author
WHERE 1 = 2 ;
test
1. Create table deptl
name | Null | type |
---|---|---|
id | int(7) | |
name | varchar(25) |
USE test ;#创建表首先要找一个库
CREATE TABLE dept1 (id INT (7), NAME VARCHAR (25)) ;
2. Insert the data in the departments table into the new table dept2
CREATE TABLE dept2
SELECT
*
FROM
myemployees.departments ;
3. Create table emp5
name | Null | type |
---|---|---|
id | not null | int(7) |
first_name | varchar(25) | |
last_name | varchar(25) | |
dept_id | not null | int(7) |
CREATE TABLE emp5(
id INT(7) NOT NULL,
first_name VARCHAR(25),
last_name VARCHAR(25),
dept_id INT(7) NOT NULL
);
4. Increase the length of the column Last_name to 50
ALTER TABLE emp5
MODIFY COLUMN last_name VARCHAR (50) ;
5. Create employees2 based on the table employees
CREATE TABLE employees2 LIKE myemployees.employees ;
6. Delete table emp5
DROP TABLE IF EXISTS emp5;
7. Rename the table employees2 to emp5
ALTER TABLE employees2
RENAME TO emp5 ;
8. Add a new column test_column in the table emp5, and check the operation
ALTER TABLE emp5
ADD COLUMN test_column VARCHAR (10) ;
DESC emp5 ;
9. Directly delete the column dept_id in the table emp5
ALTER TABLE emp5
DROP COLUMN dept_id ;