根据如下要求,完成对应的SQL语句,将SQL语句拷贝到答题区域中。 1. 创建和使用shops数据库,在shops数据库中创建数据表,数据表结构如下图所 商品分类(category)表结构 Field Type Null id int(11) NO name / varchar(20 NO Key PRI UNI Default Extra null auto increment null 商品信息(goods)表结构 Field Type Null 10 (int(11) INO name / varchar(20 NO Kev PRI UNI Default Extra null auto increment null cid int(11) NO null price float YES null stock int(11) YES null 2.按照数据兴型和约束条件要求,在表中插入数据,如下 商品分类信息表 id和name 3,以分类为主,查询每个商品的分关id. 分类名称、商品id 商品名称,商品价格和商品库存 4. 查询女装和男装中,库存大于15的商品信息。
时间: 2024-02-27 22:56:41 浏览: 113
TMS320F28xSerialPeripheralInterface.rar_The Shops
1. 创建数据库及数据表的SQL语句:
```sql
CREATE DATABASE shops;
USE shops;
CREATE TABLE category (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY name (name)
);
CREATE TABLE goods (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
cid INT(11) NOT NULL,
price FLOAT,
stock INT(11),
PRIMARY KEY (id),
UNIQUE KEY name (name),
CONSTRAINT fk_goods_category FOREIGN KEY (cid) REFERENCES category(id)
);
```
2. 插入数据的SQL语句:
```sql
INSERT INTO category (name) VALUES ('女装');
INSERT INTO category (name) VALUES ('男装');
INSERT INTO category (name) VALUES ('鞋靴');
INSERT INTO goods (name, cid, price, stock) VALUES ('连衣裙', 1, 199.9, 20);
INSERT INTO goods (name, cid, price, stock) VALUES ('T恤', 2, 59.9, 30);
INSERT INTO goods (name, cid, price, stock) VALUES ('衬衫', 2, 99.9, 10);
INSERT INTO goods (name, cid, price, stock) VALUES ('运动鞋', 3, 299.9, 5);
INSERT INTO goods (name, cid, price, stock) VALUES ('皮鞋', 3, 399.9, 15);
```
3. 查询每个商品的分类ID、分类名称、商品ID、商品名称、商品价格和商品库存的SQL语句:
```sql
SELECT c.id AS '分类ID', c.name AS '分类名称', g.id AS '商品ID', g.name AS '商品名称', g.price AS '商品价格', g.stock AS '商品库存'
FROM category c
JOIN goods g ON c.id = g.cid;
```
4. 查询女装和男装中,库存大于15的商品信息的SQL语句:
```sql
SELECT g.id AS '商品ID', g.name AS '商品名称', g.price AS '商品价格', g.stock AS '商品库存'
FROM category c
JOIN goods g ON c.id = g.cid
WHERE c.name IN ('女装', '男装') AND g.stock > 15;
```
阅读全文