02、mysql单表的CRUD

来源:互联网 发布:adidas旗舰店淘宝真假 编辑:程序博客网 时间:2024/06/08 09:54

– 操作数据库:
– 创建数据库
create database test;
– 删除数据库
drop database test;
– 查看所有的数据库
show databases;
– 操作表(表结构本身)
– 创建表
create table user(
id int PRIMARY key auto_increment,
username VARCHAR(20)
);
– 显示表结构
desc user;
– 查看所有的表
show TABLES;
– 修改表名称
alter table user rename newuser;
alter table newuser rename user;
– 修改表 给表添加字段
alter table user add password VARCHAR(20);
– 修改表 修改表字段名称
alter table user change password pwd VARCHAR(20);
– 修改字段描述
alter table user modify pwd int;
– 删除字段
alter table user drop pwd;
– 删除表
drop table user;
– 查看建表语句
show create table user;
– 操作表(内容)
– 向表中插入数据
insert into user values(1,’tom’,’123456’);
– 查询所有的数据
select * from user;
– 向表中插入数据(values后面的字段和前面user字段保持一致)
insert into user(username,password) values(‘jack’,’123456’);
– 修改表中的内容
update user set password=’123’ where username=’jack’;
– 删除表中的内容
delete from user where id=2;
– 查询表(内容)
– 格式:select … from 表名 where 条件 grop by 分组字段 having 条件 order by 排序字段 ase|desc
– 创建商品表
create table products(
pid int primary key auto_increment,
pname VARCHAR(20),
price double,
pnum int,
cno int,
pdate TIMESTAMP
);
– 插入数据(TIMESTAMP:时间戳默认时间是当前时间)
insert into products values (1,’泰国大榴莲’,98,12,1,null);
insert into products values (2,’新疆大枣’,38,123,1,null);
insert into products values (3,’新疆切糕’,68,50,2,null);
insert into products values (4,’十三香’,10,200,3,null);
insert into products values (5,’老干妈’,20,180,3,null);
insert into products values (6,’豌豆黄’,20,120,2,null);
– 简单查询
– 查询所有的商品
select * from products;
– 查询商品名称和商品价格
select pname,price from products;
– 查询所有商品都有哪些价格(distinct:去重)
select price from products;
select distinct price from products;
– 查询将所有商品的价格+10元进行显示.(别名)
select price from products;
select price+10 from products;
select price+10 as newprice from products;
– 条件查询
– 查询商品名称为十三香的商品所有信息:
select * from products where pname=’十三香’;
– 查询商品价格>60元的所有的商品信息:
select * from products where price>60;
– 查询商品名称中包含”新”的商品
select * from products where pname like ‘%新%’;
– – 匹配内容 %
“龙” 值为龙
“%龙” 值以”龙”结尾
“龙%” 值以”龙”开头
“%龙%” 值包含”龙”
– 匹配个数 “__” 占两个位置
– 查询价格为38,68,98的商品
select * from products where price = 38 or price = 68 or price=98;
select * from products where price in(38,68,98);
– 查询商品价格在50和70之间的商品信息
select * from products where price between 50 and 70;
– 排序查询
– 查询所有的商品,按价格进行排序.(asc-升序,desc-降序)
select * from products order by price desc;
– 查询名称有新的商品的信息并且按价格降序排序
select * from products where pname like ‘%新%’ order by price desc;
– 聚合函数
–所谓聚合函数:就是对一列进行计算返回值是一个忽略null值
–常用聚合函数有:sum(),avg(),max(),min(),count();
– 获得所有商品的价格的总和
select sum(price) from products;
– 获得商品表中价格的平均数
select avg(price) from products;
– 获得商品表中价格的平均数(保留2位小数)
select round(avg(price),2) from products;
– 获得商品表中有多少条记录:
select count(*) from products;
select count(1) from products;
– 分组查询
– 根据cno字段分组,分组后统计商品的个数.
select cno,count(*) from products group by cno;
– 根据cno分组,分组统计每组商品的总数量,并且总数量> 200;
select cno,sum(pnum) from products group by cno;
select cno,sum(pnum) from products group by cno having sum(pnum)>200;
– 注意:
where和having区别:
1.where 是对分组前的数据进行过滤 ;having 是对分组后的数据进行过滤
2.where 后面不能使用聚合函数,having可以
– mysql四种约束:1.主键约束(primary key):唯一且非空。2.外键约束(foreign key)。3.唯一约束(unique)。4.非空约束(not null)
– truncate 表名; 干掉表,重新创建一张空表
– delete 删除表中的记录

原创粉丝点击