--创建一个班级表classes
USE 1909e
CREATE TABLE classes (id int , name VARCHAR(20))
--创建一个学生表students
USE 1909e
CREATE TABLE students (id INT,name VARCHAR(20),birthdays date,
gender CHAR(2),score INT,class_id INT)
--在班级表中添加三条记录
INSERT INTO classes (id,name) VALUES (1,"一年级"),(2,"二年级"),(3,"三年级")
--在学生表里添加六条记录
INSERT INTO students (id,name,birthdays,gender,score,class_id)VALUES(1,"张三","2010-11-10","男",100,1),
(2,"李四","2012-11-10","女",75,1),(3,"王五","2014-11-10","男",80,2),
(4,"小明","2013-11-10","女",60,2),(5,"小李","2015-11-10","女",30,2),
(6,"小刘","2008-11-10","男",90,3)
--修改张三的名字为张五
UPDATE students set name="张五" where id=1
--删除班级名字为三年级的所有学生
DELETE FROM students WHERE class_id=3
--查询一年级分数最高的人
SELECT MAX(score) FROM students
--查询以小字开头的名字的学生
SELECT * from students WHERE name LIKE '小%'
--查询出生日期在2008-11-10到2012-11-10的学生
SELECT * from students where birthdays BETWEEN '2008-11-10' AND'2012-11-10'
--创建一个分类表cate,包含字段id,name
CREATE TABLE cate (id INT,name VARCHAR(20))
--创建一个商品表goods,包含字段id,name,number,create_time,price,cate_id
CREATE TABLE goods (id INT,name VARCHAR(20),number INT,create_time date,price DOUBLE,cate_id INT)
--在分类表中添加三条记录
INSERT into cate (id,name)VALUES (1,"服装"),(2,"玩具"),(3,"家电")
--在商品表里添加六条记录
INSERT into goods (id,name,number,create_time,price,cate_id) VALUES (1,"羽绒服",10,"2010-11-10",1.00,1),
(2,"轻便服",30,"2012-11-10",2.00,1),(3,"汽车",40,"2014-11-10",2.00,2),
(4,"公主",50,"2013-11-10",6.00,2),(5,"橡皮泥",23,"2015-11-10",3.50,2),
(6,"电饭煲",3,"2008-11-10",3.00,3)
--把名字为羽绒服修改成棉衣
UPDATE goods set name="棉衣" WHERE name="羽绒服"
--删除名字为电饭煲的记录
DELETE FROM goods WHERE name="电饭煲"
--查询以服结束的商品信息
SELECT * from goods WHERE name LIKE '%服'
--查询创建日期在2008-11-10到2012-11-10的之间的商品
SELECT * from goods where create_time BETWEEN '2008-11-10' AND'2012-11-10'
--创建一个数据库cart
CREATE DATABASE cart
--使用新创建的数据库建立两张表,用户表users:id 姓名 身高,购物车表user_cart
USE cart
CREATE TABLE users (id int,name VARCHAR(20),gao DOUBLE)
CREATE TABLE user_cart (id INT,name VARCHAR(20),shu int,price int,shan int,user_id INT)
--向用户表添加记录
INSERT into users (id,name,gao) VALUES (1,"张三",1.80),(2,"李四",1.75),(3,"小明",1.34)
--在购物车表中添加记录
INSERT INTO user_cart (id,name,shu,price,shan,user_id) VALUES (1,"商品一",10,100,1,1),
(2,"商品二",3,89,0,1),(3,"商品三",2,30,1,1),(4,"商品四",10,50,1,2),(5,"商品五",3,10,1,2),
(6,"商品六",1,76,1,2)
--更新李四购物车中商品五的名字为商品5
UPDATE user_cart set name="商品5" where name="商品五"
--查询购物车中删除和没删除的总价和为多少
SELECT SUM(price) from user_cart
--删除张三购物车中已删除的购物车信息
DELETE FROM user_cart WHERE shan="0"