当前位置:网站首页>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
边栏推荐
- How to teach yourself to learn programming
- Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
- 2021-08-11 function pointer
- Reasons and solutions for the 8-hour difference in mongodb data date display
- Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel
- 基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
- Deep learning 500 questions
- /*The rewriter outputs the contents of the IA array. It is required that the type defined by typedef cannot be used in the outer loop*/
- Dichotomy search (C language)
- Check 15 developer tools of Alibaba
猜你喜欢
Vs201 solution to failure to open source file HPP (or link library file)
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
Delayed message center design
Quick sort (C language)
C language structure to realize simple address book
Servlet基本原理与常见API方法的应用
[200 opencv routines] 218 Multi line italic text watermark
DDL statement of MySQL Foundation
C language - stack
Collection of practical string functions
随机推荐
2020-03-28
DCL statement of MySQL Foundation
Custom type: structure, enumeration, union
Number of relationship models
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
Rhcsa day 9
/*Write a function to open the file for input, read the contents of the file into the vector container of string class 8.9: type, and store each line as an element of the container object*/
Knapsack problem and 0-1 knapsack problem
Latex arranges single column table pictures in double column format articles
入职中国平安三周年的一些总结
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Rhcsa - day 13
2. Data type
183 sets of free resume templates to help everyone find a good job
今日睡眠质量记录78分
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
Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
Dynamic memory management
Basic principle of servlet and application of common API methods
Exercise 7-3 store the numbers in the array in reverse order (20 points)