MySQL—数据处理之增删改查(四)

一、数据准备

-- 数据准备
-- 1 创建教师表
CREATE TABLE teacher(
tea_num VARCHAR(10) PRIMARY KEY,-- 主键
name VARCHAR(30),
gender CHAR(2),
age INT,
department VARCHAR(30),
institute VARCHAR(50),
salary DOUBLE,
hire_date date
);

-- 1.1 插入数据
-- 教师号为100001,姓名为旗木卡卡西,性别男,年龄30,科室为综合科,就职学院为火影学院,薪水2000,入职日期为1988-12-21
INSERT INTO teacher
VALUES("100001","旗木卡卡西","男",30,"综合科","火影学院",2000,"1988-12-21");
-- 教师号为100002,姓名为迈特凯,性别男,年龄31,科室为体术科,就职学院为火影学院,薪水2100,入职日期为1987-10-21
INSERT INTO teacher
VALUES("100002","迈特凯","男",31,"体术科","火影学院",2100,"1987-10-21");
-- 教师号为100003,姓名为孙悟空,性别男,年龄100,科室为猴科,就职学院为西游学院,薪水1500,入职日期为1966-11-01
INSERT INTO teacher
VALUES("100003","孙悟空","男",100,"猴科","西游学院",1500,"1966-11-01");
-- 教师号为100004,姓名为马基,性别男,年龄42,科室为风科,就职学院为风影学院,薪水3000,入职日期为1975-04-21
INSERT INTO teacher
VALUES("100004","马基","男",42,"风科","风影学院",3000,"1975-04-21");

-- 2 创建学生表
CREATE TABLE student(
stu_num VARCHAR(10),
name VARCHAR(30),
gender CHAR(2),
age INT,
birthday DATE,
major VARCHAR(30),
institute VARCHAR(50),
fee DOUBLE,
tea_num VARCHAR(10),
create_time datetime,
PRIMARY KEY (stu_num,name), -- 主键
FOREIGN KEY (tea_num) REFERENCES teacher(tea_num) -- 外键
);

-- 2.1 插入数据
INSERT INTO student
VALUES("2023000001","鸣人","男",12, '2001-01-02',"影分身","火影学院",200.1,"100001",NOW());
INSERT INTO student
VALUES("2023000000","宇智波佐助","男",13, '2001-07-12',"写轮眼","火影学院",200.1,"100001",NOW());
INSERT INTO student
VALUES('2023000002','小樱', '女', 14, '2001-05-18', '治疗', '火影学院', '200.1',"100001",'2023-01-12 20:04:11');
INSERT INTO student
VALUES("2023000003","我爱罗","男",16, "1999-12-31","砂之术","风影学院",400,"100004",NOW());
INSERT INTO student
VALUES('2023000005','手鞠', '女', 18, '1995-01-25', '三星扇', '风影学院','200.1',"100004",NOW());

二、实操练习

-- 实操练习
-- 1 插入数据:INSERT
-- 1.1 隐士方式:在列名表中省略该列的值
-- 1.1.2 省略其中的"age","birthday"
INSERT INTO student(stu_num,name,gender,major,institute,fee,tea_num,create_time)
VALUES("2023000004","小李1","男","体术","火影学院",100,"100002",NOW());
-- 1.2 显示方式:在VALUES子句中指定空值
-- 1.2.1 其中的"age","birthday"对应的都是NULL
INSERT INTO student(stu_num,name,gender,age,birthday,major,institute,fee,tea_num,create_time)
VALUES("2023000004","小李2","男",NULL, NULL,"体术","火影学院",100,"100002",NOW());
-- 1.3 从其他表中拷贝数据
-- 1.3.1 在INSERT语句中加入子查询
-- 根据student表结构创建一个新表student2
CREATE TABLE student2 LIKE student;

-- 查询表studen2信息,确定为空
SELECT * FROM student2;

-- 将student表中stu_num为"2023000004"的学生信息移到student2中
INSERT INTO student2
SELECT *
FROM student
WHERE stu_num = "2023000004";

-- 2 更新数据:UPDATE
-- 2.1 使用WHERE子句指定需要更新的数据
UPDATE student 
SET age = 18 
WHERE stu_num = "2023000004";
-- 2.2 如果省略WHERE子句,则表中的所有数据都将被更新
UPDATE student 
SET age = 16;
-- 2.3 更新中的数据完整性错误 因为tea_num为外键,teacher.tea_num中没有666666的老师
UPDATE student
SET tea_num = "666666"
WHERE name = "小李2";

-- 3 删除数据: DELETE
-- 3.1 使用 WHERE 子句删除指定的记录
DELETE FROM student
WHERE name = "小李2";
-- 3.2 如果省略 WHERE 子句,则表中的全部数据将被删除
DELETE FROM student;
-- 3.3 删除中的数据完整性错误
-- 3.3.1 删除失败,因为外键约束,即student表中存在tea_num= "100004"的信息
DELETE FROM teacher
WHERE tea_num = "100004";
-- 3.3.2 删除成功,因为student表中不存在tea_num= "100002"的信息,允许删除
DELETE FROM teacher
WHERE tea_num = "100002";

你可能感兴趣的:(MySQL,mysql,数据库)