当前位置:网站首页>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 开始自增加的。
边栏推荐
- Technical scheme design of chain game system development - NFT chain game system development process and source code
- Byte interview: two array interview questions, please accept
- proxy
- Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
- 108. simple chat room 11: realize client group chat
- Troubleshooting ideas that can solve 80% of faults!
- Environment setup mongodb
- [Error] ld returned 1 exit status
- [latex bearer] use tables in \title (error \begin doesn't match its definition.)
- vue--vuerouter缓存路由组件
猜你喜欢

Can Luo Yonghao succeed in entering the AR field this time?

Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!

Screenshot of the answers to C language exercises

Teach you to learn dapr - 4 Service invocation

【推荐系统学习】推荐系统架构

并发之Synchronized说明

Set up your own website (16)

Kubernetes essential tools: 2021

数字藏品与NFT到底有何区别

Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
随机推荐
分布式缓存/缓存集群简介
玩转Linux,轻松安装配置MySQL
Count the number of words in a line of string and take it as the return value of the function
Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
Set up your own website (16)
去中心化NFT交易协议将击败OpenSea
The texstudio official website cannot be opened
Count the number of each vowel letter in the string
Turtle cartography
The student record consists of student number and academic performance. The data of n students have been stored in the a structure array to find out the student record with the lowest performance
对NFT市场前景的7个看法
类型多样的石膏PBR多通道贴图素材,速来收藏!
关于FlowUs这一款国民好笔记
Teach you to learn dapr - 2 Must know concept
Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
Technical scheme design of chain game system development - NFT chain game system development process and source code
Viewing the task arrangement ability of monorepo tool from turborepo
She said she was tired! So tired! I want to change my career
What is the preferential account opening policy of securities companies now? Is it safe to open an account online now?
Leetcode topic [array] -268- missing numbers