当前位置:网站首页>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
边栏推荐
- 按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
- Snake (C language)
- Tables in the thesis of latex learning
- Network disk installation
- AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
- leetcode1-3
- Dos:disk operating system, including core startup program and command program
- Velodyne configuration command
- [200 opencv routines] 218 Multi line italic text watermark
- Rhcsa12
猜你喜欢

今日睡眠质量记录78分

基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1

OSPF summary

From programmers to large-scale distributed architects, where are you (I)

Rhcsa day 9

DML statement of MySQL Foundation

VLAN part of switching technology

Doris / Clickhouse / Hudi, a phased summary in June

Huge number multiplication (C language)

Servlet基本原理与常见API方法的应用
随机推荐
leetcode1-3
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
Four characteristics and isolation levels of database transactions
Check 15 developer tools of Alibaba
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
Development guidance document of CMDB
/*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*/
Differences among opencv versions
Static comprehensive experiment ---hcip1
【Day1】 deep-learning-basics
Introduction to tree and binary tree
Linked list operation can never change without its roots
Rhcsa day 9
From programmers to large-scale distributed architects, where are you (I)
Batch distribution of SSH keys and batch execution of ansible
RHCE day 3
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
【Day2】 convolutional-neural-networks
Write a program that uses pointers to set all elements of an int array to 4.18: 0.
Dynamic memory management