基础命令
show databases; //查看数据库列表
create database shop; //创建一个名为shop的数据库
use shop; //使用shop库
//建一张名为Product的表
//包含一个id字段 字段类型为int(整数) not null表示不允许为null,promary key表示该字段为主键,auto_increment表示自动增长
create table Product(id int not null primary key auto_increment);
//添加表结构语句 添加一个名为name的 类型为varchar(字符串, 长度为100)的属性到product表中
alter table Product add column name varchar(100);
//删除表结构语句
alter table Product drop column name;
//查看Product的表结构
desc Product;
//添加一条数据小括号里写我要添加哪些字段,vulues后按照顺序写值
insert into Product(name,price,type) values("帽子",99,"服饰");
//查询Product表中全部的数据
select * from Product;
//删除Product表中id为1的数据
delete from Product where id = 1;
//修改Product表中id为2的记录的name值为拖鞋
up Product set name = "拖鞋" where id =2;
//条件查询
select * from Product where type = "服饰";
//多个条件查询
select * from Product where type = "服饰" and price >= 100;
//根据类别排序
select type from Product;
//列出表中的类别
select type from Product group by type;
select distinct type from Product;
//列出表中的类别,和该类中所包含的数量
select type,count(*) from Product group by type;
//列出Product表中的name,price元素
select name,price from Product;
//查询打八折的商品
select name,price,price * 0.8 AS price2 from Product
//将name改为Productname,并将商品打八折,定为price2
select name AS Product_name,price,price * 0.8AS price2 from Product
//查询价格大于等于100并且打八折的定为price2
select name,price,price * 0.8 AS price2 from Product where price>=100
//列出type是服饰且满足价格大于等于100并且打八折的商品定为price2
select name,price,price * 0.8 AS from Product where price>=100 and type='服饰'
//计算price的总和
SELECT SUM(price) FROM Product
//列出表中price的最大值,和最小值
SELECT MAX(price),MIN(price) FROM Product;
//计算表中price的平均值
SELECT AVG (price) FROM Product