資料庫的基本操作
1.建立資料庫
# create database xxx;
2.檢視資料庫
# show databases;
3.刪除資料庫
# drop database xxx;
4.使用/切換資料庫
# use xxx;
5.退出資料庫
# exit;
表的基本操作
1.檢視資料庫中所有的表
# show tables;
2.建立表
# create table xxx;
3.檢視錶結構
# desc xxx;
4.檢視錶詳細結構
# show create table xxx;
5.刪除表
# drop table xxx;
6.修改表名
# alter table xxx rename yyy;
7.修改欄位的資料型別
# alter table 表名 modify 屬性名 新資料型別; (注意外來鍵不能改) # alter table B modify user_name char(50);
8.修改欄位名與資料型別
# alter table 表名 change 舊屬性名 新屬性名 資料型別; # alter table GG change user_name your_name char(50);
9.增加欄位
# alter table 表明 add 新欄位名 資料型別
# alter table GG add phone varchar(20); # alter table GG add age int(4) not null; # alter table GG add address varchar(30) not null after phone;
10.刪除欄位
# alter table GG drop age;
11.刪除某條記錄
# delete from 表名 where 條件 # delete from 表名 //刪除所有記錄
12.修改表的儲存引擎
# alter table A engine=MyISAM;
13.刪除表的外來鍵約束
# alter table 表名 drop foreign key 外來鍵別名
# alter table yy2 drop foreign key y_fk;
14.刪除表
# drop table xxx;
刪除父表:先刪除所有關聯子表的外來鍵,再刪除父表
使用者登入與管理相關操作
1. 建立使用者
-> create user 'test1'@'localhost'identified by '123456';
-> create user 'test2'@'localhost'identified by 'PASSWORD(123456)';
-> insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject)values('hostname','test3',PASSWORD('123456'),'','','');
2. 檢視使用者資訊
-> select Host,User,Password from mysql.user where User='test1' or User='test2' or User='test3'; +-----------+-------+-------------------------------------------+ | Host | User | Password | +-----------+-------+-------------------------------------------+ | localhost | test1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | localhost | test2 | *A4734F92867FE18E0DD415197D5C33D2E705F0DC | | hostname | test3 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-----------+-------+-------------------------------------------+
3. 刪除使用者
-> drop user 'test2'@'localhost';
-> delete from mysql.user where Host='localhost' and User='test3';
4. 修改root使用者密碼
# mysqladmin -u root -p password '新密碼' Enter password: --->在這裡要輸入正確的舊密碼 -> update mysql.user set Password=PASSWORD('新密碼') where User='root' and Host='localhost';
5. 修改普通使用者密碼
# mysql -u test1 -p -> set password=PASSWORD('aixocm'); -> set password for 'test1'@'localhost'=PASSWORD('aixocm');
6. 解決忘記root使用者密碼問題
# /etc/init.d/mysqld start --skip-grant-tables
# mysql -u root -p --->這樣可以無密碼登陸進去了 -> update mysql.user set Password=PASSWORD('新密碼') where User='root' and Host='localhost';
使用者許可權管理操作
1. 新增許可權
grant 許可權 on 資料庫名.表名 to 'username'@'Host'
//新建使用者並增加許可權 grant select,update -> on *.* --->對所有資料庫的所有表 -> to 'test5'@'localhost' identified by 'aixocm' -> with grant option; grant delete -> on *.* -> to 'test5'@'localhost' -> with grant option;
2. 取消許可權
revoke delete -> on *.* -> from 'test5'@'localhost';
//取消所有許可權 revoke all privileges,grant option from 'test5'@'localhost';
3. 檢視許可權
-> select User,Host,Select_priv,Update_priv,Delete_priv from mysql.user where User='test5'; +-------+-----------+-------------+-------------+-------------+ | User | Host | Select_priv | Update_priv | Delete_priv | +-------+-----------+-------------+-------------+-------------+ | test5 | localhost | Y | Y | Y | +-------+-----------+-------------+-------------+-------------+ -> show grants for 'root'@'localhost'\G; *************************** 1. row *************************** Grants for root@localhost: GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*5DBA547161E5D9C6508C3C6CF5D1B26940E3BC13' WITH GRANT OPTION *************************** 2. row *************************** Grants for root@localhost: GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION
4.重新整理(載入)許可權
-> flush privileges;
儲存引擎相關操作
1.檢視資料庫支援的儲存引擎
# show engines; # show engines\G; Engine:名稱 Support:是否支援(YES/DEFAULT) Comment:說明 Transaction:是否支援事務 XA:是否支援分散式交易處理的XA規範 savapoint:是否支援儲存點
2.檢視儲存引擎詳細資訊
# show engine innodb status\G;
3.檢視預設儲存引擎
# show variables like 'storage_engine';
4.修改預設儲存引擎
# vim /etc/my.cnf(配置檔案)
default-storage-engine=xxx