创建数据库:
create database 数据库的名字
--创建数据库的时候,指定字符集
create database 数据库的名字 character set 字符集;
create database 数据库的名字 character set 字符集 collate 校对规则;
--查看数据库定义的语句
show create database 数据库的名字
--查看所有数据库
show databases;
--修改数据库的操作
--修改数据的字符集
alter database 数据库的名字 character set 字符集
删除数据库
drop database 数据库名字;
查看一下当前正在使用的数据库:
select database();
表的CRUD操作:
创建表:
create table 表名(
列名 列的类型 约束,
列名2 列的类型 约束
);
列的类型:int、char、varchar、double、float、boolean、date、time、datetime、timestamp、text、blob
列的约束:
create table student(
sid int primary key,
sname varchar(31),
sex int,
age int
);
主键约束:primary key
唯一约束:unique
非空约束:not null
查看表
-- 查看所有的表
show tables;
-- 查看表的创建过程
show create table student;
--查看表结构
desc student;
修改表:
添加列(add)
alter table 表名 add 列名 列的类型 列的约束
修改列(modify)
alter table student modify sex varchar(2);
修改列名(change)
alter table stduent change sex gender varchar(2);
删除列(drop):
alter table student drop chengji;
Sql完成对表中数据的CRUD的操作:
插入数据:
insert into 表名 (列名1,列名2,列名3) values(值1,值2,值3);
--简单写法
insert into student values(1,'zhangsan',1,23);
insert into student(sid,sname) values(3,'lisi');
--批量插入(效率高)
insert into student values
(4,'zhangsan',1,23),
(5,'zhangsan',1,23),
(6,'zhangsan',1,23);
--查看表中数据
select * from student;
删除记录:
delete from 表名 [where 条件]
delete from student where sid=10;
delete删除数据 和 truncate删除数据有什么差别?
delete:DML一条一条删除表中的数据
truncate: DDL 先删除表再重建表,
关于哪条执行效率高:具体要看表中的数据量,如果数据比较少,delete比较高效,如果是数据比较多,truncate比较高效
更新表记录
update 表名 set 列名=列的值,列名2=列的值2 [where 条件]
--将sid为5的名字改成李四
--如果参数是字符串,日期要加上单引号
update student set sname='李四' where sid = 5;
查询记录:
select [distinct] [*] [列名,列名2] from 表名 [where 条件]
distinct:去除重复的数据
select:选择显示哪些列的内容
--商品分类
1.分类的ID
2.分类名称
3.分类描述
create table category(
cid int primary key auto_increment,
cname varchar(10),
cdesc varchar(31)
);
insert into category values(null,'手机数码','电子产品,生产');
select * from category;
select cname,cdesc from category;
--所有商品
1.商品ID
2.商品名称
3.商品的价格
4.生产日期
5.商品分类ID
商品和商品分类:所属关系
create table product(
pid int primary key auto_increment,
pname varchar(10),
price double,
pdate timestamp,
cno int
);
insert into product values(null,'小米mix4',998,null,1);
--简单查询:
--查询所有的商品:
select * from product;
--查询商品名称和商品价格:
select pname,price from product;
--别名查询.as的关键字,as关键字是可以省略
--表别名:select p.pname ,p.price from product p;(主要是用在多表查询);
--列别名:select pname as 商品名称,price as 商品价格 from product;
--去掉重复的值
--查询商品所有的价格
select price from product;
select distinct price from product;
--select运算查询:仅仅在查询结果上做了运算
select *,price*1.5 from product;
select *,price*1.5 as 折后价 from product;
--条件查询[where关键字]
指定条件,确定要操作的记录
--查询商品价格>60元的所有商品信息
select * from product where price > 60;
--where后的条件写法
--关系运算符:> >= < <= = != <>
<>:不等于 :标准SQL语法
!=:不等于:非标准SQL语法
--查询商品价格不等于88的所有商品
select * from product where price <> 88;
--查询商品价格在10到100之间
select * from product where price > 10 and price < 100;
between...and...
select * from product where price between 10 and 100;
--逻辑运算:and,or,not
--查询出商品价格 小于100 或者商品价格 大于900
select * from product where price <100 or price >900;
--like:模糊查询
_:代表的是一个字符
%:代表的是多个字符
--查询出名字中带有饼的所有商品 '%饼%'
select * from product where pname like '%饼';
--in 在某个范围中获得值
--查询出商品分类ID在 1,4,5里面的所有商品
select * from product where cno in (1,4,5);
--排序查询:order by 关键字
asc:ascend 升序(默认的排序方式)
desc:descend 降序
select * from product order by price;
select * from product order by price desc;
select * from product where pname like '%小' order by price asc;
--聚合函数:
sum():求和
avg():求平均值
count():统计数量
max():最大值
min():最小值
select sum(price) from product;
select avg(price) from product;
select count(*) from product
--注意:where 条件后面不能接聚合函数
select * from product where price > (select avg(price) from product);
--分组:group by
--1.根据cno字段分组,分组后统计商品的个数
select cno,count(*) from product group by cno;
--2.根据cno分组,分组统计每组商品的平均价格 并且商品平均价格 > 60
select cno,avg(price) from product group by cno having avg(price) > 60;
--having 关键字 可以接聚合函数的 出现在分组之后
--where 关键字 它是不可以接聚合函数,出现在分组之前