目录
- -- 创建user表
- CREATE TABLE user(
- id INT PRIMARY KEY auto_increment, -- 用户id
- name VARCHAR(20), -- 用户姓名
- age INT -- 用户年龄
- );
- -- 添加数据
- INSERT INTO user VALUES (1,'张三',23);
- INSERT INTO user VALUES (2,'李四',24);
- INSERT INTO user VALUES (3,'王五',25);
- INSERT INTO user VALUES (4,'赵六',26);
-
- -- 创建订单表
- CREATE TABLE orderlist(
- id INT PRIMARY KEY auto_increment, -- 订单id
- number VARCHAR(20), -- 订单编号
- uid INT, -- 外键字段
- CONSTRAINT ou FOREIGN KEY (uid) REFERENCES user(id)
- );
- -- 添加数据
- INSERT INTO orderlist VALUES (1,'hm001',1);
- INSERT INTO orderlist VALUES (2,'hm002',1);
- INSERT INTO orderlist VALUES (3,'hm003',2);
- INSERT INTO orderlist VALUES (4,'hm004',2);
- INSERT INTO orderlist VALUES (5,'hm005',3);
- INSERT INTO orderlist VALUES (6,'hm006',3);
- INSERT INTO orderlist VALUES (7,'hm007',NULL);
-
- -- 创建商品分类表
- CREATE TABLE category(
- id INT PRIMARY KEY auto_increment, -- 商品分类id
- NAME VARCHAR(20) -- 商品分类名称
- );
- -- 添加数据
- INSERT INTO category VALUES (1,'手机数码');
- INSERT INTO category VALUES (2,'电脑办公');
- INSERT INTO category VALUES (3,'烟酒茶糖');
- INSERT INTO category VALUES (4,'鞋靴箱包');
-
- -- 创建商品表
- CREATE TABLE product(
- id INT PRIMARY KEY auto_increment, -- 商品id
- NAME VARCHAR(20), -- 商品名称
- cid INT, -- 外键字段
- CONSTRAINT cp_1 FOREIGN KEY (cid) REFERENCES category(id)
- );
- -- 添加数据
- INSERT INTO product VALUES (1,'华为手机',1);
- INSERT INTO product VALUES (2,'小米手机',1);
- INSERT INTO product VALUES (3,'联想电脑',2);
- INSERT INTO product VALUES (4,'苹果电脑',2);
- INSERT INTO product VALUES (5,'中华香烟',3);
- INSERT INTO product VALUES (6,'玉溪香烟',3);
- INSERT INTO product VALUES (7,'计生用品',NULL);
-
- -- 创建中间表
- CREATE TABLE us_pro(
- upid INT PRIMARY KEY auto_increment, -- 中间表id
- uid INT, -- 外键字段,需要和用户表的主键产生关联
- pid INT, -- 外键字段,需要和商品表的主键产生关联
- CONSTRAINT up_1 FOREIGN KEY (uid) REFERENCES user(id),
- CONSTRAINT up_2 FOREIGN KEY (pid) REFERENCES product(id)
- );
- -- 添加数据
- INSERT INTO us_pro VALUES (NULL,1,1);
- INSERT INTO us_pro VALUES (NULL,1,2);
- INSERT INTO us_pro VALUES (NULL,1,3);
- INSERT INTO us_pro VALUES (NULL,1,4);
- INSERT INTO us_pro VALUES (NULL,1,5);
- INSERT INTO us_pro VALUES (NULL,1,6);
- INSERT INTO us_pro VALUES (NULL,1,7);
- INSERT INTO us_pro VALUES (NULL,2,1);
- INSERT INTO us_pro VALUES (NULL,2,2);
- INSERT INTO us_pro VALUES (NULL,2,3);
- INSERT INTO us_pro VALUES (NULL,2,4);
- INSERT INTO us_pro VALUES (NULL,2,5);
- INSERT INTO us_pro VALUES (NULL,2,6);
- INSERT INTO us_pro VALUES (NULL,2,7);
- INSERT INTO us_pro VALUES (NULL,3,1);
- INSERT INTO us_pro VALUES (NULL,3,2);
- INSERT INTO us_pro VALUES (NULL,3,3);
- INSERT INTO us_pro VALUES (NULL,3,4);
- INSERT INTO us_pro VALUES (NULL,3,5);
- INSERT INTO us_pro VALUES (NULL,3,6);
- INSERT INTO us_pro VALUES (NULL,3,7);
- INSERT INTO us_pro VALUES (NULL,4,1);
- INSERT INTO us_pro VALUES (NULL,4,2);
- INSERT INTO us_pro VALUES (NULL,4,3);
- INSERT INTO us_pro VALUES (NULL,4,4);
- INSERT INTO us_pro VALUES (NULL,4,5);
- INSERT INTO us_pro VALUES (NULL,4,6);
- INSERT INTO us_pro VALUES (NULL,4,7);
- -- 显式内连接
- -- 查询用户信息和对应的订单信息
- SELECT * FROM user INNER JOIN orderlist ON orderlist.uid=user.id;
-
- -- 查询用户信息和对应的订单信息,起别名
- SELECT * FROM user u INNER JOIN orderlist o ON o.uid=u.id;
-
- -- 查询用户姓名,年龄和订单编号
- SELECT
- u.name, -- 用户姓名
- u.age, -- 用户年龄
- o.number -- 订单编号
- FROM
- user u -- 用户表
- INNER JOIN
- orderlist o -- 订单表
- ON
- o.uid=u.id;
- -- 隐式内连接
- -- 查询用户姓名,年龄和订单编号
- SELECT
- u.name, -- 用户姓名
- u.age, -- 用户年龄
- o.number -- 订单编号
- FROM
- user u, -- 用户表
- orderlist o -- 订单表
- WHERE
- o.uid=u.id;