mysql -u root -p
select now();
exit();
show databases;
create database test charset=utf8;
drop database test;
use test;
select database();
show tables;
create table students(
id int unsigned not null primary key auto_increment,
name varchar(20) not null,
gender enum("男","女"),
height decimal(3,2),
age tinyint default 0
);
##### 常见数据类型:
整数:int,bit
小数:decimal
字符串:varchar,char
日期时间: date, time, datetime
枚举类型:enum
##### 常见的约束:
主键 primary key: 物理上存储的顺序. MySQL 建议所有表的主键字段都叫 id, 类型为 int unsigned
非空 not null: 此字段不允许填写空值.
惟一 unique: 此字段的值不允许重复.
默认 default: 当不填写字段对应的值会使用默认值,如果填写时以填写为准.
外键 foreign key: 对关系字段进行约束, 当为关系字段填写值时, 会到关联的表中查询此值是否存在, 如果存在则填写成功, 如果不存在则填写失败并抛出异常.
drop table students;
alter table students add birthday datatime;
alter table students modify birthday data;
alter table students change birthday birth datatime not null;
alter table students drop birth;
desc students;
select * from students;
select name,age from students;
insert into students values(0,"小明","男",1.75,18);
insert into students(name,gender,age) values("小红","女","17");
insert into students values (0,"小张","男",1.75,18),(null,"小王","男",1.68,17);
insert into students(name,gender,age) values("小花","女","17"),("小黑","男","19");
update students set name = "张三" where id = 2;
-- 永久删除:
delete from students where id = 1;
-- 标记式删除:
alter table students add is_delete bit not null default 0;
update students set is_delete = 1 where id = 2;
select name as 姓名 from students;
select distinct name from students;
--查询年龄大于18的学生
select * from students where age>18;
--查询id在2和4之间的学生(包含)
select * from students where id between 2 and 4;
--查询id在(3,5,7)内的学生
select * from students where id in (3,5,7);
--查询姓名最后一个字为"红"的学生
select * from students where name like ‘%红‘;
--查询名字是两个字且以"小"开头的学生
select * from students where name like ‘小_‘;
--查询未删除男生信息,按学号降序
select * from students where is_delete = 0 and gender = ‘男‘ order by id desc;
--查询前3行男生信息
select * from students where gender = ‘男‘ limit 3;
select count(*) from students;
-- 查询男生的编号最大值
select max(id) from students where gender="男";
-- 查询男生的编号最小值
select min(id) from students where gender="男";
select avg(height) from students where gender="女";
select gender from students group by gender;
select gender,name from students group by gender,name;
-- group_concat显示每个分组的指定字段的信息
select gender,group_concat(name) from students group by gender;
select gender,round(avg(age),2) from students group by gender;
select gender, count(*) from students group by gender having count(*) > 2;
select gender, group_concat(age) from students group by gender with rollup;
inner/left/right on :连接查询关键字
on:连接查询条件
先创建班级表:
create table classes(id int unsigned not null primary key auto_increment,name varchar(20) not null);
插入3条班级数据:
insert into classes values(0,"一班"),(null,"二班"),(null,"三班");
students 表增加c_id字段,并赋值
select * from students as s inner join classes as c on s.c_id=c.id
select * from students as s right join classes as c on s.c_id = c.id
select * from students as s right join classes as c on s.c_id = c.id
select c.id, c.title, c.pid, p.title from areas c inner join areas p on c.pid = p.id;
在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句,外部那个select语句则称为主查询.
子查询是可以独立使用的查询语句,辅助主查询充当数据源.
select * from students where age > (select avg(age) from students);
select * from students where (age,height) = (select max(age),max(height) from students);
create table school(
id int not null primary key auto_increment,
name varchar(30)
);
create table teacher(
id int not null primary key auto_increment,
name varchar(30),
s_id int,
foreign key (s_id) references school(id)
);
alter table students add foreign key (c_id) references classes(id);
alter table drop foreign key teacher_ibfk_1;
(*外键名使用"show create table teacher"命令查找*)
已存在商品表goods,包含字段:种类cate_name,商品名name,价格price
-- 先创建单独的商品分类表,仅包含id和商品名name
create table goods_cate(
id int not null primary key auto_increment, name varchar(30) not null
);
-- 查询goods表中商品的分类信息
select cate_name from goods group by cate_name;
-- 将查询结果插入到good_cates表中
insert into goods_cate(name) select cate_name from goods group by cate_name;
-- 查看goods表中的商品分类名称对应的商品分类id
select * from goods g inner join goods_cate gc on g.cate_name=gc.name;
-- 将goods表中的分类名称更改成商品分类表中对应的分类id,连接更新表中的某个字段
updata goods inner join goods_cate on goods.cate_name=goods_cate.name set goods.cate_name=goods_cate.id;
原文:https://www.cnblogs.com/jcydd/p/11476931.html