MySQL資料庫操作指令

來源:互聯網
上載者:User

標籤:

一、資料庫建立使用者

1)簡單建立

   CREATE  USER  [email protected];

2)帶有密碼

   CREATE  USER  [email protected]  IDENTIFIED  BY ‘123456’

這裡的[email protected]是建立的使用者名稱,123456是使用者的密碼。

 

注意:MySQL資料庫命令不區分大小寫。

 

二、建立使用者之後,需要對使用者賦予相應的許可權,一般用GRANT指令

其格式為:GRANT 許可權 ON 許可權範圍 to 使用者名稱@登入主機 IDENTIFIED BY ‘密碼’;

1)GRANT ALL ON *.* TO ‘ty’@’localhost’ ;

       讓使用者在本地登入,並賦予其所有資料庫、表所有許可權,“ALL”的意思是所有許可權,ON子句中*.*是所有資料庫、表,”localhost”表示在本地登入。

2)GRANT SELECT,UPDATE,DELETE,INSERT ON *.* TO ‘ty’@’%’ IDENTIFIED BY ‘123456’

   讓使用者ty在所有地方都可以登入賦予其增刪查改等許可權,這裡的’%’表示使用者在所有地方任何主機都可以登入。

 

三、查看許可權

    1)目前使用者

  Show  grants;

2)其他使用者(使用關鍵詞for)

      Show  grants  for  [email protected];

 

四、撤銷許可權

撤銷已經賦予給Mysql使用者的許可權,用revoke指令,另外只需把關鍵字“to”換成“from”即可:

Grant  all  on  *.*  to  [email protected];

Revoke all  on  *.*  from  [email protected];

 

五、資料庫操作指令

   1.1建立資料庫

      Create  database  Tang;

   1.2建立資料表

     Create  table  userinfo(

         id  int(4)  not null  primary key  auto_increment,

         userName  char(20)  not null,

         password  char(20)  not null);

 

   2.1查看顯示資料庫

      Show  databases;

   2.2顯示資料表

       使用desc語句,擷取資料表結構

mysql> desc userinfo;

 

+-----------+-------------+----------+---------+----------------+--------+

| Field          | Type           | Null         | Key        | Default            | Extra    |

+-----------+-------------+----------+----------+---------------+--------+

| id              | int(11)        | NO          | PRI         | NULL              |             |

| userName  | varchar(20)  | NO          |              | NULL              |             |

| password   | varchar(20)  | NO         |               | NULL             |             |

+----- ------+-------------+---------+----------+---------------+---------+

3 rows in set (0.01 sec

 

 

   3.1刪除資料庫

      drop  database  Tang;

      drop  database  if  exists  Tang;

   3.2刪除表格的欄位

      Alter  table  userinfo  drop  new_sex;

   3.3 刪除資料表

       drop  table  userinfo;

   3.4 刪除表中具體的資料(注意這裡只用的命令也不同,是delete不是drop)

       delete  from  userinfo  where  id=0;

      注意: 在不確定是否存在一個資料庫或表時,如果要執行刪除指令,最好加上一個條件判斷“if  exists”,這樣可避免發生錯誤。

 

   4.1添加表的欄位,這是在表的結構上做添加

       Alter  table  userinfo  add  sex  char(10)  not null;

   4.2往表中插入資料,這是對錶的內容做添加

      Insert  into  userinfo  values(0,’ty’,’1234’),(1,’xy’,’5678’);

 

   5.1修改資料庫的密碼

      Set  password  for  [email protected]  =  old_password(‘123456’);  

   5.2 修改原表的欄位

   Alter  table  userinfo  change  sex  new_sex  bit  not null;

   5.3 修改表中的資料

       Updata  userinfo  set  userName=’th’  where  id=1;

   5.4修改表名

       使用rename指令

      RENAME  table  userinfo  to  user;

   

    6.use指令表示選擇一個資料庫

      Use  Tang;

 

 

    7.查詢表中的資料(select)

          查詢所有資料

             Select  * from  userinfo;

          查詢id是0~2之間的行資料

             Select  * from  userinfo  order by  id  limit 0,2;

          查詢前5條資料

             Select  * from  userinfo  limit 5;

          查詢具體某一條資料

             Select  *from  userinfo  where  id=1;     

 

MySQL資料庫操作指令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.