mysql常用的yu語句 MySql常用命令總結

2021-10-18 15:49:52 字數 2802 閱讀 7080

1:使用show語句找出在伺服器上當前存在什麼資料庫:

mysql> show databases;

2:建立乙個資料庫mysqldata

mysql> create database mysqldata;

3:選擇你所建立的資料庫

mysql> use mysqldata; (按回車鍵出現database changed

時說明操作成功!)

4:檢視現在的資料庫中存在什麼表

mysql> show tables;

5:建立乙個資料庫表

mysql> create table mytable (name varchar(20), ***

char(1));

6:顯示表的結構:

mysql> describe mytable;

7:往表中加入記錄

mysql> insert into mytable values (」hyq」,」m」);

8:用文字方式將資料裝入資料庫表中(例如d:/mysql.txt)

mysql> load data local infile 「d:/mysql.txt」 into

table mytable;

9:匯入.sql檔案命令(例如d:/mysql.sql)

mysql>use database;

mysql>source d:/mysql.sql;

10:刪除表

mysql>drop table mytable;

11:清空表

mysql>delete from mytable;

12:更新表中資料

mysql>update mytable set ***=」f」 where

name=』hyq』;

在windows中mysql以服務形式存在,在使用前應確保此服務已經啟動,未啟動可用net start

mysql命令啟動。而linux中啟動時可用「/etc/rc.d/init.d/mysqld

start」命令,注意啟動者應具有管理員許可權。

剛安裝好的mysql包含乙個含空密碼的root帳戶和乙個匿名帳戶,這是很大的安全隱患,對於一些重要的應用我們應將安全性盡可能提高,在這裡應把匿名帳戶刪除、

root帳戶設定密碼,可用如下命令進行:

use mysql;

delete from user where user=」";

update user set password=password(』newpassword』) where

user=』root』;

如果要對使用者所用的登入終端進行限制,可以更新user表中相應使用者的host欄位,在進行了以上更改後應重新啟動資料庫服務,此時登入時可用如下類似命令:

mysql -uroot -p;

mysql -uroot -pnewpassword;

mysql mydb -uroot -p;

mysql mydb -uroot -pnewpassword;

上面命令引數是常用引數的一部分,詳細情況可參考文件。此處的mydb是要登入的資料庫的名稱。

在進行開發和實際應用中,使用者不應該只用root使用者進行連線資料庫,雖然使用root使用者進行測試時很方便,但會給系統帶來重大安全隱患,也不利於管理技術的提高。我們給乙個應用中使用的使用者賦予最恰當的資料庫許可權。如乙個只進行資料插入的使用者不應賦予其刪除資料的許可權。mysql的使用者管理是通過

user表來實現的,新增新使用者常用的方法有兩個,一是在user表插入相應的資料行,同時設定相應的許可權;二是通過grant命令建立具有某種許可權的使用者。其中grant的常用用法如下:

grant all on mydb.* to newusername@hostname identified by

「password」 ;

grant usage on *.* to newusername@hostname identified by

「password」;

grant select,insert,update on mydb.* to newusername@hostname

identified by 「password」;

grant update,delete on mydb.testtable to newusername@hostname

identified by 「password」;

若 要給此使用者賦予他在相應物件上的許可權的管理能力,可在grant後面新增with grant

option選項。而對於用插入user表新增的使用者,password欄位應用password

函式進行更新加密,以防不軌之人竊看密碼。對於那些已經不用的使用者應給予清除,許可權過界的使用者應及時**許可權,**許可權可以通過更新user表相應字段,也可以使用revoke操作。

全域性管理許可權:

file: 在mysql伺服器上讀寫檔案。

process: 顯示或殺死屬於其它使用者的服務執行緒。

reload: 過載訪問控制表,重新整理日誌等。

shutdown: 關閉mysql服務。

資料庫/資料表/資料列許可權:

alter: 修改已存在的資料表(例如增加/刪除列)和索引。

create: 建立新的資料庫或資料表。

delete: 刪除表的記錄。

drop: 刪除資料表或資料庫。

index: 建立或刪除索引。

insert: 增加表的記錄。

select: 顯示/搜尋表的記錄。

update: 修改表中已存在的記錄。

特別的許可權:

all: 允許做任何事(和root一樣)。

usage: 只允許登入–其它什麼也不允許做。

mysql常用的yu語句 常用mysql語句備份

1.場景 由於需求變動,需要將一張表裡面的兩個字段合併並存到其中的乙個欄位中 可以這樣 update table set column1 concat ws ifnull column1,ifnull column2,也可以這樣 update table set column1 concat ifn...

mysql語句什麼或什麼 一些常用的mysql語句

1,檢視索引 show index from tablename show keys from tablename 2,新增索引 alter table tablename add index index name column list alter table tablename add uniq...

常用mysql語句 常用MySql語句

新建資料表 drop table if exists ga game way create table ga game way id int 11 unsigned not null auto increment comment id primary key id using btree,主鍵 un...