1.MySQL數(shù)據(jù)庫管理
1.1 常用的數(shù)據(jù)類型
1.2 char和varchar區(qū)別
CHAR和VARCHAR類型類似,但它們保存和檢索的方式不同。它們的最大長度和是否尾部空格被保留等方面也不同,在存儲或檢索過程中不進(jìn)行大小寫轉(zhuǎn)換。
下表顯示了將各種字符串值保存到CHAR(4)和VARCHAR(4)列后的結(jié)果,說明了CHAR和VARCHAR之間的差別:
字節(jié)大小
- char無論是否有值,都會占用固定長度的字節(jié)大小,保存在磁盤上都是4字節(jié)。
- varchar在保存字符時,默認(rèn)會加一個隱藏的結(jié)束符,因此結(jié)束符會多算一個字節(jié)。
優(yōu)劣比較
- varchar比char節(jié)省磁盤空間。
- 但varchar類型的數(shù)據(jù)讀寫速度比char慢,因為char是連續(xù)的磁盤空間,·而varchar在多次增刪改查中會產(chǎn)生一些磁盤空間碎片。
1.3 SQL語句分類
SQL語句用于維護(hù)管理數(shù)據(jù)庫,包括數(shù)據(jù)查詢、數(shù)據(jù)更新、訪問控制、對象管理等功能。
2.數(shù)據(jù)表高級操作
2.1 克隆表
(1)克隆表,將數(shù)據(jù)表的數(shù)據(jù)記錄生成到新的表中
方法一:
create table test01 like KY08; #通過LIKE方法,復(fù)制KY08表結(jié)構(gòu)生成test01 表
insert into test01 select * from KY08;
#此方法能保證新表的表結(jié)構(gòu)、表數(shù)據(jù)跟舊表都是一致的
方法二:
CREATE TABLE test02 (SELECT * from KY08);
#此方法創(chuàng)建的新表的表數(shù)據(jù)和舊表是一樣的,但可能會出現(xiàn)新表的表結(jié)構(gòu)和舊表的不一致
show create table test02\G #獲取數(shù)據(jù)表的表結(jié)構(gòu)、索引等信息
SELECT * from test02;
2.2 清空表
(2)清空表,刪除表內(nèi)的所有數(shù)據(jù)
方法一:
delete from test01;
#DELETE清空表后,返回的結(jié)果內(nèi)有刪除的記錄條目;DELETE工作時是一行一行的刪除記錄數(shù)據(jù)的;如果表中有自增長字段,使用DELETE FROM刪除所有記錄后,再次新添加的記錄會從原來最大的記錄ID后面繼續(xù)自增寫入記錄。
方法二:
truncate table test01;
#TRUNCATE 清空表后,沒有返回被刪除的條目;TRUNCATE 工作時是將表結(jié)構(gòu)按原樣重新建立,因此在速度上 TRUNCATE 會比 DELETE 清空表快;使用 TRUNCATE TABLE 清空表內(nèi)數(shù)據(jù)后,ID 會從 1 開始重新記錄。
2.3 創(chuàng)建臨時表
(3)temporary關(guān)鍵字,創(chuàng)建臨時表
#臨時表創(chuàng)建成功之后,使用SHOW TABLES命令是看不到創(chuàng)建的臨時表的,臨時表會在連接退出后被銷毀。 如果在退出連接之前,也可以可執(zhí)行增刪改查等操作,比如使用 DROP TABLE 語句手動直接刪除臨時表。
CREATE TEMPORARY TABLE 表名 (字段1 數(shù)據(jù)類型,字段2 數(shù)據(jù)類型[,...][,PRIMARY KEY (主鍵名)]);
例:
create temporary table test03 (
id int(4) zerofill primary key auto_increment,
name varchar(10) not null,
cardid int(18) not null unique key,
hobby varchar(50));
insert into test03 values(1,'zhangsan',123456,'running');
select * from test03;
show tables;
quit
select * from test03;
3.MySQL的六大約束
參數(shù) | 含義 |
---|---|
primary key | 表示此字段的主鍵約束,此字段數(shù)據(jù)不能重復(fù),不能為null。一個表中只能由要給主鍵 |
unique key | 表示此字段唯一鍵約束,此字段數(shù)據(jù)不可以重復(fù)。一張表中只能有一個主鍵,但是一張表中可以有多個唯一鍵 |
not nul | 表示此字段為非空約束,此字段的值不允許為NULL |
default | 默認(rèn)值約束 字段的值如果沒有設(shè)置則使用默認(rèn)值自動填充 |
auto_increment | 表示此字段為自增長字段(自增約束),即每條記錄自動遞增1,默認(rèn)從1開始遞增;自增長字段數(shù)據(jù)不可以重復(fù);自增長字段必須是主鍵;如添加的記錄數(shù)據(jù)沒有指定此字段的值且添加失敗也會自動遞增一次 |
foregin key | 表示此字段為外鍵約束,保證相關(guān)的表的數(shù)據(jù)的完整性和一致性 |
use mybl;
create table if not exists info ( #表示檢測要創(chuàng)建的表是否已存在,如果不存在就繼續(xù)創(chuàng)建。
id int(4) zerofill primary key auto_increment, #指定主鍵的第二種方式。其中int(4) zerofill表示若數(shù)值不滿4位數(shù),則前面用"0"填充,例如0001。
name varchar(10) not null default '匿名',
cardid int(18) not null unique key,
hobby varchar(50));
注意:
int(N) 需要和zerofill primary配合使用,否則N的值沒有意義。
4.外鍵約束
4.1 外鍵概述
創(chuàng)建外鍵約束,保證數(shù)據(jù)的完整性和一致性。
外鍵的定義:
如果同一個屬性字段X在表一中是主鍵,而在表二中不是主鍵,則字段X稱為表二的外鍵。
主鍵表和外鍵表的理解:
(1)以公共關(guān)鍵字作主鍵的表為主鍵表(父表、主表)
(2)以公共關(guān)鍵字作外鍵的表為外鍵表(從表、外表)
**注意:**
與外鍵關(guān)聯(lián)的主表的字段必須設(shè)置為主鍵。要求從表不能是臨時表,主從表的字段具備相同的數(shù)據(jù)類型、字符長度和約束。
4.2 創(chuàng)建主從表
#創(chuàng)建主表 profession
create table profession (pid int,pname char(6));
#創(chuàng)建從表 student
create table student (sid int,sname varchar(10),age int,proid int);
#為主表profession添加一個主鍵約束,主鍵名建議以“PK_”開頭,constraint PK_pid指定主鍵名稱,可以省略不寫,系統(tǒng)會自動分配一個名稱
alter table profession add constraint PK_pid primary key (pid);
#為從表student表添加外鍵,并將student表的proid字段和 profession表的pid字段建立外鍵關(guān)聯(lián)。外鍵名建議以“FK_”開頭,constraint FK_pro指定外鍵約束,可以省略不寫,系統(tǒng)會自動分配一個名稱
alter table student add constraint FK_pro foreign key (proid) references profession (pid);
desc profession; #查看主鍵表的表結(jié)構(gòu)
desc student; #查看外鍵表的表結(jié)構(gòu)
show create table student; #可以查看表結(jié)構(gòu)的詳細(xì)信息
4.3 主從表中插入數(shù)據(jù)
#插入新的數(shù)據(jù)記錄時,要先主表再從表
insert into student values (1,'王二',12,3);
#向主鍵表profession中添加數(shù)據(jù)內(nèi)容
insert into profession values (1,'云計算');
insert into profession values (2,'大數(shù)據(jù)');
insert into profession values (3,'JAVA');
insert into profession values (4,'UI設(shè)計');
#向外鍵表student中添加數(shù)據(jù)內(nèi)容
desc profession;
insert into student values (1,'王二',12,3);
insert into student values (2,'毛五',16,2);
insert into student values (3,'肖三',40,4);
insert into student values (3,'李六',30,1);
4.4 主從表中刪除數(shù)據(jù)
#刪數(shù)數(shù)據(jù)記錄時,要先從表再主表,也就是說刪除主鍵表的記錄時必須先刪除其他與之關(guān)聯(lián)的表中的記錄
select * from student;
select * from student;
delete from profession where pid=4;
update student set proid=2 where sname='肖三';
delete from profession where pid=4;
select * from profession;
4.5 刪除外鍵約束
#查看和刪除外鍵約束
show create table student;
alter table student drop foreign key FK_pro; #先刪除外鍵約束
alter table student drop key FK_pro;
desc student;
show create table student;
5.數(shù)據(jù)庫用戶管理
5.1 新建用戶
create user '用戶名'@'來源地址' [identified by [password] '密碼'];
- ‘用戶名’:指定將創(chuàng)建的用戶名;
- ‘來源地址’:指定新創(chuàng)建的用戶可在哪些主機(jī)上登錄,可使用IP地址、網(wǎng)段、主機(jī)名的形式,本地用戶可用localhost,允許任意主機(jī)登錄可用通配符%
- ‘密碼’:若使用明文密碼,直接輸入’密碼’,插入到數(shù)據(jù)庫時由Mysql自動加密;
- 若使用加密密碼,需要先使用select password(‘密碼’); 獲取密文,再在語句中添加password ‘密文’;
- 若省略’‘identified by’'部分,則用戶的密碼將為空(不建議使用).
create user 'user1'@'localhost' identified by '123';
select password('abc123');
create user 'user2'@'localhost' identified by password '*6691484EA6B50DDDE1926A220DA01FA9E575C18A';
create user 'user3'@'%' identified by '123';
select user();
5.2 查看用戶信息
#創(chuàng)建后的用戶保存在mysql數(shù)據(jù)庫的user表里
use mysql;
select user,host, authentication_string from user;
#查看當(dāng)前登錄用戶
select user();
#查看明文密碼'123'的密文信息
select password('123');
#按照上面查找出來的密文密碼信息,創(chuàng)建新用戶clr,按照任意方式登錄
create user 'clr'@'%' identified by password '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257';
5.3 重命名用戶
rename user 'clr'@'%' to 'gzy'@'localhost'; #將clr用戶重命名為gzy用戶
select user,host, authentication_string from user;
5.4 刪除用戶
drop user 'user3'@'%'; #刪除user3用戶
select user,host, authentication_string from user;
5.5 修改當(dāng)前登錄用戶密碼
set password=password('123'); #修改當(dāng)前登錄用戶的密碼
select user,host, authentication_string from user;
5.6 修改其他用戶密碼
set password for 'gzy'@'localhost' = password('abc123');
select user,host, authentication_string from user;
5.7 忘記root密碼的解決辦法
(1)修改/etc/my.cnf 配置文件,不使用密碼直接登錄到mysql
vim /etc/my.cnf
[mysqld]
skip-grant-tables #添加該命令行后,會直接跳過mysql數(shù)據(jù)庫的認(rèn)證授權(quán)登錄
systemctl restart mysqld
mysql #輸入mysql后,可直接登錄
(2)使用update修改root密碼,刷新數(shù)據(jù)庫
update mysql.user set authentication_string=password('111') where user='root' and host='localhost';
flush privileges;
quit
mysql -u root -p111 #使用更改后的root用戶密碼,即可登錄系統(tǒng)
注意:
最后再把/etc/my.cnf配置文件里的skip-grant-tables刪除,并重啟mysql服務(wù)。
6. 數(shù)據(jù)庫用戶授權(quán)
6.1 all privilege包含的權(quán)限
授權(quán)用戶權(quán)限是all privilege。這個all privilege都有哪些權(quán)限?all privilege權(quán)限如下:
insert(插入數(shù)據(jù))
select (查詢數(shù)據(jù))update (更新表的數(shù)據(jù))delete (刪除表中數(shù)據(jù))create (創(chuàng)建庫,表)drop(刪除庫,表)refernces
index(建立索引)alter(更改表屬性)create temp orary tableslock tables(鎖表)
execute
create view (創(chuàng)建視圖)show view (顯示視圖)
create routine(創(chuàng)建存儲過程)alter routine(修改存儲過程)event(事件)
trigger on(創(chuàng)建觸發(fā)器)
6.2 授予權(quán)限
GRANT語句:專門用來設(shè)置數(shù)據(jù)庫用戶的訪問權(quán)限。當(dāng)指定的用戶名不存在時,GRANT語句將會創(chuàng)建新的用戶;當(dāng)指定的用戶名存在時, GRANT 語句用于修改用戶信息。
GRANT 權(quán)限列表 ON 數(shù)據(jù)庫名.表名 TO ‘用戶名’@‘來源地址’ [IDENTIFIED BY ‘密碼’];
#權(quán)限列表:用于列出授權(quán)使用的各種數(shù)據(jù)庫操作,以逗號進(jìn)行分隔,如“select,insert,update”。使用“all”表示所有權(quán)限,可授權(quán)執(zhí)行任何操作。
#數(shù)據(jù)庫名.表名:用于指定授權(quán)操作的數(shù)據(jù)庫和表的名稱,其中可以使用通配符“”。例如,使用“kgc.”表示授權(quán)操作的對象為 kgc數(shù)據(jù)庫中的所有表。
#‘用戶名@來源地址’:用于指定用戶名稱和允許訪問的客戶機(jī)地址,即誰能連接、能從哪里連接。來源地址可以是域名、IP地址,還可以使用“%”通配符,表示某個區(qū)域或網(wǎng)段內(nèi)的所有地址,如“%.kgc.com”、“192.168.80.%”等。
#IDENTIFIED BY:用于設(shè)置用戶連接數(shù)據(jù)庫時所使用的密碼字符串。在新建用戶時,若省略“IDENTIFIED BY”部分,則用戶的密碼將為空。
#允許用戶gzy在本地查詢gzy數(shù)據(jù)庫中所有表的數(shù)據(jù)記錄,但禁止查詢其他數(shù)據(jù)庫中的表的記錄
grant select on gzy.* to 'gzy'@'localhost';
#授予clr用戶從本地登錄,對于所有數(shù)據(jù)庫以及庫中所有表的權(quán)限
grant all on *.* to 'clr'@'localhost' identified by '123';
#允許用戶lisi在所有終端遠(yuǎn)程連接mysql,并擁有所有權(quán)限
grant select on *.* to 'lisi'@'%' identified by '123456';
flush privileges;
quit
mysql -ugzy -pabc123
use gzy;
show tables;
select * from profession;
insert into values (4,'C語言');
delete from profession where pid=3;
6.3 查看權(quán)限
#查看gzy用戶具有哪些權(quán)限
show grants for 'gzy'@'localhost';
6.4 撤銷權(quán)限
revoke select on gzy.* from 'gzy'@'localhost';
show grants for 'gzy'@'localhost';
7. 知識點總結(jié)
外鍵約束
主鍵表: alter table 表名 add primary key (主鍵字段);
外鍵表: alter table 表名 add foreign key (外鍵字段) references 主鍵表 (主鍵字段);
注意:
插入新數(shù)據(jù)時,先在主鍵表插入數(shù)據(jù)再在外鍵表插入對應(yīng)數(shù)據(jù);刪除數(shù)據(jù)時,先在外鍵表刪除數(shù)據(jù)再在主鍵表刪除對應(yīng)數(shù)據(jù).文章來源:http://www.zghlxwxcb.cn/news/detail-483230.html
找回root密碼?
1)修改mysql配置文件/etc/my.cnf,在 [mysqld] 下面添加 skip-grant-tables
2)重啟mysqld服務(wù)。使用 mysql 命令直接登錄mysql
3)執(zhí)行 update mysql.user set authentication_string=password(‘密碼’) where user=‘root’ and host=‘localhost’;
4)還原配置文件,重啟mysqld服務(wù),使用 mysql -u 用戶 -p[密碼] -h 地址 -P 端口 來驗證登錄文章來源地址http://www.zghlxwxcb.cn/news/detail-483230.html
權(quán)限管理
grant 權(quán)限1,權(quán)限2,.... on 庫名.表名 to '用戶名'@'源地址' [identified by '密碼'];
all [privileges] *.*
show grants for '用戶名'@'源地址';
revoke 權(quán)限1,權(quán)限2,.... on 庫名.表名 from '用戶名'@'源地址';
all
到了這里,關(guān)于【數(shù)據(jù)庫二】數(shù)據(jù)庫用戶管理與授權(quán)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!