(零基礎)入門學習Python| 面向物件技術簡介(下)
阿新 • • 發佈:2020-12-08
技術標籤:mysql
mysql
建立使用者
create user 'username'@'host' IDENTIFIED by 'password'
--剛建立的使用者許可權都為N,可以連線資料庫,不能為自己授許可權host
--表示允許使用者登入的主機名,可以使用%萬用字元,如果不指定,則認為不限制主機,
--等價於@'%',如果不新增密碼,則認為不需要碼登入
create user ' user1'@'localhost' IDENTIFIED by 'pwd1','user2'@'localhost' IDENTIFIED by 'pwd2'
--同時建立多個使用者
建立資料庫
create DATABASE [資料庫名稱]
default CHARACTER set utf8 --指定預設字符集和校對
default collate utf8_general_ci
建立表
create table person(
id int comment '使用者id' auto_increment, --設定自增
name VARCHAR(10) COMMENT '使用者名稱', --列註釋
PRIMARY KEY (id) --設定主鍵
)
COMMENT '使用者資訊表' --表註釋
刪除資料庫
drop database [資料庫名稱]
刪除使用者
drop user [username]
--如果刪除的使用者建立了表,索引或者其他資料庫物件,會繼續保留,mysql沒有記錄誰建立了這些物件
刪除表
drop table [表名稱]
刪除表資料
delete from [表名稱] --刪除單張表全部資料
truncate [表名稱]
插入資料到表
insert into person (name) values ('張三' ) --插入一行資料
insert into person (name) values ('李四'),('王五') --插入多行資料
insert into stu (name) select name from person --將一張表資料插入到另一張表中 (指定列)
insert into stu select * from person --將一張表資料插入到另一張表中 (全部列)
更新使用者名稱
rename user 'aaa'@'localhost' to 'bbb'@'localhost'
更新使用者密碼
set PASSWORD for 'username'@'localhost' = PASSWORD('pwd')
更新資料庫
alter database dd character set sjis default COLLATE sjis_japanese_ci
--修改資料庫預設字符集和校對,只對後建立的表有效
更新表資料
update person set name='李六' where id=3 --將person表中id為6的name更新(單表更新)
update stu,person set stu.name='王七',person.name='王七' where stu.id=person.id and stu.id=1
--將person和stu表中id為1的name更新(多表更新)
檢視使用者
select user() --返回結果為當前登入使用者 [使用者名稱]@[IP地址]
select user,host FROM mysql.user; 顯示資料庫所有使用者和授權登入的ip
檢視資料庫
show databases
select schema_name from information_schema.SCHEMATA
select schema_name,default_character_set_name,default_collation_name from
information_schema.SCHEMATA
--mysql自帶兩個資料庫 information_schema,mysql
檢視資料庫中的表
show tables
select table_name from information_schema.`TABLES` where table_schema=[資料庫名稱]
使用資料庫
use [資料庫名稱] --登入後指定
mysql -u [使用者名稱] -p [資料庫名稱] --登入時指定
授予表許可權
grant select,delete on *.* to 'username'@'host','username2'@'host'
--可以將多個許可權授予多個使用者,如果指定的使用者不存在,會自動建立且沒有密碼
grant select,delete on *.* to 'username'@'%' IDENTIFIED by 'password'
--如果指定的使用者不存在,最好指定密碼
--