create tablebrf_student (
idint primary keyauto_increment,
namevarchar(20) not null ,——()内表示指定的长度,notnull表示不可以为空
phonevarchar(11) unique not null,——unique表示是唯一的
sextinyint default 0,——default 0,表示默认是0
namevarchar(20) not null,
addrvarchar(30) ,
brithdatetime default current_timestamp,index(name) ,key(name)——加上索引,相当于字典的目录
);
——int、float不需要指定字长,varchar需要指定字长create tablebrf_score (
idint primary keyauto_increment,
scorefloat not null,
sidint not null——sid是这个表的外键,加上外键就可以和其他的表关联起来了,也就是上个表的学生id
);create table student_new like student; --快速创建一个和student表结构一样的表
修改表:alter table student(表名) add class2 before(指定加到哪个字段前面) addr int not null; --增加字段
alter table student drop addr; --删除字段
alter table student change name new_name varchar(20) not null; --修改字段
alter table student modify name varchar(30) ;
删除表:drop tablestudent;
清空表:truncate table student; --自增长id会重新开始
其他操作:
show tables;--查看当前所有表
show create table student; --查看建表语句
desc student; --查看表结构
数据操作:
增:insert into student values ('','python','11111111111',0,'北京','2019-01-03 18:39:23'); --写全
insert into student (name,phone) values ('mysql','12345678901'); --指定字段
insert into student (name,phone) values ('mysql1','12345678902'),('mysql2','22345678901'); --多条
insert into student_new select * from student; --把一个表的数据快速导出到另外一个表
修改:update student set name='mysql3' ; --修改全表数据
update student set name'mysql2',sex=1; --修改多个字段
update student set name='mysql3' where id = 1; #指定修改某条数据
删除:delete from student; --整表数据删除
delete from student where id = 3; --指定数据删除
查询:
基本查询select * fromstudent;select id,name,addr from student; --指定字段【id、name等是指定查询的字段】
【指定范围】
第一种‘in’方法:select * id,name from student where id in('xiaohei',1,2,4,5);select * id,name from student where id not in('xiaohei',1,2,4,5); ——【同Python,可以 not in】
第二种‘BETWEEN’方法:select id,username from app_myuser BETWEEN 80 and 90 and sex= 0;——可以and加多个条件select id as 编号, addr 地址 , name 姓名 from student; --字段加别名
where条件select * from student where id=1; --where条件 >,=,<=,!=,<>也是不等于
select * from student where id in (1,2,3) and id != 5; --in和and条件
select * from student where id between 1 and 5; --范围
select * from student where id between 1 and 5 or id > 10; --or
排序select * from student where id between 1 and 5 order by id desc; --一起用的话,order by必须写在where条件后面
select * from student order by id desc ; --降序排序,按照id,升序的话是asc【不加desc默认是升序,加上desc就是降序】——可多个字段
select * from student order by id,name asc ; --升序,按照id和name排序,asc可以省略不写
分组select * from student group by sex; --按照某个字段分组,可以写多个字段
select * from student group by sex having addr !='北京';select * from student where id >5 group by sex having addr !='北京'; --如果有where条件,必须写在group by前面,group by后面不能再写where条件,如果有条件必须用having子句
limitselect * from limit 5;——limit后跟多少就是要查询多少条数据select * from limit 2,5;——2和5:表示从第2行开始查询5条数据select id as 编号, addr 地址 , name 姓名 from student limit 2; --前N条数据
select id as 编号, addr 地址 , name 姓名 from student limit 1,5; --从第一行开始,向后取5行,不包含第一行的数据
select * from student where id >0 group by sex having addr !='北京' limit 5; --limit必须写在最后面
select * from student where id >0 group by sex having addr !='北京' order by id desc limit 5; --limit必须写在最后面
#如果一个sql里面有where、group by、排序、limit,顺序一定是1、where 2、group by 3、order by 4、limit
聚合函数select count(*) from student; --多少条数据
select count(addr) from student; --某个字段不为空的有多少条
select count(*) 人数 ,sex 性别 from student group by sex; --多少条数据
select avg(age) from student; --平均值
select sum(score) from score; --和
select min(score) fromscore;select max(score) fromscore;
子查询,一个字段的结果是从另外一个sql语句中查询出来的:select * from student where id in (select sid from score where score >= 60);
多表查询select * from student a ,score b where a.id = b.sid and a.score>90;select a.name,b.score,a.class2 from student a ,score b where a.id = b.sid and a.score>90;select a.name ,b.score,a.class2 from student a inner join score b on a.id = b.sid where a.score > 90;select a.name ,b.score,a.class2 from student a left join score b on a.id = b.sid where a.score > 90;
授权【给数据库加用户,设置权限】时:
(1)GRANT ALL privileges ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456';
【ALL 也可以是select insert delete,是select只会有select权限,ALL是有所有的权限】
——第一个* 代表哪个数据库,如是 *代表是授权可以操作所有的,后边的是可以操作哪些表,'root'是代表加了一个什么用户,'localhost'是一个IP地址,可以限制IP地址登录,
写了某个IP就是只可以某个IP登录,'localhost'代表只可以本机登录,远程登录不了,BY'123456'是密码。
(2)GRANT ALL privileges ON byz.* TO 'byz'@'%' IDENTIFIED BY '123456';【指定 某个值 操作 某个数据库】
——byz代表指定的某个数据库,* 代表某个表,如是*则查看全部,‘byz'就是个表名,%代表所有的IP都可以,也可以指定
(3)flush privileges; 【刷新下权限】