標籤:ext ase class create 存在 tables logs esc ble
1、顯示資料庫
show databases;
2、選擇資料庫
use 資料庫名;
3、顯示資料庫中的表
show tables;
4、顯示資料表的結構
describe 表名;
5、顯示表中記錄
SELECT * FROM 表名
6、建庫
create databse 庫名;
7、建表
create table 表名 (欄位設定列表);mysql> create table name( -> id int auto_increment not null primary key , -> uname char(8), -> gender char(2), -> birthday date );Query OK, 0 rows affected (0.03 sec)mysql> show tables;+------------------+| Tables_in_userdb |+------------------+| name |+------------------+1 row in set (0.00 sec)mysql> describe name;+----------+---------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+---------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || uname | char(8) | YES | | NULL | || gender | char(2) | YES | | NULL | || birthday | date | YES | | NULL | |+----------+---------+------+-----+---------+----------------+4 rows in set (0.00 sec)註: auto_increment 自增 primary key 主鍵
8、增加記錄
insert into name(uname,gender,birthday) values(‘張三‘,‘男‘,‘1971-10-01‘);
9、修改記錄
update name set birthday=‘1971-01-10‘ where uname=‘張三‘;
10、刪除記錄
delete from name where uname=‘張三‘;
11、刪除表
drop table 表名
12、刪除庫
drop database 庫名;
13、備份資料庫
mysqldump -u root -p --opt 資料庫名>備份名; //進入到庫目錄
14、恢複
mysql -u root -p 資料庫名<備份名; //恢複時資料庫必須存在,可以為空白資料庫
15、資料庫授權
格式:grant select on 資料庫.* to 使用者名稱@登入主機 identified by "密碼"
例1、增加一個使用者user001密碼為123456,讓他可以在任何主機上登入,並對所有資料庫有查詢、插入、修改、刪除的許可權。首先用以root使用者連入MySQL,然後鍵入以下命令:
mysql> grant select,insert,update,delete on *.* to [email protected]"%" Identified by "123456";
例2、增加一個使用者user002密碼為123456,讓此使用者只可以在localhost上登入,也可以設定指定IP,並可以對資料庫test進行查詢、插入、修改、刪除的操作 (localhost指本地主機,即MySQL資料庫所在的那台主機)
//這樣使用者即使用知道user_2的密碼,他也無法從網上直接存取資料庫,只能通過MYSQL主機來操作test庫。
//首先用以root使用者連入MySQL,然後鍵入以下命令:
mysql>grant select,insert,update,delete on test.* to [email protected] identified by "123456";
Linux下MySQL資料庫常用基本操作