create database db_name charset utf8;#創建數據庫
drop database db_name;#刪除數據庫
use db_name;#切換數據庫
show databases;#查看所有數據庫
表操作:
創建表:
數據類型:
整形:
tinyint smallint int bigint
浮點型:
float double
字符串:
char varchar text
日期類型:
date datetime timestamp
約束:
1、主鍵約束 唯一、非空 primary key
2、外鍵約束 foreign key
3、唯一約束 unique
4、非空約束 not null
5、默認值約束 default
6、自增長 auto_increment
DDL、DCL、DML語句:
DDL:
數據定義語句 create、drop、alter
DML:
數據操縱語句 select、insert、update、delete
DCL:
數據控制語句 grant
建表
create table student (
id int primary key auto_increment,
name varchar(20) not null ,
phone varchar(11) unique not null,
sex tinyint default 0,
addr varchar(50),
brith datetime default current_timestamp,
index(name)
);
create table score (
id int primary key auto_increment,
score float not null,
sid int not null
);
create table student_new like student; -- 快速創建一個和student表結構一樣的表
修改表:
alter table student add class2 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 table student;
清空表:
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 * from student;
select id,name,addr from student; --指定字段
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
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子句
limit
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) from score;
select max(score) from score;
子查詢
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;
授權
GRANT ALL privileges ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456';
GRANT ALL privileges ON byz.* TO 'byz'@'%' IDENTIFIED BY '123456';
flush privileges;