船停在港灣是很安全的,但那不是造船的目的! 用戶 資料庫常用的指令: 數據表的常用指令 ...
船停在港灣是很安全的,但那不是造船的目的!
用戶
- 創建用戶
- mysql>grant 許可權(select,insert,update,delete) on 資料庫.數據表 to 用戶名@電腦的地址 identified by "用戶的密碼"
- 許可權 【有四種許可權】
- select 查詢
- insert 插入
- update 修改
- delete 刪除
- 資料庫.數據表 【有四種寫法】
- *.* 表示所有資料庫與所有數據表 所有
- *.user 表示所有資料庫的user表 某個表的資料庫
- mysql.* 表示資料庫mysql的所有數據表 某個資料庫
- mysql.user 表示資料庫mysql的資料庫user表 某個資料庫的某個數據表
- 用戶名@電腦的地址
- 用戶名 要創建的用戶名稱,不能跟原先的用戶名相同
- 電腦的地址 mysql軟體所在的電腦連接
- 在自己電腦使用時,用 127.0.0.1
- 與伺服器的mysql交互時,用伺服器的IP
- 許可權 【有四種許可權】
- mysql> grant select,insert,update,delete on mysql.* to [email protected] identif ied by "ceosows";
Query OK, 0 rows affected (0.01 sec) - mysql> grant select,insert,update,delete on *.* to [email protected] identif ied by "ceosows";
Query OK, 0 rows affected (0.01 sec)
- mysql>grant 許可權(select,insert,update,delete) on 資料庫.數據表 to 用戶名@電腦的地址 identified by "用戶的密碼"
- 查看用戶
- select * from 資料庫.數據表
- select * from mysql.user
資料庫常用的指令:
- 創建資料庫
- mysql>create database 資料庫名 ;
- 顯示資料庫
- mysql>show databases;
- +--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| twwq |
| xhkdb |
+--------------------+
6 rows in set (0.02 sec)
- 刪除資料庫
- drop database 資料庫名;
- mysql> drop database twwq;
Query OK, 0 rows affected (0.05 sec)
- 連接資料庫
- use 資料庫名;
- mysql>use twwq;
Database changed
- 查看 當前連接的資料庫
- mysql>select database();
+------------+
| database() |
+------------+
| twwq |
+------------+
1 row in set (0.00 sec)
- mysql>select database();
數據表的常用指令
- 創建表
- create table 表名(<欄位名1> <類型1> [,..<欄位名n> <類型n>]); //警告:沒有給表填加欄位,會出錯
- mysql> create table registered(id int(8) not null primary key auto_increment,
-> username char(20) not nul
-> password char(20) not null,
-> repassword char(20) not null);
Query OK, 0 rows affected (0.19 sec)
- 查看表
- mysql>desc 表名 ; (方式一) mysql> show columns from 表名; (方式二)
- mysql>desc registered; (方式一) mysql> show columns from registered;(方式二 )
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(8) | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | NULL | |
| password | char(20) | NO | | NULL | |
| repassword | char(20) | NO | | NULL | |
+------------+----------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
- 修改表
- rename table (原先的)表名 to (修改後的)表;
- mysql> rename table registered to sows;
Query OK, 0 rows affected (0.06 sec) - 查看效果
- mysql> desc sows;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(8) | NO | PRI | NULL | auto_increment |
| name | char(20) | YES | | NULL | |
| password | char(20) | NO | | NULL | |
| repassword | char(20) | NO | | NULL | |
+------------+----------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
- mysql> desc sows;
- 刪除表
- mysql>drop table 表名; //警告:永久性刪除數據表,慎用
- mysql>drop table registered;
Query OK, 0 rows affected (0.05 sec)
- 增加 alter add命令用來增加表的欄位。
- 增添欄位
- alter table 數據表 add 欄位 參數 其他;
- mysql> alter table registered add sex char(2);
Query OK, 0 rows affected (0.33 sec)
Records: 0 Duplicates: 0 Warnings: 0 - 查看效果
- mysql> desc registered;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(8) | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | NULL | |
| password | char(20) | NO | | NULL | |
| repassword | char(20) | NO | | NULL | |
| sex | char(2) | YES | | NULL | | //增加sex 欄位
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.01 sec) - mysql> desc registered;
ERROR 1146 (42S02): Table 'twwq.registered' doesn't exist //原因表名被修改,因此原先的表名,無法使用了
- mysql> desc registered;
- 修改欄位
- alter table 數據表 change (要被修改的)欄位 (修改後的)欄位 參數;
- mysql> alter table registered change username name char(20);
Query OK, 0 rows affected (0.33 sec)
Records: 0 Duplicates: 0 Warnings: 0 - 查看效果
- mysql> desc registered;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(8) | NO | PRI | NULL | auto_increment |
| name | char(20) | YES | | NULL | | // username 修改成 name
| password | char(20) | NO | | NULL | |
| repassword | char(20) | NO | | NULL | |
| sex | char(2) | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
- mysql> desc registered;
- 刪除欄位
- alter table 數據表 drop 欄位;
- mysql> alter table registered drop sex;
Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0 - 查看效果
- mysql> desc registered;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(8) | NO | PRI | NULL | auto_increment |
| name | char(20) | YES | | NULL | |
| password | char(20) | NO | | NULL | |
| repassword | char(20) | NO | | NULL | |
+------------+----------+------+-----+---------+----------------+ //sex 欄位 已經被刪除
4 rows in set (0.01 sec)
- mysql> desc registered;
- 加索引
- alter table 數據表 add index 索引名(【已經存在於表的】欄位名);
- mysql> alter table registered add index sows_name(username);
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0 - 查看效果
- mysql> desc registered;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(8) | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | MUL | NULL | | //Key 欄位出現 MUL
| password | char(20) | NO | | NULL | |
| repassword | char(20) | NO | | NULL | |
| sex | char(2) | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
- mysql> desc registered;
- 刪除索引
- alter table 數據表 drop index 索引名;
- mysql> alter table registered drop index sows_name;
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0 - 查看效果
- mysql> desc registered;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(8) | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | NULL | | //key 欄位的內容消失
| password | char(20) | NO | | NULL | |
| repassword | char(20) | NO | | NULL | |
| sex | char(2) | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
- mysql> desc registered;
- 增添欄位
- 插入數據
- mysql>insert into 數據表(欄位 //如果已經實現步驟1,則不用再寫欄位) values (根據欄位的數量,變數類型、變數長度要求、是否允許為空,來進行填寫內容)
- mysql> insert into registered values (1,'sows','asqw1234','asqw1234'),(2,'ceo','
aswe1322','aswe1322'),(3,'jk','qazxsw12','qazxsw12');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0 // 成功插入 3條數據
- 查看表中的數據 //準確性的查詢所需數據
- mysql>select * from 數據表; //獲取表中所有數據
- mysql>select * from registered;
+----+----------+----------+--------------+
| id | username | password | repassword |
+----+----------+----------+--------------+
| 1 | sows | asqw1234 | asqw1234 |
| 2 | ceo | aswe1322 | aswe1322 |
| 3 | jk | qazxsw12 | qazxsw12 |
+----+----------+----------+--------------+
3 rows in set (0.00 sec) //獲取到3條數據
- mysql>select * from registered;
- mysql>select * from 數據表 order by id limit 從哪個位置開始,一共幾條;
- mysql> select * from registered order by id limit 1,2;
+----+----------+----------+------------+
| id | username | password | repassword |
+----+----------+----------+------------+
| 2 | ceo | aswe1322 | aswe1322 |
| 3 | jk | qazxsw12 | qazxsw12 |
+----+----------+----------+------------+
2 rows in set (0.00 sec) //獲取兩條數據
- mysql> select * from registered order by id limit 1,2;
- mysql>select * from 數據表; //獲取表中所有數據
- 刪除表中的數據
- mysql>delect from 數據表 where 條件限制 ;
- mysql> delete from registered where id=1;
Query OK, 1 row affected (0.01 sec) //刪除成功 - 使用5,查看刪除的效果
- mysql> select * from registered;
+----+----------+----------+------------+
| id | username | password | repassword |
+----+----------+----------+------------+
| 2 | ceo | aswe1322 | aswe1322 |
| 3 | jk | qazxsw12 | qazxsw12 |
+----+----------+----------+------------+
2 rows in set (0.00 sec) //id為1的數據被成功刪除了
- mysql> select * from registered;
- 修改表中的數據
- mysql>update 數據表 set (要修改數據的)欄位=修改後的數據 where 條件限制
- mysql> update registered set username='sowsceo' where id=2;
Query OK, 1 row affected (0.01 sec) //修改成功
Rows matched: 1 Changed: 1 Warnings: 0 //修改的數量 - 查看修改後的效果
- mysql> select * from registered;
+----+----------+----------+------------+
| id | username | password | repassword |
+----+----------+----------+------------+
| 2 | sowsceo | aswe1322 | aswe1322 | //username 從ceo 變成 sowsceo
| 3 | jk | qazxsw12 | qazxsw12 |
+----+----------+----------+------------+
2 rows in set (0.00 sec)
- mysql> select * from registered;
- mysql> update registered set username='sowsceo' where id=2;
- mysql>update 數據表 set (要修改數據的)欄位=修改後的數據 where 條件限制