当前位置:网站首页>Mysql database DDL and DML
Mysql database DDL and DML
2022-07-03 16:41:00 【Don't go to graduate school, don't change your name TL】
modify , Delete , Using a database
Modification of data sheet ( Structural changes )
SQL Introduce
What is? SQL?
Structured Query Language: Structured query language . In fact, it defines the operation of all relational databases The rules .
There may be some differences in each way of database operation , We call it “ dialect ”.
SQL General grammar
SQL Statement can One or more lines Writing , With End of semicolon .
You can use Spaces and indents To enhance the Readability .
MySQL Database SQL sentence Case insensitive , It is recommended to use uppercase .
Comments for the database :
Single-line comments :-- The comment # The comment (mysql specific )
Multiline comment :/* The comment */
SQL classification
DDL(Data Definition Language) Data definition language
Used to define database objects : database , surface , Column, etc. . keyword :create, drop,alter etc.
DML(Data Manipulation Language) Data operation language
It is used to update the data of tables in the database Additions and deletions . keyword :insert, delete, update etc.
DQL(Data Query Language) Data query language ( above all !!!)
be used for Inquire about Records of tables in the database ( data ). keyword :select, where etc.
DCL(Data Control Language) Data control language ( understand )
It is used to define the access rights and security level of the database , And create users . keyword :GRANT, REVOKE etc.
DDL Data definition language
DDL database
Query the database
(1) Query all databases
SHOW DATABASES;
(2) Query the creation statement of a database
SHOW CREATE DATABASE demo;
Create database
(1) Create a demo The database of
CREATE DATABASE demo;
(2) Create database ( Judge , If it doesn't exist, create it ) (exists)
CREATE DATABASE IF NOT EXISTS demo;
If you have already created this database ——demo, The program will not report an error , But there will be warnings
(3) Create database ( Specify character set ) (character set)
CREATE DATABASE demo01 CHARACTER SET utf8;
Don't worry about the warning ! In this way, a new one named demo01 And the character set is utf8 The database of .
Small cases
establish demo02 database 、 Create... If it doesn't exist , The specified character set is gbk
CREATE DATABASE IF NOT EXISTS demo02 CHARACTER SET gbk;
have access to show create database demo02; View character set
modify , Delete , Using a database
(1) Modify the character set of the database (character set)
ALTER DATABASE demo CHARACTER SET gbk;
Above SQL Code handle demo The character set of is modified to gbk
In order to deepen the memory , We compare it with the above (3) Create database ( Specify character set ) Contrast
-- Modify the character set of the database
ALTER DATABASE Database name CHARACTER SET Character set ;
-- Specify character set to create database
CREATE DATABASE Database name CHARACTER SET Character set ;
(2) Delete database (drop)
DROP DATABASE demo01;
(3) Delete database ( Judge , Delete... If it exists )
DROP DATABASE IF EXISTS demo02;
(4) Using a database
USE demo;
(4.1) Query the database in use
SELECT DATABASE();
DDL Data sheet
Query of data table
(1) Query all data tables
SHOW TABLES;
Because of the db1 There is only one... In the database surface
(2) The structure of the query table (desc)
DESC student;
Detailed structure information of the table
(3) Query the status information of the table (status,like)
SHOW TABLE STATUS FROM database LIKE ' Data sheet ';
SHOW TABLE STATUS FROM db1 LIKE 'student';
Data table creation
(1) Create data table
among constraint It will be explained in detail in the following chapters !
-- Create a product list ( Product id , Name of commodity , commodity price , inventory , Shelf time )
CREATE TABLE product(
id INT,
NAME VARCHAR(20),
price DOUBLE,
num INT,
insert_time DATE
);
-- see product Detailed structure of the table
DESC product;
( repair ) data type ( part )
1. int: Integer types
2. double: Decimal type
3. date: date , Include only mm / DD / yyyy yyyy-MM-dd
4. datetime: date , Including month, day, hour, minute, second yyyy-MM-dd HH:mm:ss
5. timestamp: Timestamp type Including month, day, hour, minute, second yyyy-MM-dd HH:mm:ss
If you don't assign a value to this field in the future , Or assign the value to null, be The current system time is used by default , To automatically assign
6. varchar: character string
Modification of data sheet ( Structural changes )
(1) Modify the name of the table (rename to)
take product Change the name of the table to product2
ALTER TABLE product RENAME TO product2;
(2) Modify the character set of the table (character set)
take demo In the database product2 The character set of the table is changed to utf8.
ALTER TABLE product2 CHARACTER SET utf8;
notes : You can compare it with the above
-- Modify the character set of the database
ALTER DATABASE Database name CHARACTER SET Character set ;
-- Specify character set to create database
CREATE DATABASE Database name CHARACTER SET Character set ;
-- modify surface Character set for
ALTER TABLE Table name CHARACTER SET Character set ;
(3) Add a separate column (add)
take product2 Add a column to the table color
ALTER TABLE product2 ADD color VARCHAR(10);
(4) Modify the data type of a column in the table (modify)
ALTER TABLE product2 MODIFY color INT;
Have already put varchar(10) Change it to int data type .
(5) Change column name and data type (change)
ALTER TABLE Table name CHANGE Old column names New column names The data type of the new column ;
ALTER TABLE product2 CHANGE color address VARCHAR(200);
(6) Delete a column (drop)
Delete address Column
ALTER TABLE product2 DROP address;
Deletion of data table
(1) Delete data table
DROP TABLE product2;
(2) Delete data table ( Judge , Delete if it exists )
DROP TABLE IF EXISTS product2;
DML Data operation language
New table data
(1) Add data to the specified column
The number of column names and values as well as the data type should be Corresponding , Except for numeric types , All other data types should be quoted ,( single 、 Both are OK , Single quotation marks are recommended )
-- Complete writing
INSERT INTO Table name ( Name 1, Name 2,...) VALUES ( value 1, value 2,...);
INSERT INTO product (id,NAME,price,num) VALUES (100,' Zhang San ',5,5);
SELECT * FROM product;
Be careful :
In this way, add a row of data , A data item can have no data , When the position of the corresponding column name should be removed , as follows :
id by 100,name For Li Si ,price It's empty ,num It's empty .
INSERT INTO product (id,NAME) VALUES (100,' Li Si ');
(2) Add data to all columns by default
-- Omit column names
INSERT INTO Table name VALUES ( value 1, value 2, value 3,...);
INSERT INTO product VALUES (101,' The computer ',9999.99,23);
(3) Batch add data
-- Use between multiple values "," separate
INSERT INTO Table name VALUES ( value 1, value 2, value 3,...),( value 1, value 2, value 3,...),( value 1, value 2, value 3,...);
INSERT INTO product VALUES
(101,' The computer ',9999.99,23),
(102,' The notebook ',2.99,2),
(103,' Rice cooker ',99.99,53);
SELECT * FROM product;
modify 、 Delete table data
(1) Modify the data in the table (update)
UPDATE Table name SET Name 1= value 1, Name 2= value 2,... [WHERE Conditions ];
Modify the statement There must be conditions , If there is no condition , All data will be All modified .
UPDATE product SET price=3500 WHERE NAME = ' The computer ';
take name The value is computer price Value changed to 3500.
(2) Delete data from table (delete)
DELETE FROM Table name [WHERE Conditions ];
Delete... In the statement There must be conditions , If there is no condition , All data will be deleted .
DELETE FROM product WHERE NAME = ' The computer ';
take name The value is the computer record deleted .
边栏推荐
- Page dynamics [2]keyframes
- Interpretation of several important concepts of satellite antenna
- nifi从入门到实战(保姆级教程)——flow
- Cocos Creator 2.x 自动打包(构建 + 编译)
- 香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
- Simulink oscilloscope data is imported into Matlab and drawn
- 2022爱分析· 国央企数字化厂商全景报告
- 特征多项式与常系数齐次线性递推
- Data driving of appium framework for mobile terminal automated testing
- 0214-27100 a day with little fluctuation
猜你喜欢
一台服务器最大并发 tcp 连接数多少?65535?
What material is sa537cl2? Analysis of mechanical properties of American standard container plate
What is the pledge pool and how to pledge?
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
Netease UI automation test exploration: airtest+poco
Daily code 300 lines learning notes day 10
爱可可AI前沿推介(7.3)
Visual SLAM algorithms: a survey from 2010 to 2016
QT串口ui设计和解决显示中文乱码
NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri
随机推荐
NSQ源码安装运行过程
QT串口ui设计和解决显示中文乱码
How to set up SVN server on this machine
线程池执行定时任务
【剑指 Offer】58 - I. 翻转单词顺序
PHP二级域名session共享方案
Is it safe to open a stock account by mobile registration? Does it need money to open an account
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
NSQ source code installation and operation process
What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
如何在本机搭建SVN服务器
Simulink oscilloscope data is imported into Matlab and drawn
Mysql 将逗号隔开的属性字段数据由列转行
斑马识别成狗,AI犯错的原因被斯坦福找到了
Overview of satellite navigation system
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
What is the material of sa302grc? American standard container plate sa302grc chemical composition
PHP secondary domain name session sharing scheme
Explore Netease's large-scale automated testing solutions see here see here