当前位置:网站首页>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 )
边栏推荐
- What is needed to develop a domestic arm intelligent edge computing gateway
- [AUTOSAR twelve mode management]
- 全志A40i/T3如何通过SPI转CAN
- KingbaseES ALTER TABLE 中 USING 子句的用法
- Correctly distinguish the similarities and differences among API, rest API, restful API and web service
- matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
- [shutter] image component (configure local GIF image resources | load placeholder with local resources)
- 指针进阶(一)
- [flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
- MySQL multi table joint deletion
猜你喜欢
[overview of AUTOSAR three RTE]
How wide does the dual inline for bread board need?
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
[AUTOSAR eight OS]
MySQL
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
How to systematically learn machine learning
ROS2之ESP32简单速度消息测试(极限频率)
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
12_微信小程序之微信视频号滚动自动播放视频效果实现
随机推荐
鏈錶內指定區間反轉
465. 最优账单平衡 DFS 回溯
飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
解决ReactNative使用webView存在缓存问题
[AUTOSAR twelve mode management]
Button wizard play strange learning - go back to the city to buy medicine and add blood
Esp32 simple speed message test of ros2 (limit frequency)
The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
Basic use of sringcloud & use of component Nacos
合并K个已排序的链表
R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
链表内指定区间反转
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
Specified interval inversion in the linked list
Telephone network problems
[C language] branch and loop statements (Part 1)
Advanced pointer (I)
R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
[love crash] neglected details of gibaro