<b>1</b><b>、顯示資料庫</b>
show databases;
<b>2</b><b>、選擇資料庫</b>
use 資料庫名;
<b>3</b><b>、顯示資料庫中的表</b>
show tables;
<b>4</b><b>、顯示資料表的結構 </b>
describe 表名;
<b> 5</b><b>、顯示表中記錄 </b>
select * from 表名
<b> 6</b><b>、建庫</b>
create
databse 庫名;
<b> 7</b><b>、建表</b>
<a></a>
create table 表名 (字段設定清單);
mysql> create table name(
-> id int auto_increment not null primary key ,
-> uname
char(<b>8</b>),
->
gender char(<b>2</b>),
-> birthday
date );
query ok, <b>0</b> rows affected (<b>0.03</b> sec)
mysql> show tables;
+------------------+
| tables_in_userdb |
| name |
<b>1</b> row in set (<b>0.00</b> sec)
mysql> describe name;
+----------+---------+------+-----+---------+----------------+
| field
| type
| null | key | default | extra |
| id
| int(<b>11</b>) | no | pri | null | auto_increment |
| uname
| char(<b>8</b>) | yes | | null | |
| gender
| char(<b>2</b>) | yes | | null | |
| birthday |
date |
yes | | null | |
<b>4</b> rows in set (<b>0.00</b> sec)
注: auto_increment 自增
primary key 主鍵
<b> 8</b><b>、增加記錄</b>
insert into name(uname,gender,birthday) values('張三','男','1971-10-01');
<b> 9</b><b>、修改記錄</b>
update name set
birthday='1971-01-10' where uname='張三';
<b> 10</b><b>、删除記錄</b>
delete from name where uname='張三';
<b> 11</b><b>、删除表</b>
drop table 表名
<b> 12</b><b>、删除庫</b>
drop database 庫名;
<b>13</b><b>、備份資料庫 </b>
mysqldump -u root -p --opt 資料庫名>備份名; //進入到庫目錄
<b>14</b><b>、恢複</b>
mysql -u root -p 資料庫名<備份名; //恢複時資料庫必須存在,可以為空資料庫
<b> 15</b><b>、資料庫授權</b>
格式:grant select on 資料庫.* to 使用者名@登入主機 identified by "密碼"
例1、增加一個使用者user001密碼為123456,讓他可以在任何主機上登入,并對所有資料庫有查詢、插入、修改、删除的權限。首先用以root使用者連入mysql,然後鍵入以下指令:
mysql> grant select,insert,update,delete on *.* to user001@"%"
identified by "<b>123456</b>";
例2、增加一個使用者user002密碼為123456,讓此使用者隻可以在localhost上登入,也可以設定指定ip,并可以對資料庫test進行查詢、插入、修改、删除的操作 (localhost指本地主機,即mysql資料庫所在的那台主機)
//這樣使用者即使用知道user_2的密碼,他也無法從網上直接通路資料庫,隻能通過mysql主機來操作test庫。
//首先用以root使用者連入mysql,然後鍵入以下指令:
mysql>grant select,insert,update,delete on test.* to user002@localhost identified by
"<b>123456</b>";
注:
其次也可以采用修改表的方式,處理使用者的登入方式:
資料庫: mysql
表: user
修改: user表中的host列的值來現實登入入口