-- 1、建表-----------------------------------------------
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
NO INT,
NAME VARCHAR(20) ,
gender CHAR(1) ,
gradeid INT ,
phone INT ,
address VARCHAR(15) ,
register_time DATETIME ,
userid INT
)
-- 2、删表-----------------------------------------------
DROP TABLE student
-- 3、 设置外建----------------------------------------------------------------
ALTER TABLE student ADD CONSTRAINT fk_student_userid FOREIGN KEY(userid) REFERENCES USER(id);
ALTER TABLE student ADD CONSTRAINT fk_student_gradeid FOREIGN KEY(gradeid) REFERENCES grade(id);
-- 4、选择查询------------------------------------------------
SELECT
s.id,
s.no,
s.name sname,
s.gender,
g.name gname,
s.phone,
s.address,
s.register_time,
u.account
FROM student s LEFT JOIN grade g ON s.gradeid = g.id
LEFT JOIN USER u ON s.userid = u.id
//选择------------------------------------------------------
SELECT * FROM student WHERE id = '1'
//插入------------------------------------------------------
INSERT INTO student(NO,NAME,gender,gradeid,phone,address,register_time,userid)VALUE('107','马七','女',1,'555555555','大连','2021-01-08','2')
//删除------------------------------------------------------
DELETE FROM student WHERE id = '7'
//修改------------------------------------------------------
UPDATE student SET NAME='jim',gender='男' WHERE id = 4
建表效果: