Sql语句的书写也被称之为CRUD,即C(Create增加)R(Retrieve添加)U(Update更新)D(Delete删除)四个操作的首字母。
我们先来看增、删、改这三个相对较为简单,语法比较固定。
这是我创建的表,大家可以复制一下方便演示操作,大家在Navicat中创建数据库,然后进入新建查询,复制代码运行就好了(一下所有表均是以id为主键,并且id设置为自增)
名词解释:
字段:字段指的是一个表中的一列的名字,例如下面表中的id,name等。
记录:表中的每一行叫做一个记录
一、创建表
student表
/*
Navicat Premium Data Transfer
Source Server : SqlBase
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost:3306
Source Schema : test01
Target Server Type : MySQL
Target Server Version : 50624
File Encoding : 65001
Date: 15/07/2024 09:43:56
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`age` int(11) NOT NULL,
`sno` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '学号',
`class_num` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '班级号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '张三', '男', 20, '202001', '20201001');
INSERT INTO `student` VALUES (2, '李四', '男', 21, '202002', '20201001');
INSERT INTO `student` VALUES (3, '王五', '男', 20, '202003', '20201001');
INSERT INTO `student` VALUES (4, '张安', '女', 23, '202004', '20201001');
INSERT INTO `student` VALUES (5, '老张', '女', 21, '202005', '20201001');
INSERT INTO `student` VALUES (6, '花无缺', '女', 22, '202006', '20201002');
INSERT INTO `student` VALUES (7, '小鱼儿', '男', 20, '202007', '20201002');
INSERT INTO `student` VALUES (8, '安顺', '男', 20, '202008', '20201002');
INSERT INTO `student` VALUES (9, '东方月初', '女', 20, '202009', '20201002');
INSERT INTO `student` VALUES (10, '乌贼', '男', 20, '202010', '20201002');
INSERT INTO `student` VALUES (11, '蝴蝶兰', '女', 20, '202011', '20201003');
INSERT INTO `student` VALUES (12, '柳如烟', '男', 20, '202012', '20201003');
INSERT INTO `student` VALUES (13, '花房', '女', 20, '202013', '20201003');
INSERT INTO `student` VALUES (14, '各环节', '男', 20, '202014', '20201003');
INSERT INTO `student` VALUES (15, '玖万', '女', 20, '202015', '20201003');
INSERT INTO `student` VALUES (16, '冥道人', '男', 22, '202016', '20201004');
SET FOREIGN_KEY_CHECKS = 1;
relationship表存储学生和所选课程之间的关系
/*
Navicat Premium Data Transfer
Source Server : SqlBase
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost:3306
Source Schema : test01
Target Server Type : MySQL
Target Server Version : 50624
File Encoding : 65001
Date: 15/07/2024 09:43:45
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for relationship
-- ----------------------------
DROP TABLE IF EXISTS `relationship`;
CREATE TABLE `relationship` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sno` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '学号',
`cno` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '课程号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 39 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of relationship
-- ----------------------------
INSERT INTO `relationship` VALUES (1, '202001', '1001');
INSERT INTO `relationship` VALUES (2, '202001', '1002');
INSERT INTO `relationship` VALUES (3, '202001', '1003');
INSERT INTO `relationship` VALUES (4, '202002', '1001');
INSERT INTO `relationship` VALUES (5, '202002', '1002');
INSERT INTO `relationship` VALUES (6, '202003', '1003');
INSERT INTO `relationship` VALUES (7, '202004', '1001');
INSERT INTO `relationship` VALUES (8, '202004', '1002');
INSERT INTO `relationship` VALUES (9, '202004', '1003');
INSERT INTO `relationship` VALUES (10, '202005', '1001');
INSERT INTO `relationship` VALUES (11, '202005', '1002');
INSERT INTO `relationship` VALUES (12, '202006', '1003');
INSERT INTO `relationship` VALUES (13, '202006', '1001');
INSERT INTO `relationship` VALUES (14, '202006', '1002');
INSERT INTO `relationship` VALUES (15, '202007', '1003');
INSERT INTO `relationship` VALUES (16, '202009', '1001');
INSERT INTO `relationship` VALUES (17, '202009', '1002');
INSERT INTO `relationship` VALUES (18, '202009', '1003');
INSERT INTO `relationship` VALUES (19, '202010', '1001');
INSERT INTO `relationship` VALUES (20, '202010', '1002');
INSERT INTO `relationship` VALUES (21, '202010', '1003');
INSERT INTO `relationship` VALUES (22, '202011', '1001');
INSERT INTO `relationship` VALUES (23, '202012', '1002');
INSERT INTO `relationship` VALUES (24, '202012', '1003');
INSERT INTO `relationship` VALUES (25, '202013', '1001');
INSERT INTO `relationship` VALUES (26, '202013', '1002');
INSERT INTO `relationship` VALUES (27, '202014', '1003');
INSERT INTO `relationship` VALUES (28, '202014', '1001');
INSERT INTO `relationship` VALUES (29, '202014', '1002');
INSERT INTO `relationship` VALUES (30, '202015', '1003');
INSERT INTO `relationship` VALUES (31, '202015', '1001');
INSERT INTO `relationship` VALUES (32, '202016', '1002');
INSERT INTO `relationship` VALUES (33, '202016', '1003');
SET FOREIGN_KEY_CHECKS = 1;
class表(班级名 )
/*
Navicat Premium Data Transfer
Source Server : SqlBase
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost:3306
Source Schema : test01
Target Server Type : MySQL
Target Server Version : 50624
File Encoding : 65001
Date: 15/07/2024 09:42:16
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for class
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`class_num` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '班级号',
`class_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '班级名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES (1, '20201001', '彩虹班');
INSERT INTO `class` VALUES (2, '20201002', '基础班');
INSERT INTO `class` VALUES (3, '20201003', '橙光班');
INSERT INTO `class` VALUES (4, '20201005', '紫光班');
SET FOREIGN_KEY_CHECKS = 1;
course 课程
/*
Navicat Premium Data Transfer
Source Server : SqlBase
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost:3306
Source Schema : test01
Target Server Type : MySQL
Target Server Version : 50624
File Encoding : 65001
Date: 15/07/2024 09:42:35
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '选课表',
`cno` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '课程号',
`gradeName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '课程名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES (1, '1001', 'Jvm基础');
INSERT INTO `course` VALUES (2, '1002', 'Java基础');
INSERT INTO `course` VALUES (3, '1003', '数据结构');
SET FOREIGN_KEY_CHECKS = 1;
二、增加操作(插入操作)
1.单行添加
语法一:
INSERT INTO student(name,sex,age,sno,class_num)
VALUE ('hah','男',18,'202017','20201004');
INSERT INTO student(字段1,字段2,字段3,字段4,字段5,字段6)
VALUES (值1,值1 , 值1 , 值1 , 值1 ,值1);
语法二:
INSERT INTO student SET id = 18,name = '叶秋',sex = '男',
age = 18,sno = '202018',class_num = '20201004';
INSERT INTO student SET 列表1= 值1 , 列表2= 值2 ,列表3= 值3 ,列表4= 值4,列表5= 值5,列表6= 值6;
2.多行添加
语法:
(这个和单行插入的语法一类似,不过需要将VALUE改为VALUES)
INSERT INTO student(列表1,列表2,列表3,列表4,列表5,列表6)
VALUES (值1,值1 , 值1 , 值1 , 值1 ,值1) , (值2,值2 , 值2 , 值2 , 值2 ,值2) ,
(值3,值3 , 值3 , 值3 , 值3 ,值3);
INSERT INTO student(name,sex,age,sno,class_num)
VALUES ('叶秋','男',24,'202017','20201004'),
('叶修','男',24,'202017','20201004'),
('苏沐橙','女',18,'202017','20201004');
注意:1.若未语句中未涉及id则id会自动加一添加到记录中,如下
2.varchar数据额需要加单引号(如‘叶秋’),int数据则不需要
三、修改操作(update )
语法:
update 表名 set 字段1 = 新值 , 字段2 = 新值 where 条件列 = 值;
更新单行数据:
UPDATE student set name = '李逵',age =1 WHERE id = 1;
只更新了id为1的记录,中set到的字段,其他的字段和记录不变
更新所有记录中的字段
如果我们不加修改个条件会怎么样的?
答案是会修改,所有记录中的该字段,所以要谨慎使用
UPDATE student set name = '张三';
四、删除操作
1.删除满足条件的记录
语法:
delete from 表名 where 条件列 = 值;
delete from student where id = 26;
2.删除整张表所有记录
语法1:
DELETE FROM 表名
语法2:
TRUNCATE table 表名
语法3:
DROP table 表名
3.delete 、truncate和DROP的区别:
- delete后边可以跟where条件,truncate,drop不可以
- delete是一行行删除,效率低下
- truncate是全部清空在创建一个与表原始结构相同 的新表
- drop是彻底删除整张表,这张表就不存在了,就连结构都没有了
五、查询
查询语句是sql语句中最复杂,变化最多 ,最灵活的语句,这里先简单介绍一个全表查询和根据逐主键查询,剩下内容请看我的另一篇关于SQL查询语句的博客。
1.全表查询
select * from student
2.根据主键查询
SELECT * FROM student where id= 1;