当前位置:网站首页>DDL language of MySQL database: create, modify alter, delete drop of databases and tables
DDL language of MySQL database: create, modify alter, delete drop of databases and tables
2022-07-04 10:31:00 【Big bear loves to work】
MySQL Database DDL Language : Creation of libraries and tables 、 modify 、 Delete
1、DDL Data definition language
Library and table management
- Library management :
establish、modify、Delete - The management of the table :
establish、modify、Delete
- Library management :
The main keywords used
- establish :
create - modify :
alter - Delete :
drop
- establish :
Be careful: Delete here , Is to delete the entire table , The table will not exist after deletion ; and delete and truncate It refers to the operation of data , After deleting , Just the data was deleted , But the table still exists
2、 Library management
2.1 Library creation
CREATE DATABASE books;
CREATE DATABASE IF NOT EXISTS books;
2.2 Modification of the library ( Not much can be modified , Such as modifying the character set )
ALTER DATABASE books CHARACTER SET gbk;
2.3 Deletion of Library
DROP DATABASE IF NOT EXISTS books;
3、 The management of the table
3.1 The creation of a table
- grammar :
create table Table name (
Name The data type of the column [( length ) constraint ],
Name The data type of the column [( length ) constraint ],
....
Name The data type of the column [( length ) constraint ]
);
Case study 1: stay books Create tables in the database book
USE books;
CREATE TABLE book(
id INT,
bName VARCHAR(20),
price DOUBLE,
autherId INT,
publishDate DATETIME
);
DESC book;
Case study 2: Create table author
USE books;
CREATE TABLE author(
id INT,
auName VARCHAR(20),
nation VARCHAR(20)
);
DESC author;
3.2 The modification of table
- Basic grammar :
#① Change column names
alter table Table name change column The original name New column names The data type of the new column ;
#② Modify the length and constraint or data type of the column name
alter table Table name modify column Name New data types ;
#③ Add new column
alter table Table name add column Name Column type constraint ;
#④ Delete column
alter table Table name drop column Name ;
#⑤ Modify the name of the table
alter table Table name rename to The new name of the table
① Change the column name of the table
Case study: modify book Of publishDate by pubdate
ALTER TABLE book CHANGE COLUMN publishdate pubdate DATETIME;
② Modify the length and constraints of column names
Case study: modify pubdate The type of timestamp Time stamp
ALTER TABLE book MODIFY COLUMN pubdate TIMESTAMP;
③ Add new column
Case study: stay author Add an annual salary column to the table annual
ALTER TABLE author ADD COLUMN annual DOUBLE;
④ Delete column
Case study: Delete annual salary column annual
ALTER TABLE author DROP COLUMN annual;
⑤ Modify the name of the table
Case study: modify author Table name of
ALTER TABLE author RENAME TO book_author;
3.3 The deletion of the table
DROP TABLE book_author;
DROP TABLE IF EXISTS book_author;
# Displays the tables in the current database
SHOW TABLES;
4、 A common way of writing ( Delete before creating )
DROP DATABASE IF EXISTS Library name ;
CREATE DATABASE The name of the new library ;
DROP TABLE IF EXISTS Table name ;
CREATE TABLE The new name of the table (
...
);
5、 Replication of tables
5.1 Just copy the structure of the table Use like keyword
# preparation : Create a table
CREATE TABLE IF NOT EXISTS author(
id INT,
autherName VARCHAR(20),
nation VARCHAR(20)
);
DESC author;
INSERT INTO author VALUES
(1,' Haruki Murakami ',' Japan '),
(2,' Mo yan ',' China '),
(3,' Yuhua ',' China ');
# Copy the structure of the table
CREATE TABLE coptab LIKE author;
5.2 Copy table structure and data
CREATE TABLE coptab2
SELECT * FROM author;
5.3 Copy only part of the data of the table
CREATE TABLE coptab3
SELECT id, autherName FROM author
WHERE nation = ' China ';
5.4 Copy only some fields of the table
CREATE TABLE coptab4
SELECT id, autherName FROM author
WHERE 0; # As long as the screening condition is not satisfied
MySQL Learning notes — Silicon Valley
边栏推荐
- Three schemes of ZK double machine room
- leetcode1-3
- Ruby时间格式转换strftime毫秒匹配格式
- If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
- Container cloud notes
- MPLS: multi protocol label switching
- The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
- Sword finger offer 05 (implemented in C language)
- 入职中国平安三周年的一些总结
- uniapp---初步使用websocket(长链接实现)
猜你喜欢

入职中国平安三周年的一些总结

Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment

DDL statement of MySQL Foundation

Huge number multiplication (C language)

Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2

The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server

183 sets of free resume templates to help everyone find a good job

The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago

Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing

RHCE day 3
随机推荐
If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
2020-03-28
Knapsack problem and 0-1 knapsack problem
Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel
Read a piece of text into the vector object, and each word is stored as an element in the vector. Convert each word in the vector object to uppercase letters. Output the converted elements in the vect
Collection of practical string functions
Some summaries of the third anniversary of joining Ping An in China
Basic principle of servlet and application of common API methods
Recursive method to achieve full permutation (C language)
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2
C language structure to realize simple address book
Reasons and solutions for the 8-hour difference in mongodb data date display
AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
Two way process republication + routing policy
A little feeling
Exercise 7-8 converting strings to decimal integers (15 points)
Recursion and divide and conquer strategy
Dynamic address book
Latex insert picture, insert formula
用数据告诉你高考最难的省份是哪里!