標籤: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基本操作