当前位置:网站首页>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
边栏推荐
- Dynamic memory management
- 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
- Latex learning insertion number - list of filled dots, bars, numbers
- If you don't know these four caching modes, dare you say you understand caching?
- Four characteristics and isolation levels of database transactions
- Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
- C language - stack
- Collection of practical string functions
- 使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
- 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
猜你喜欢
Development guidance document of CMDB
RHCE day 3
From programmers to large-scale distributed architects, where are you (I)
Four characteristics and isolation levels of database transactions
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Architecture introduction
Occasional pit compiled by idea
【Day1】 deep-learning-basics
MPLS: multi protocol label switching
Huge number multiplication (C language)
随机推荐
Reasons and solutions for the 8-hour difference in mongodb data date display
Pod management
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
Custom type: structure, enumeration, union
Quick sort (C language)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Today's sleep quality record 78 points
Exercise 7-2 finding the maximum value and its subscript (20 points)
C language - stack
Es advanced series - 1 JVM memory allocation
Exercise 7-3 store the numbers in the array in reverse order (20 points)
Rhcsa12
leetcode1-3
Whether a person is reliable or not, closed loop is very important
Rhsca day 11 operation
Huge number (C language)
MPLS: multi protocol label switching
/*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*/
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
Deep learning 500 questions