mysql的相關操作
安裝mysql
安裝windows服務
破解密碼
統一字元編碼
基本的sql語句
- 資料庫
- show databases
- create database db1 charset gbk;
- use db1;
- drop database db1;
- alter database db1 charset utf8
- 表
- a = 2 a='2' String a = '2';
- create table t1(id int,name varchar(20) );
- alter
- drop table t1;
- select id from t1; select id,name from t1; select * from t1;
- desc t1;
- 記錄
- insert into t1(id,name) values(1,'alex');
- insert into t1 values();插入空的值
- update
- delete table t1 where id =2;
今日內容
補充內容:建立使用者和授權
1.建立使用者:
# 指定ip:192.118.1.1的mjj使用者登入
create user 'mjj'@'192.118.1.1' identified by '123';
# 指定ip:192.118.1.開頭的mjj使用者登入
create user 'mjj'@'192.118.1.%' identified by '123';
# 指定任何ip的mjj使用者登入
create user 'mjj'@'%' identified by '123';
2.刪除使用者
drop user '使用者名稱'@'IP地址';
3.修改使用者
rename user '使用者名稱'@'IP地址' to '新使用者名稱'@'IP地址';
4.修改密碼
set password for '使用者名稱'@'IP地址'=Password('新密碼');
對當前的使用者授權管理
檢視許可權
show grants for '使用者'@'IP地址'
#授權 mjj使用者僅對db1.t1檔案有查詢、插入和更新的操作
grant select ,insert,update on db1.t1 to "mjj"@'%';
# 表示有所有的許可權,除了grant這個命令,這個命令是root才有的。mjj使用者對db1下的t1檔案有任意操作
grant all privileges on db1.t1 to "mjj"@'%';
#mjj使用者對db1資料庫中的檔案執行任何操作
grant all privileges on db1.* to "mjj"@'%';
#mjj使用者對所有資料庫中檔案有任何操作
grant all privileges on *.* to "mjj"@'%';
遠端連線:
mysql -uskx -P3306 -h 192.168.15.113 -p123
複製表
#即複製表結構 又複製記錄
create table t2 select * from db1.t1;
# 只複製表結構,不復制記錄
create table t2 select * from db1.t1 where 1>3;
create table t2 like db1.t1;
資料型別
整型 預設是有符號
資料型別 無符號(unsigned)和有符號 用0填充 zerofill
約束的作用: 保證資料的完整性和一直性
- tinyint [-128~127] 小整數
- int 整數
- bigint 極大整數
create table t1(id int(4) unsigned,name char(20));
---
浮點型
- float 單精度 隨著小數位數的增多,不準確
- double 雙精度 隨著小數位數的增多.不準確,比float要準確
- decimal 小數 精準的小數
日期型別
year 年份 (1901~2155)
date 年月日
time 時分秒
datetime 年月日 時分秒
now() sql語言中自帶的內容函: 獲取當前的時間(根據資料型別)
create table t10(born_year year,intClass datetime);
字元型別
- char 定長,簡單粗暴,浪費空間,存取速度快
- varchar 變長,精準,節省空間,存取速度慢
length():檢視位元組數
char_length():檢視字元數
列舉和集合
create table consumer(
id int,
name varchar(50),
sex enum('male','female','other') default 'male',
level enum('vip1','vip2','vip3','vip4'),#在指定範圍內,多選一
fav set('play','music','read','study') #在指定範圍內,多選多
);
注意:在sql中使用tinyint(1)來表示boolean型別
完整性約束
not null 與 default
- 如果單獨設定not null 不能插入空值
- 如果即設定了not null,又指定default,可以插入空值,會走default
unique key
單列唯一
create table t4(
id int not null,
name char(20) unique
);
create table t4(
id int not null,
name char(20),
unique(name)
);
insert into t4(id,name) values(1,'alex');
insert into t4(id,name) values(1,'wusir');
多列唯一
- 只要有一列相同,不能插入
create table t5(
id int,
name char(20),
unique(id),
unique(name)
);
聯合唯一 ***
- 多列相同時,不能插入
create table t6(
id int,
name char(20),
unique(id,name)
);
應用場景: 選課系統,一個學生可以選擇多個課程,一個課程可以被多個學生選擇,
student_id course_name
100 '生物'
101 '生物'
100 '化學
primary key
化學反應: not null + unique
單列主鍵 不能為空 並且是唯一
# primary key 索引(針對於大量資料) 查詢速度要快
create table t7(
id int primary key,
name varchar(10) unique
);
create table t8(
id int not null unique,
name varchar(10) unique
);
聯合主鍵
create table t9(
id int,
name varchar(10),
primary key(id,name)
);
auto_increment
create table student(
id int primary key auto_increment,
name varchar(20) not null,
sex enum('male','female') default 'male',
ip varchar(20) unique
);
insert into student(name,sex,ip) values ('alex','female','127.0.0.5'),('wusir','male','173.45.32.1');
*清空表區分delete和truncate的區別:*
delete from t1; #如果有自增id,新增的資料,仍然是以刪除前的最後一樣作為起始。
truncate table t1;資料量大,刪除速度比上一條快,且直接從零開始。
foreign key ***
外來鍵
# 先建立主表
create table dep(
id int primary key auto_increment,
name char(10) unique,
dep_desc varchar(50) not null
);
# 校區表
create table school(
id int primary key auto_increment,
addr varchar not null
)
# 建立從表
create table emp(
eid int primary key auto_increment,
name char(10) not null,
age int not null,
dep_id int,
school_id int,
constraint fk_dep foreign key(dep_id) references dep(id)
on delete cascade
on update cascade,
constraint fk_school foreign key(school_id) references school(id)
on delete cascade
on update cascade,
);
insert into dep(name,dep_desc) values('校長部','校長管理有限部門'),('公關部','公關管理有限部門'),('IT部門','IT技術有限部門'),('財務部','管錢很多部門');
insert into emp(name,age,dep_id)
values
('alex',18,1),
('wusir',30,2),
('吳老闆',20,3),
('馬老闆',18,4),
('邱老闆',20,2),
('女神',16,3);