当前位置:网站首页>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 .
边栏推荐
- Interviewer: how does the JVM allocate and recycle off heap memory
- Cocos Creator 2. X automatic packaging (build + compile)
- Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
- Data driving of appium framework for mobile terminal automated testing
- Is it safe to open an account with flush?
- [combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties
- What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
- Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
猜你喜欢
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
TCP congestion control details | 3 design space
Cocos Creator 2.x 自动打包(构建 + 编译)
什么是质押池,如何进行质押呢?
为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
Interviewer: how does the JVM allocate and recycle off heap memory
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
Add color to the interface automation test framework and realize the enterprise wechat test report
Learn from me about the enterprise flutter project: simplified framework demo reference
Threejs Part 2: vertex concept, geometry structure
随机推荐
Golang 装饰器模式以及在NSQ中的使用
Leetcode binary search tree
How to set up SVN server on this machine
8 tips for effective performance evaluation
Eleven requirements for test management post
Arduino esp32: overall framework of lvgl project (I)
Détails du contrôle de la congestion TCP | 3. Espace de conception
AcWing 第58 场周赛
ThreeJS 第二篇:顶点概念、几何体结构
PHP二级域名session共享方案
Explore Cassandra's decentralized distributed architecture
Is it safe to open a stock account by mobile registration? Does it need money to open an account
消息队列消息丢失和消息重复发送的处理策略
【剑指 Offer 】64. 求1+2+…+n
What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
nifi从入门到实战(保姆级教程)——flow
MongoDB 的安装和基本操作
SVN使用规范
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..