MySQL Notes 002 (View)

Here I created three tables to use the view

CREATE TABLE userd(
user_id INT(4) PRIMARY KEY,
loginid VARCHAR(10) NOT NULL,
name_ VARCHAR(10) NOT NULL,
mobile INT(11) NOT NULL,
email VARCHAR(20) NOT NULL
);
CREATE TABLE course(
course_id INT(4) PRIMARY KEY,
course_name VARCHAR(10) NOT NULL,
description VARCHAR(10) NOT NULL
);
CREATE TABLE user_course(
id INT(4) PRIMARY KEY,
user_id INT(4) NOT NULL,
course_id INT(4) NOT NULL
);
– Add foreign key constraints. Foreign key constraints can only be added under primary key or UNIQUE constraints. If you add two, change the name slightly
– for example: user_course and user_course1
ALTER TABLE user_course ADD CONSTRAINT fk_user_course
FOREIGN KEY(user_id) REFERENCES userd(user_id);
ALTER TABLE user_course ADD CONSTRAINT fk_user_course1
FOREIGN KEY(course_id) REFERENCES course(course_id);

The created table:
Insert picture description here
Insert picture description here
Insert picture description here

Here we use userd and course as the outer join of user_course to connect the three tables
-query
SELECT u.name_,c.course_name,c.description
FROM userd u JOIN user_course uc ON u.user_id=uc.user_id JOIN course c ON c = uc.course_id .course_id
the WHERE u.name_ = 'peanut';
the SELECT * = name_ the FROM vw_user_course the WHERE 'peanut';
view
the CREATE
the vIEW test. vw_user_course
the AS u.name_ the SELECT, c.course_name, c.description
the FROM userd the ON UC U user_course the JOIN u.user_id=uc.user_id JOIN course c ON c.course_id=uc.course_id;
(SELECT * FROM vw_user_course); The
view is not a table and does not save data, but a virtual table. The data of the view comes from the base table.
If the data of the view only comes from a base table, then modifying the view is equivalent to modifying the base table.

Guess you like

Origin blog.csdn.net/m0_49708063/article/details/108735956