当前位置:网站首页>14《MySQL 教程》INSERT 插入数据
14《MySQL 教程》INSERT 插入数据
2022-06-26 16:57:00 【太原sem】
1.向数据表插入一条数据
这里以前面新建的 teacher 表为例, teacher 表的字段为 name、age、id_number,向教师表新增一条数据命令如下:
INSERT INTO teacher (name,age,id_number) VALUES ('秦小贤',18,'42011720200604088X');执行下上面的这条命令,执行结果如下图:

其中 “INSERT INTO” 关键字表示这条命令是插入数据命令,“teacher” 是要插入数据的目标数据表名称,“name,age,id_number” 是表的字段名,“VALUES” 后面跟的是字段对应的值,值的顺序和前面字段的顺序一致。
2.查询表所有数据
上面介绍了如何向数据表插入一条数据,这里需要查看数据表已有的所有数据:
SELECT * FROM teacher;执行结果如下图:

“SELECT” 关键字表示这条命令是查询相关命令,"*" 则代表要查询出表中所有的数据。“FROM teacher” 则表明要查询的是哪一个数据表。
关于 SELECT 查询语句还有很多中使用场景,比如我们要查询出表中 age 字段的值大于 20 岁的数据。关于 SELECT 的其他使用我们会在后面的小节详细讲解。
3. 向数据表插入多条数据
新增多条数据命令:
INSERT INTO teacher(name,age,id_number)VALUES('王小花',19,'42011720200604077X'),('张晓丽',18,'42011720200604099X'),('刘美丽',20,'42011720200604020X'),('吴帅',21,'42011720200604022X'),('张平',22,'42011720200604033X')执行结果如下图:

4.小结
本小节介绍了如图向数据表插入一条数据、查询表所有数据、向表插入多条数据。前面介绍数据表的设计规范时介绍了数据表的第二设计范式要求所有数据表需要有业务主键。需要注意的是本节中 teacher 表的业务主键为自增 id,因此插入数据的时候不需要插入 id 字段的值。id 字段的默认是从 1 开始自增的,也可以指定自增起始值,如下建表语句,id自增值是从100开始的:
CREATE TABLE `teacher` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL DEFAULT '教师名', `age` int(10) unsigned NOT NULL DEFAULT '0', `id_number` varchar(18) NOT NULL DEFAULT '', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8其中 “AUTO_INCREMENT=100” 表示自增主键 id 的值默认从 100 开始自增加的。
边栏推荐
- 分布式架构概述
- 玩轉Linux,輕松安裝配置MySQL
- Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack
- Record the use process of fenics
- Day10 daily 3 questions (2): count the number of the largest groups
- 数字藏品与NFT到底有何区别
- Calculate the average of N numbers in the group indexed by the formal parameter x, move the data less than the average in the group indexed to the front of the array, and move the data greater than or
- Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
- Kubernetes essential tools: 2021
- Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
猜你喜欢

Fire evacuation and self rescue... This safety production and fire training is full!

SQL injection for Web Security (3)

Teach you to learn dapr - 3 Run the first with dapr Net program

建立自己的网站(16)

【MATLAB项目实战】基于卷积神经网络与双向长短时(CNN-LSTM)融合的锂离子电池剩余使用寿命预测

Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC

【uniapp】uniapp手机端使用uni.navigateBack失效问题解决

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)

【万字总结】以终为始,详细分析高考志愿该怎么填

Microservice architecture practice: business management background and SSO design, SSO client design
随机推荐
[recommendation system learning] recommendation system architecture
Notes on flowus
In those years, interview the abused red and black trees
Teach you to learn dapr - 3 Run the first with dapr Net program
[recommendation system learning] technology stack of recommendation system
R329 (maix-ii-a (M2A) data summary
分布式架构概述
Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
[latex bearer] use tables in \title (error \begin doesn't match its definition.)
Interpretation of cloud native microservice technology trend
Set up your own website (16)
Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
【推荐系统学习】推荐系统的技术栈
防火 疏散 自救…这场安全生产暨消防培训干货满满!
Introduction to minimal API
Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)
Problems encountered this week
Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
玩转Linux,轻松安装配置MySQL
Teach you to learn dapr - 4 Service invocation