CREATE DATABASE IF NOT EXISTS hhh DEFAULT CHARSET utf8;
USE hhh;
#建学生表
CREATE TABLE IF NOT EXISTS stu
(
NO INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
INSERT INTO stu(NAME)
VALUES("张三"),("李四"),("夏目"),("贵志"),("小红");
#建科目表
CREATE TABLE IF NOT EXISTS sub
( subno INT PRIMARY KEY AUTO_INCREMENT,
subname VARCHAR(25)
);
INSERT INTO sub(subname)
VALUES("java"),("c++"),("python"),("mysql");
#建成绩表
CREATE TABLE IF NOT EXISTS score(NO INT,score INT,subno INT);
INSERT INTO score(NO,score,subno)VALUES(1,77,1),(2,88,2),(3,99,3),(4,66,4);
#增
INSERT INTO stu(NAME)
VALUES("老王");
#删
DELETE FROM stu WHERE NAME="小红";
#改
UPDATE stu SET NAME="波波熊" WHERE NO=1;
#查
SELECT * FROM stu;
#三表关联
SELECT st.*,sc.* FROM stu st
LEFT JOIN score sc ON st.no=sc.no
LEFT JOIN sub su ON sc.subno=su.subno;