当前位置:网站首页>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 .
边栏推荐
- 面试官:JVM如何分配和回收堆外内存
- 为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
- Page dynamics [2]keyframes
- TCP擁塞控制詳解 | 3. 設計空間
- NSQ source code installation and operation process
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- Daily code 300 lines learning notes day 10
- 8 cool visual charts to quickly write the visual analysis report that the boss likes to see
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
- Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
猜你喜欢

Interviewer: how does the JVM allocate and recycle off heap memory

What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR

什么是质押池,如何进行质押呢?

跟我学企业级flutter项目:简化框架demo参考

Thread pool executes scheduled tasks

【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事

8个酷炫可视化图表,快速写出老板爱看的可视化分析报告

(补)双指针专题

关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM

NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
随机推荐
8 tips for effective performance evaluation
Visual SLAM algorithms: a survey from 2010 to 2016
Nifi from introduction to practice (nanny level tutorial) - flow
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
JSON 与 BSON 区别
[combinatorics] recursive equation (outline of recursive equation content | definition of recursive equation | example description of recursive equation | Fibonacci Series)
What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
arduino-esp32:LVGL项目(一)整体框架
[statement] about searching sogk1997 and finding many web crawler results
PHP中register_globals参数设置
Is it safe to open a stock account by mobile registration? Does it need money to open an account
远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
Interviewer: how does the JVM allocate and recycle off heap memory
Golang 匿名函数使用
Develop team OKR in the way of "crowdfunding"
面试官:JVM如何分配和回收堆外内存
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
【剑指 Offer】58 - II. 左旋转字符串
One article takes you to understand machine learning
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time