当前位置:网站首页>MySQL foundation 05 DML language
MySQL foundation 05 DML language
2022-07-03 01:12:00 【Super brother 1986】
The meaning of database : data storage 、 Data management
Data management method of database :
- adopt SQLyog And other management tools to manage database data
- adopt DML Statement management database data
DML Language : Data operation language
- Used to manipulate data contained in database objects
Include :
INSERT ( Add data statement )
UPDATE ( Update data statement )
DELETE ( Delete data statement )
INSERT command
INSERT INTO Table name [( Field 1, Field 2, Field 3,…)] VALUES(‘ value 1’,‘ value 2’,‘ value 3’)
Be careful :
- Fields or values are separated by commas .
- ’ Field 1, Field 2…’ This part can be omitted , But the added value must be consistent with the table structure , Data columns , The order corresponds to , And the quantity is the same .
- Multiple data can be inserted at the same time , values Separated by commas .
– How to create a foreign key : Create sub table and foreign key
-- Grade table (id\ Grade name )
CREATE TABLE `grade` (
`gradeid` INT(10) NOT NULL AUTO_INCREMENT COMMENT ' grade ID',
`gradename` VARCHAR(50) NOT NULL COMMENT ' Grade name ',
PRIMARY KEY (`gradeid`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
-- Student information sheet ( Student number , full name , Gender , grade , mobile phone , Address , Date of birth , mailbox , ID number )
CREATE TABLE `student` (
`studentno` INT(4) NOT NULL COMMENT ' Student number ',
`studentname` VARCHAR(20) NOT NULL DEFAULT ' anonymous ' COMMENT ' full name ',
`sex` TINYINT(1) DEFAULT '1' COMMENT ' Gender ',
`gradeid` INT(10) DEFAULT NULL COMMENT ' grade ',
`phoneNum` VARCHAR(50) NOT NULL COMMENT ' mobile phone ',
`address` VARCHAR(255) DEFAULT NULL COMMENT ' Address ',
`borndate` DATETIME DEFAULT NULL COMMENT ' Birthday ',
`email` VARCHAR(50) DEFAULT NULL COMMENT ' mailbox ',
`idCard` VARCHAR(18) DEFAULT NULL COMMENT ' ID number ',
PRIMARY KEY (`studentno`),
KEY `FK_gradeid` (`gradeid`),
CONSTRAINT `FK_gradeid` FOREIGN KEY (`gradeid`) REFERENCES `grade`
(`gradeid`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
-- How to add statements using statements ?
-- grammar : INSERT INTO Table name [( Field 1, Field 2, Field 3,...)] VALUES(' value 1',' value 2',' value 3')
INSERT INTO grade(gradename) VALUES (' Freshman ');
-- The primary key increases automatically , Can we omit that ?
INSERT INTO grade VALUES (' Sophomore ');
-- Inquire about :INSERT INTO grade VALUE (' Sophomore ') Error code : 1136
Column count doesn`t match value count at row 1
-- Conclusion :' Field 1, Field 2...' This part can be omitted , But the added value must be consistent with the table structure , Data columns , The order corresponds to , And the quantity is one
Cause .
-- Insert multiple data at a time
INSERT INTO grade(gradename) VALUES (' Junior year '),(' Senior ');
update command
grammar :
UPDATE Table name SET column_name=value [,column_name2=value2,…] [WHERE condition];
Be careful :
- column_name For the data column to change
- value Is the modified data , It can be a variable , Specifically refers to , Expression or nested SELECT result
- condition Filter by , Modify all column data of the table if not specified
Operator :
-- Modify grade information
UPDATE grade SET gradename = ' high school ' WHERE gradeid = 1;
DELETE command
DELETE FROM Table name [WHERE condition];
Be careful :condition Filter by , Delete all column data of the table if not specified
-- Delete the last data DELETE FROM grade WHERE gradeid = 5
TRUNCATE command
effect : Used to completely empty table data , But the table structure , Indexes , Constraints, etc., remain unchanged ;
grammar :
TRUNCATE [TABLE] table_name;
Be careful : The difference in DELETE command
identical : Can delete data , Do not delete table structure , but TRUNCATE Faster, different :
- Use TRUNCATE TABLE To reset AUTO_INCREMENT Counter
- Use TRUNCATE TABLE No impact on transactions ( It will be said after the transaction )
test :
-- Create a test table
CREATE TABLE `test` (
`id` INT(4) NOT NULL AUTO_INCREMENT,
`coll` VARCHAR(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
-- Insert several test data
INSERT INTO test(coll) VALUES('row1'),('row2'),('row3');
-- Delete table data ( No where Conditions of the delete)
DELETE FROM test;
-- Conclusion : If not specified Where Delete all column data of the table , The current value of auto increment is still based on the original value , Will log .
-- Delete table data (truncate)
TRUNCATE TABLE test;
-- Conclusion :truncate Delete data , The auto increment current value will return to the original value and start again ; No logging .
-- Also use DELETE Empty database table data of different engines . After restarting the database service
-- InnoDB : Auto increment column starts again from the initial value ( Because it's stored in memory , If you cut off the power, you will lose )
-- MyISAM : The auto increment column still starts from the previous auto increment data ( In the file , Will not be lost )
边栏推荐
- [AUTOSAR eight OS]
- Leetcode-871: minimum refueling times
- KingbaseES ALTER TABLE 中 USING 子句的用法
- Deep analysis of data storage in memory
- [C language] branch and loop statements (Part 1)
- Matlab finds the position of a row or column in the matrix
- Trois tâches principales: asynchrone, courrier et timing
- [overview of AUTOSAR three RTE]
- 无向图的割点
- R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
猜你喜欢
测试右移:线上质量监控 ELK 实战
Asynchronous, email and scheduled tasks
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
tail -f 、tail -F、tailf的区别
Merge K sorted linked lists
[case sharing] let the development of education in the new era advance with "number"
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
How to convert Quanzhi a40i/t3 to can through SPI
数据分析思维分析犯法和业务知识——分析方法(一)
随机推荐
Win10 can't be installed in many ways Problems with NET3.5
MySQL
The difference between tail -f, tail -f and tail
excel IF公式判断两列是否相同
MySQL
[case sharing] let the development of education in the new era advance with "number"
信息熵的基础
(C语言)数据的存储
安全运营四要素之资产、脆弱性、威胁和事件
Assets, vulnerabilities, threats and events of the four elements of safe operation
Esp32 simple speed message test of ros2 (limit frequency)
按鍵精靈打怪學習-多線程後臺坐標識別
Leetcode-1964: find the longest effective obstacle race route to each position
mysql 多表联合删除
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
Key wizard play strange learning - front desk and Intranet send background verification code
Compare version number
机器学习术语
Leetcode-224: basic calculator