当前位置:网站首页>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
边栏推荐
- MySQL case
- Differences among opencv versions
- Static comprehensive experiment ---hcip1
- 基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
- Summary of several job scheduling problems
- From programmers to large-scale distributed architects, where are you (2)
- 【OpenCV 例程200篇】218. 多行倾斜文字水印
- Dynamic memory management
- /*Rewrite the program, find the value of the element, and return the iterator 9.13: pointing to the found element. Make sure that the program works correctly when the element you are looking for does
- Batch distribution of SSH keys and batch execution of ansible
猜你喜欢
随机推荐
Knapsack problem and 0-1 knapsack problem
Rhcsa - day 13
Exercise 7-8 converting strings to decimal integers (15 points)
Rhcsa12
【Day1】 deep-learning-basics
2. Data type
Evolution from monomer architecture to microservice architecture
Write a program to judge whether the two arrays are equal, and then write a similar program to compare the two vectors.
What is devsecops? Definitions, processes, frameworks and best practices for 2022
RHCE - day one
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
leetcode1-3
C language structure to realize simple address book
Add t more space to your computer (no need to add hard disk)
Write a program to define an array with 10 int elements, and take its position in the array as the initial value of each element.
Crawl Zhejiang industry and trade news page
OSPF summary
183 sets of free resume templates to help everyone find a good job
按键精灵跑商学习-商品数量、价格提醒、判断背包