mysql資料庫管理
mysql資料庫管理
SQL語句概述.
1、SQL語言
Structured Query Language的縮寫,即結構化查詢語言
關係型資料庫的標準語言
用於維護管理資料庫
包括資料查詢、資料更新、訪問控制、物件管理等功能
2、SQL分類
DDL:資料定義語言
DML:資料操縱語言
DQL:資料查詢語言
DCL:資料控制語言
# char和varchar的區別
1、char的長度是不可變的,而varchar的長度是可變的
欄位d:型別varchar(10),值為: abc,儲存為: abc (自動變為3個的長度)
2、超出長度自動擷取
欄位c:型別char(3),值為: abcdefg,儲存為: abc (defg自動刪除)
欄位e:型別varchar(3),值為: abcdefg,儲存為:abc (defg自動刪除)
3、varchar(10)和char(10),都表示可存10個字元,無論存放的是數字、字母還是UTF8漢字(每個漢字3位元組),都可以存放10個
4、char最多可以存放255個字元
varchar的最大長度為65535個位元組,varchar可存放的字元數跟編碼有關字元型別若為gbk,每個字元最多佔2個位元組,最大長度不能超過32766個字元字元型別若為utf8,每個字元最多佔3個位元組,最大長度不能超過21845個字元
注:
一個漢字佔多少長度與編碼有關:
UTF-8:一個漢字=3個位元組
GBK:一個漢字=2個位元組
mysql資料庫常用語句分類
1、DDL(Data Definition Language,資料定義語言)∶用來建立資料庫、資料庫物件和定義欄位,如 CREATE、ALTER、DROP。
2、DML(Data Manipulation Language,資料操縱語言)∶用來插入、刪除和修改資料庫中的資料,如 INSERT、UPDATE、DELETE。
3、DQL (Data Query Language,資料查詢語言):用來查詢資料庫中的資料,如SELECT。
4、DCL (Data Control Language,資料控制語言):用來控制資料庫元件的存取許可、存取許可權等,如 COMMIT、ROLLBACK、GRANT、REVOKE。
一、DDL
1.建立新的資料庫
例項:
mysql> create database auth;//建立auth資料庫
Query OK, 1 row affected (0.00 sec)
2.建立新的表
CREATE TABLE表名(欄位1名稱型別,欄位2名稱型別,…,PRIMARY KEY (主鍵名))
例項:
建立一個表 裡面新增屬性
mysql> use auth;//進入auth資料庫
Database changed
mysql> create table users(user_name char(64) not null primary key, user_passwd varchar(64) default '');
Query OK, 0 rows affected (0.01 sec)
mysql> describe users; //檢視user表
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| user_name | char(64) | NO | PRI | NULL | |
| user_passwd | varchar(64) | YES | | | |
+-------------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql>
mysql> create database aaa; //建立aaa資料庫
Query OK, 1 row affected (0.01 sec)
mysql> use aaa; //進入表
Database changed
mysql> create table cj(id int(10) auto_increment primary key, age int(3) not null, name varchar(128) not null, score decimal(5));
Query OK, 0 rows affected (0.00 sec)
mysql> describe cj; //查看錶
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| age | int(3) | NO | | NULL | |
| name | varchar(128) | NO | | NULL | |
| score | decimal(5,0) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
3、增加一個屬性 修改表結構
mysql> alter table cj add column addr int(3);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc cj;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| age | int(3) | NO | | NULL | |
| name | varchar(128) | NO | | NULL | |
| score | decimal(5,0) | YES | | NULL | |
| addr | int(3) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql>
4.刪除一個數據表
mysql> create table cj2 like cj; //克隆cj表的資料 並且生成cj2
Query OK, 0 rows affected (0.00 sec)
mysql> drop table cj2; //刪除cj2表
Query OK, 0 rows affected (0.01 sec)
5、刪除一個數據庫
mysql> create database abc; //建立abc資料庫
Query OK, 1 row affected (0.00 sec)
mysql> drop database abc; //刪除abc資料庫
Query OK, 0 rows affected (0.01 sec)
二、DML
1.插入資料記錄
INSERT INTO 表名(欄位1,欄位:2,…) VALUES(欄位1的值,
欄位2的值,…)
mysql> desc users; //查看錶結構
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| user_name | char(64) | NO | PRI | NULL | |
| user_passwd | varchar(64) | YES | | | |
+-------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into users(user_name,user_passwd) values('zhangsan', password('123456')); //建立zhangsan 密碼123456(密文)
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into users values('lisi','abc123'); //建立lisi user_passwd數值是abc123(不是密碼,是數值)
Query OK, 1 row affected (0.00 sec)
mysql> select * from users; //檢視users表裡面的所有內容
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | abc123 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql>
mysql> insert into cj(age,name,score,addr) values(17,'zhangsan',60,101), (19,'lisi',89,102), (19,'wangwu',88,103), (20,'wangerma',99,104), (22,'song',77,105);
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from cj;
+----+-----+----------+-------+------+
| id | age | name | score | addr |
+----+-----+----------+-------+------+
| 1 | 17 | zhangsan | 60 | 101 |
| 2 | 19 | lisi | 89 | 102 |
| 3 | 19 | wangwu | 88 | 103 |
| 4 | 20 | wangerma | 99 | 104 |
| 5 | 22 | song | 77 | 105 |
+----+-----+----------+-------+------+
5 rows in set (0.00 sec)
mysql>
查詢核心庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| auth |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql>
2、修改資料記錄
mysql> select * from cj;
+----+-----+----------+-------+------+
| id | age | name | score | addr |
+----+-----+----------+-------+------+
| 1 | 17 | zhangsan | 60 | 101 |
| 2 | 19 | lisi | 89 | 102 |
| 3 | 19 | wangwu | 88 | 103 |
| 4 | 20 | wangerma | 99 | 104 |
| 5 | 22 | song | 77 | 105 |
+----+-----+----------+-------+------+
5 rows in set (0.00 sec)
mysql> update cj set age=99 where name='zhangsan'; //更新zhangsan的年齡99
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from cj;
+----+-----+----------+-------+------+
| id | age | name | score | addr |
+----+-----+----------+-------+------+
| 1 | 99 | zhangsan | 60 | 101 |
| 2 | 19 | lisi | 89 | 102 |
| 3 | 19 | wangwu | 88 | 103 |
| 4 | 20 | wangerma | 99 | 104 |
| 5 | 22 | song | 77 | 105 |
+----+-----+----------+-------+------+
5 rows in set (0.00 sec)
mysql>
3、刪除資料庫記錄
mysql> delete from cj where name='zhangsan'; //刪除name是zhangsan
Query OK, 1 row affected (0.00 sec)
mysql> select * from cj;
+----+-----+----------+-------+------+
| id | age | name | score | addr |
+----+-----+----------+-------+------+
| 2 | 19 | lisi | 89 | 102 |
| 3 | 19 | wangwu | 88 | 103 |
| 4 | 20 | wangerma | 99 | 104 |
| 5 | 22 | song | 77 | 105 |
+----+-----+----------+-------+------+
4 rows in set (0.00 sec)
mysql>
三、DQL
1、查詢資料記錄
SELECT欄位名1,欄位名2,… FROM 表名WHERE 條件表示式
mysql> select * from users;
ERROR 1146 (42S02): Table 'aaa.users' doesn't exist
mysql> use auth;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | abc123 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql> select user_name from users; //從user表中檢視user_name
+-----------+
| user_name |
+-----------+
| lisi |
| zhangsan |
+-----------+
2 rows in set (0.00 sec)
mysql>
mysql> select user_name,user_passwd from users; //查詢倆個用,號隔開
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | abc123 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql>
四、DCL
1.授予許可權
GRANT許可權列表ON資料庫名.表名 TO使用者名稱@來源地址〔 IDENTIFIED BY‘密碼’]
mysql> grant select on *.* to 'lisi'@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show grants for 'lisi'@'localhost';
+-------------------------------------------+
| Grants for [email protected] |
+-------------------------------------------+
| GRANT SELECT ON *.* TO 'lisi'@'localhost' |
+-------------------------------------------+
1 row in set (0.00 sec)
[[email protected] ~]# mysql -ulisi -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20 Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show grants for 'lisi'@'localhost';
+-------------------------------------------+
| Grants for [email protected] |
+-------------------------------------------+
| GRANT SELECT ON *.* TO 'lisi'@'localhost' |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql>
臨時表
在資料庫裡臨時建立的表,記錄當下的短暫資料,退出資料庫後,表將消失
mysql> use aaa; //進入aaa資料庫
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create temporary table song (id int(3) not null,name varchar(64) not null); //建立臨時表
Query OK, 0 rows affected (0.00 sec)
mysql> show tables; //查看錶 沒有song表的存在
+---------------+
| Tables_in_aaa |
+---------------+
| cj |
+---------------+
1 row in set (0.00 sec)
mysql> insert into song values (1,'shu'),(2,'feng'); //給song表新增屬性
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from song; //能臨時看到表中的資料
+----+------+
| id | name |
+----+------+
| 1 | shu |
| 2 | feng |
+----+------+
2 rows in set (0.00 sec)
mysql>
克隆表
mysql> create table cj2 like cj;; //從cj表拷貝結構給cj2
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+---------------+
| Tables_in_aaa |
+---------------+
| cj |
| cj2 |
+---------------+
2 rows in set (0.00 sec)
mysql> insert into cj2 select * from cj; //拷貝cj表上的內容給cj2
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from cj2; //檢視cj2的內容
+----+-----+----------+-------+------+
| id | age | name | score | addr |
+----+-----+----------+-------+------+
| 2 | 19 | lisi | 89 | 102 |
| 3 | 19 | wangwu | 88 | 103 |
| 4 | 20 | wangerma | 99 | 104 |
| 5 | 22 | song | 77 | 105 |
+----+-----+----------+-------+------+
4 rows in set (0.00 sec)
mysql>