当前位置:网站首页>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 开始自增加的。
边栏推荐
- Implementation of MySQL master-slave architecture
- Leetcode topic [array] -268- missing numbers
- Leetcode topic [array] -283- move zero
- Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
- [latex bearer] use tables in \title (error \begin doesn't match its definition.)
- SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
- Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
- A simple membership card management system based on Scala
- Platform management background and merchant menu resource management: access control design of platform management background
- Screenshot of the answers to C language exercises
猜你喜欢

Cache breakdown! Don't even know how to write code???

进军AR领域,这一次罗永浩能成吗?

SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用

In those years, interview the abused red and black trees

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

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

Basic requirements: 7 problems in singleton mode
![[matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion](/img/a6/6d3914360ffe4732db0dbd2aaf1994.png)
[matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion

玩转Linux,轻松安装配置MySQL

去中心化NFT交易协议将击败OpenSea
随机推荐
Viewing the task arrangement ability of monorepo tool from turborepo
Use FST JSON to automatically generate faster JSON serialization methods
Problems encountered this week
Programmer interview guide - self introduction
Viteconfigure project path alias
Overall context of concurrent programming
C language -- legal identifier and integer
Day10 daily 3 questions (3): String Matching in array
10 cloud security best practices that enterprises need to know
Decentralized NFT transaction protocol will defeat opensea
vue--vuerouter缓存路由组件
Kubernetes essential tools: 2021
Call the random function to generate 20 different integers and put them in the index group of institute a
分布式架构概述
分布式缓存/缓存集群简介
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
离婚协议中的几个重点
直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
Teach you to learn dapr - 4 Service invocation
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