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的 类型为verchr(字符串,长度为100)的属性到product表中
alter table Product add column name varchar(100);
//删除表结构语句
alter table Product drop column name;
//查看product的表结构
desc Product;
//添加一条数据小括号里写我要添加那些字段,values后按照顺序写值
insert into Product (name,project,type) values("帽子" 99 "服饰");
//查询Product表中全部的数据
select * from Product;
//删除Product表中id为1的数据
delet from Product where id=1;
//修改Product表中id为2的记录的name值为拖鞋
update Product set name="拖鞋"where id=2;
//条件查询
select * from Product where type="服饰";
//多个条件查询
select * from Product where type="服饰" and price >=100;
//只查询name,price
select name,price from Product;
//分组查询
select type from Product group by type;
//去重查询
select distinct type from Product;
//分组查询,统计有多少条
select type,count(*) from Product group by type;
//打八折,AS改名字
select name,price,price * 0.8 AS price2 from Product;
//大于300打八折
select name,price,price * 0.8 AS price2 from Product where price > 300;
//大于300打八折
select name,price,price * 0.8 AS price2 from Product where price <= 300 and type='衣服';
//求和
select sum(price) from Product;
//平均值
select avg(price) from Product;
//最大值
select max(price) from Product;
//最大值最小值
select max(price),min(price) from Product;
//开始一条事故
start transaction;
//结束事故
commit;