当前位置:网站首页>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 .
边栏推荐
- What material is sa537cl1? Sa537cl1 corresponds to the national standard material
- TCP拥塞控制详解 | 3. 设计空间
- CC2530 common registers for port interrupts
- Unity项目优化案例一
- Golang 匿名函数使用
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- Thread pool executes scheduled tasks
- MySQL single table field duplicate data takes the latest SQL statement
- 8 tips for effective performance evaluation
- [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
猜你喜欢

Basis of target detection (IOU)

QT串口ui设计和解决显示中文乱码

CC2530 common registers for port interrupts

What is the maximum number of concurrent TCP connections for a server? 65535?

What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR

Visual SLAM algorithms: a survey from 2010 to 2016

NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)

Explore Cassandra's decentralized distributed architecture

(Supplement) double pointer topic
![[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](/img/81/59ed6bebf5d85e9eb71bd4ca261309.jpg)
[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
随机推荐
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
Extraction of the same pointcut
Construction practice camp - graduation summary of phase 6
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
Is it safe to open an account with flush?
智慧之道(知行合一)
Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
Cocos Creator 2. X automatic packaging (build + compile)
(Supplement) double pointer topic
QT串口ui设计和解决显示中文乱码
Cocos Creator 2.x 自动打包(构建 + 编译)
Interviewer: how does the JVM allocate and recycle off heap memory
SVN使用规范
TCP拥塞控制详解 | 3. 设计空间
Register in PHP_ Globals parameter settings
IDEA-配置插件
PHP CI (CodeIgniter) log level setting
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
[combinatorics] recursive equation (outline of recursive equation content | definition of recursive equation | example description of recursive equation | Fibonacci Series)
Svn usage specification