mysql基本操作

來源:互聯網
上載者:User

標籤:mysql命令

mysql基本操作


  對於一個企業來說,資料的重要性是毋庸置疑的。作為一個營運人員,必須要掌握基本的資料庫操作,為了方便以後查看,我把一些簡單的資料庫命令寫出來。


查看資料庫

MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               |+--------------------+


建立資料庫

MariaDB [(none)]> create database xhk;


進入資料庫並建立一個表

MariaDB [(none)]> use xhk;Database changedMariaDB [xhk]> create table stu (id int auto_increment primary key,name varchar(20) unique key);


查看錶的結構,有2種方法

MariaDB [xhk]> DESC stu;+-------+-------------+------+-----+---------+----------------+| Field | Type        | Null | Key | Default | Extra          |+-------+-------------+------+-----+---------+----------------+| id    | int(11)     | NO   | PRI | NULL    | auto_increment || name  | varchar(20) | YES  | UNI | NULL    |                |+-------+-------------+------+-----+---------+----------------+MariaDB [xhk]> show create table stu;+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table                                                                                                                                                                              |+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| stu   | CREATE TABLE `stu` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(20) DEFAULT NULL,  PRIMARY KEY (`id`),  UNIQUE KEY `name` (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 |+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+


在表中插入多條資料

MariaDB [xhk]> insert into stu(id,name) value(1,‘xhk‘),(2,‘xxx‘);


查詢表的內容

MariaDB [xhk]> select * from stu;+----+------+| id | name |+----+------+|  1 | xhk  ||  2 | xxx  |+----+------+


也可以按照需要查詢

MariaDB [xhk]> select * from stu where id = 1;+----+------+| id | name |+----+------+|  1 | xhk  |+----+------+


使用*號查詢所有,但線上資料很大,使用*可能導致資料庫壓力增大,影響效能,建議使用特定的值來查詢

MariaDB [xhk]> select name from stu;+------+| name |+------+| xhk  || xxx  |+------+


修改表中的資料

MariaDB [xhk]> update xhk set name=‘qqq‘ where id=2;


刪除表中的一行

MariaDB [xhk]> delete from stu where id=2 ;


增加表的欄位

MariaDB [xhk]> alter table stu add age int;


刪除表的欄位

MariaDB [xhk]> alter table stu drop age;


修改欄位的類型

MariaDB [xhk]> alter table stu modify name varchar(20);


修改欄位的名字以及類型

MariaDB [xhk]> alter table stu change name names varchar(10);


修改表的名字

MariaDB [xhk]> rename table stu to student;


刪除表中的全部資料

MariaDB [xhk]> delete from student;


建立使用者和設定密碼並且指定使用者對所有庫都有所有權,並且允許所有主機

MariaDB [xhk]> grant all on *.* to "xhk1"@"%" identified by "123456";


修改使用者的密碼

MariaDB [xhk]> update  mysql.user set password=password(‘redhat‘) where user="xhk1";


查看使用者的許可權

MariaDB [xhk]> show grants for "xhk1"@"localhost";+----------------------------------------------------------------------------------------------------------------------+| Grants for [email protected]                                                                                            |+----------------------------------------------------------------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO ‘xhk1‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ |+----------------------------------------------------------------------------------------------------------------------+


建立使用者只有對xhk庫的student表有查詢許可權,並且只允許本地登入

MariaDB [xhk]> grant select,update,delete,insert on xhk.student to "xhk1"@"localhost" identified by "123456";

收回使用者的某個許可權

MariaDB [xhk]> revoke update,delete,insert on xhk.student to "xhk1"@"localhost";


刪除使用者

MariaDB [xhk]> delete from mysql.user where name="xhk1" and host="localhost";


本文出自 “xhk__營運” 部落格,轉載請與作者聯絡!

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.