数据库练习2

-- 创建数据库
CREATE DATABASE my2


-- 使用数据库
DROP DATABASE my2


-- 删除数据库
DROP DATABASE my2


-- 创建表
CREATE TABLE t_product(
id INT PRIMARY KEY AUTO_INCREMENT,
productName VARCHAR(50),
price DECIMAL,
factory VARCHAR(10),
createDate DATE,
keepDate INT,
type1 VARCHAR(20)


)CHARSET=utf8;


-- 删除表
DROP TABLE t_product;


-- 查询表中所有记录*表示查询所有列
SELECT*FROM t_product;


-- 查询表中所有记录,但只表示id、product两个列的值
SELECT id,studentName FROM t_product;


-- 添加
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('大蒜',8,'四川省','2015-01-22',30,'蔬菜类');
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('黄瓜',7,'四川省','2015-05-12',10,'蔬菜类');
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('扇贝',20,'东北省','2015-06-14',30,'海鲜类');
INSERT INTO t_product(productName,prict,factory,createDate,keepDate,type1) VALUES ('海蜇',30,'东北省','2015-07-11',40,'海鲜类');
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('乌贼',15,'东北省','2015-02-30',25,'海鲜类');
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('鸡蛋',4,'山东省','2015-04-15',15,'禽蛋类');
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('鸭蛋',3,'山东省','2015-07-20',3,'禽蛋类');
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('鸡肉',4,'山东省','2015-08-10',13,'禽蛋类');
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('金枪鱼',25,'吉林省','2015-06-25',100,'鱼类');
INSERT INTO t_product(productName,price,factory,createDate,keepDate,type1) VALUES ('比目鱼',50,'吉林省','2015-09-22',25,'鱼类');


-- 删除id为3的记录
DELETE FROM t_product WHERE id=3;


-- 修改商品'大蒜’价格为9元
-- p 表示给这个表取别名
-- 同时改价格和产地,中间以逗号隔开
UPDATE t_product  p SET p.price=9,factory='四川绵阳' WHERE p.productName='大蒜';


-- 所有海鲜类商品打8折
UPDATE t_product SET price=price*0.8 WHERE type1='海鲜类';


-- 所有禽蛋类商品价格上浮2%
UPDATE t_product SET price=price+(price*0.02) WHERE type1='禽蛋类';


-- 查询所有鱼类商品
SELECT*FROM t_product  WHERE type1='鱼类';


-- 查询所有保质期3个月以上的商品
SELECT*FROM t_product WHERE keepDate>3 ;


-- 查询所有山东省产的禽蛋类商品(产地模糊查询)
SELECT*FROM t_product WHERE factory='%东%' AND type1='禽蛋类';


-- 查询所有2015年9月份所产的商品
SELECT*FROM t_product WHERE createDate='2015-09';


-- 查询所有过期商品
SELECT*FROM t_product WHERE keepDate<(NEW().createDate);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值