MySQl操作整理01
- 連接數據庫:
mysql -u [useename] -p
mysql -u [username] -h [host] -p
- 用戶管理:
create user 'spring'@'192.168.1.1' identified by '123';
create user 'spring'@'192.168.1.%' identified by '123';
create user 'spring'@'%' identified by '123';
drop user 'spring'@'%';
rename user 'spring'@'%' to 'new_spring'@'%';
set password for 'spring'@'%' = password('ab123');
- 權限管理:
grant select,insert,update on db1.t1 to 'spring'@'%';
grant all privileges on db1.t1 to 'spring'@'%';
revoke all privileges on db1.t1 from 'spring'@'%';
- 數據庫操作:
create database db1 default charset=utf8;
create database db1 default charset utf8;
drop database db1;
show databases;
–
對數據表的操作:
show tables;
create table t1(
列名 類型 null,
列名 類型 not null,
列名 類型 not null auto_increment primary key,
id int,
name char(10)
)engine=innodb default charset=utf8;
# innodb 支持事物,原子性操作 ****
# myisam
not null:是否爲空
auto_increment 表示自增
primary key 主鍵表示約束(不能重複且不能爲空);加速查找;加速查找
一個表只能有一個主鍵
主鍵可以由多列組成
create table t2(
nid int(11) not null auto_increment,
pid int(11) not null,
num int(11),
primary key(nid,pid)
)engine=innodb default charset=utf8;
create table t3(
id int auto_increment primary key,
name char(10),
id1 int,
id2 int,
constraint fk_t3_t2 foreign key(id1,id2) reference t2(nid,pid)
)engine=innodb default charset=utf8;
外鍵:
create table department(
id bigint auto_increment primary key,
title char(15)
)engine=innodb default charset=utf8;
create table userinfo(
uid bigint auto_incremant peimary,
name varchar(32),
department_id int,
constraint fk_user_depar foreign key (department_id) reference color(id)
)engine=innodb default charset=utf8;
清空表:
delete from t1;
truncate table t1; #清除auto_increment自增
刪除表:
drop table t1;
修改表:
添加列: alter table 表名 add 列名 類型;
刪除列: alter table 表名 drop column 列名;
修改列:
alter table 表名 modify column 列名 類型; -- 類型
alter table 表名 change 原列名 新列名 類型;-- 列名,類型
添加主鍵:
alter table 表名 add primary key(列名)
刪除主鍵:
alter table 表名 drop primary key;
alter table 表名 modify 列名 int, drop primary key;
添加外鍵:
alter table 從表 add constraint 外鍵名(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主鍵(主鍵字段);
刪除外鍵:
alter table 表名 drop foreign key 外鍵名稱
修改默認值:
alter table 表名 auto_increment=20;
查看錶結構:
desc t1;
查看創建表信息:
show create table t1;
數據類型:
數據類型:
數字、字符串、時間類型
數字:
tinyint
int
bigint
float
double
decimal
字符串:
char(10) 速度快 (10)示10個字符
varchar(10) 節省空間
PS:創建數據表長列往前放
時間類型
datetime
enum
set
create table t1(
id int signed auto_increment primary key,
num decimal(10,5),
name char(10)
)engine=innodb default charset=utf8;
#拿tinyint字段來舉例,unsigned後,字段的取值範圍是0~255,而signed的範圍是-128~127
對數據行進行操作:
insert into t1(name,age) values('autumn',18),('spring',19);
insert into t1(name,age) select name,age from tb2;
delete from t1 where id >6;
delete from t1 where id != 6;
truncate table t1; #清除auto_increment自增
update t1 set name='root' where id=5;
update t1 set name ='autumn' where id=6 and age=18;
select * from t1;
select id,name from t1;
查詢操作
- 臨時表:
select num,course_id from (select num,course_id from score where num >60) as B;
select * from t1;
select * from t1 where id=1;
select * from t1 where id!=2;
select * from t1 where id >2;
select * from t1 where id >=2 and name='spring';
select name,age from t1;
select * from t1 where id in (1,2,3);
select * from t1 where id not in (1,2,3);
select * from t1 where id in (select id from t2);
select * from t1 where id between 5 and 12;包括2和12
通配符:
select * from t1 where name like "a%"
select * from t1 where name like "a_"
分頁:
select * from t1 limit 10;
select * from t1 limit 0,10;
select * from t1 limit 10,10;
select * from t1 limit 2 offset 4; 從第5個開始往後2個
排序:
select * from t1 order by id desc;從大到小
select * from t1 order by id asc; 從小到大
select * from t2 order by id desc, age asc;
distinct:
select DISTINCT student_id from score where num < 60
取最後5條數據
select * from t1 order by id desc limit 5;
分組:
select count(1),max(id),part_id from userinfo group by part_id;
count()
max()
min()
sum()
avg()
*******如果對聚合函數進行二次篩選時,必須使用having******
select count(1),max(id),part_id from userinfo group by part_id having count(id) > 1;
select count(id),part_id from userinfo where id >0 group by part_id having count(id) > 1;
連表操作:
select * from userinfo,department;笛卡爾基
select * from userinfo,department where userinfo.uid =department.id
select * from userinfo left join department on userinf.uid = department.id(在之前版本的myqsl中 這兩種方式是有性能差別,但是在現在版本的mysql中性能是一樣的,推薦用 left join 的方法)
select * from userinfo right join department on userinfo.uid = department.id
left:左邊表全顯示,right:右邊全顯示
select * from userinfo innder join department on userinfo.uid = department.id
將出現null的一行隱藏
select * from
department5
left join userinfo5 on userinfo5.part_id =department5.id
left join userinfo6 on userinfo5.part_id =department5.id
select
score.sid,
student.sid
from
score
left join student on score.student_id = student.sid
left join course on score.course_id = course.cid
left join class on student.class_id = class.cid
left join teacher on course.teacher_id=teacher.tid
select count(id) from userinfo;