數據庫的表操作
- 插入 insert
表結構
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ip | int(11) | YES | | NULL | |
| port | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
完整插入insert into 表名 values (值1.值2);
insert into test.service values(100,‘80’);
指定字段插入數據
insert into 表名(字段1,字段2) values (值1.值2);
插入多條記錄
insert into 表名 values (值1.值2),(值1.值2);
2,查詢 select
表結構
+----+-------+------------+----------+--------+--------+
| id | name | hire_date | salary | office | dep_id |
+----+-------+------------+----------+--------+--------+
| 1 | jack | 2019-07-11 | 5000.00 | 501 | 100 |
| 2 | ma | 2019-07-11 | 6000.00 | 501 | 100 |
| 3 | alice | 2019-07-11 | 7000.00 | 501 | 100 |
| 4 | tian | 2019-07-11 | 14000.00 | 502 | 101 |
| 5 | harry | 2019-01-11 | 3000.00 | 502 | 101 |
| 6 | zhu | 2019-07-11 | 2200.00 | 503 | 102 |
+----+-------+------------+----------+--------+--------+
單表查詢
簡單查詢
select * from enploy;
去重:
mysql> select distinct office from enploy;
+--------+
| office |
+--------+
| 501 |
| 502 |
| 503 |
+--------+
單條件查詢
單條件查詢
mysql> select name,salary from enploy where salary>5000;
+-------+----------+
| name | salary |
+-------+----------+
| ma | 6000.00 |
| alice | 7000.00 |
| tian | 14000.00 |
+-------+----------+
多條件查詢
mysql> select name ,salary,office from enploy where salary>5000 AND office=501;
+-------+---------+--------+
| name | salary | office |
+-------+---------+--------+
| ma | 6000.00 | 501 |
| alice | 7000.00 | 501 |
+-------+---------+--------+
集合查詢in
mysql> select name ,salary from enploy where salary in (5000,6000,7000);
+-------+---------+
| name | salary |
+-------+---------+
| jack | 5000.00 |
| ma | 6000.00 |
| alice | 7000.00 |
+-------+---------+
關鍵字模糊查詢like
'% '表示任意多個字符
'_ '表示任意一個字符
mysql> select name,salary from enploy where name like 'al%';
+-------+---------+
| name | salary |
+-------+---------+
| alice | 7000.00 |
+-------+---------+
排序
單列排序 ORDER BY
DESC 降序
AESC 升序
按時間降序
mysql> select * from enploy ORDER BY hire_date DESC;
+----+-------+------------+----------+--------+--------+
| id | name | hire_date | salary | office | dep_id |
+----+-------+------------+----------+--------+--------+
| 1 | jack | 2019-07-11 | 5000.00 | 501 | 100 |
| 2 | ma | 2019-07-11 | 6000.00 | 501 | 100 |
| 3 | alice | 2019-07-11 | 7000.00 | 501 | 100 |
| 4 | tian | 2019-07-11 | 14000.00 | 502 | 101 |
| 6 | zhu | 2019-07-11 | 2200.00 | 503 | 102 |
| 5 | harry | 2019-01-11 | 3000.00 | 502 | 101 |
+----+-------+------------+----------+--------+--------+
取排序後的前幾位limit
mysql> select * from enploy ORDER BY hire_date DESC limit 5;
分組查詢:把dep_id相同的name組成一個組
mysql> select dep_id,group_concat(name) from enploy group by dep_id;
+--------+--------------------+
| dep_id | group_concat(name) |
+--------+--------------------+
| 100 | jack,ma,alice |
| 101 | tian,harry |
| 102 | zhu |
+--------+--------------------+
字符串匹配的方式:
where name = ‘alice’
where name like ‘ali%’
where name regexp ‘^ali’
多表查詢
兩個表數據
mysql> select * from employe;
+--------+----------+------+--------+
| emp_id | emp_name | age | dep_id |
+--------+----------+------+--------+
| 1 | tianyun | 19 | 200 |
| 2 | tom | 26 | 201 |
| 3 | jack | 30 | 201 |
| 4 | alice | 24 | 202 |
| 5 | robin | 40 | 200 |
| 6 | natasha | 28 | 204 |
+--------+----------+------+--------+
mysql> select * from department;
+--------+----------+
| dep_id | dep_name |
+--------+----------+
| 200 | hr |
| 201 | it |
| 202 | sale |
| 203 | fd |
+--------+----------+
內連接:只找出匹配的行(沒有natasha)
mysql> select employe.emp_name,employe.age,employe.dep_id,department.dep_name from employe,department where employe.dep_id = department.dep_id;
+----------+------+--------+----------+
| emp_name | age | dep_id | dep_name |
+----------+------+--------+----------+
| tianyun | 19 | 200 | hr |
| tom | 26 | 201 | it |
| jack | 30 | 201 | it |
| alice | 24 | 202 | sale |
| robin | 40 | 200 | hr |
+----------+------+--------+----------+
左,右連接查詢
語法:select 字段列表 from 表1 left | right join 表2 on 表1.字段 = 表2.字段;
找出所有員工及所屬部門,包括沒有部門的員工
mysql> select emp_id,emp_name,dep_name from employe left join department on employe.dep_id = department.dep_id;
+--------+----------+----------+
| emp_id | emp_name | dep_name |
+--------+----------+----------+
| 1 | tianyun | hr |
| 5 | robin | hr |
| 2 | tom | it |
| 3 | jack | it |
| 4 | alice | sale |
| 6 | natasha | NULL |
+--------+----------+----------+
找出所有部門包含的員工,包括空部門
mysql> select emp_id,emp_name,dep_name from employe right join department on employe.dep_id = department.dep_id;
+--------+----------+----------+
| emp_id | emp_name | dep_name |
+--------+----------+----------+
| 1 | tianyun | hr |
| 2 | tom | it |
| 3 | jack | it |
| 4 | alice | sale |
| 5 | robin | hr |
| NULL | NULL | fd |
+--------+----------+----------+
複合查詢
找出所有部門年齡大於25的員工
mysql> select emp_id,emp_name,age,dep_name from employe,department where employe.dep_id = department.dep_id and age>25;
+--------+----------+------+----------+
| emp_id | emp_name | age | dep_name |
+--------+----------+------+----------+
| 2 | tom | 26 | it |
| 3 | jack | 30 | it |
| 5 | robin | 40 | hr |
+--------+----------+------+----------+
帶比較運算符的子查詢
查詢年齡大於等於25歲員工所在部門
mysql> select dep_id,dep_name from department where dep_id IN (select distinct dep_id from employe where age >= 25);
+--------+----------+
| dep_id | dep_name |
+--------+----------+
| 201 | it |
| 200 | hr |
+--------+----------+
帶exists關鍵字子查詢
exists存在時,內層查詢返回true和false,爲true,外層查詢語句進行查詢,false不查詢。
mysql> select * from employe
-> where exists (select * from department where dep_id = 203);
+--------+----------+------+--------+
| emp_id | emp_name | age | dep_id |
+--------+----------+------+--------+
| 1 | tianyun | 19 | 200 |
| 2 | tom | 26 | 201 |
| 3 | jack | 30 | 201 |
| 4 | alice | 24 | 202 |
| 5 | robin | 40 | 200 |
| 6 | natasha | 28 | 204 |
+--------+----------+------+--------+
6 rows in set (0.00 sec)
mysql> select * from employe where exists (select * from department where dep_id = 300);
Empty set (0.00 sec)
mysql 索引
創建索引
創建表的時候創建索引(插入數據慢)
create 在已創建表上加索引
查看並測試索引
刪除索引
-
創建表
mysql> create table t3(id int ,name varchar(10));
-
創建存儲過程
mysql> delimiter $$ //改變結束符爲$$ delimiter == \d mysql> create procedure autoinsert1() //創建存儲過程 -> BEGIN //開始 -> declare i int default 1; //設定一個變量默認值爲1 -> while (i<50000)do -> insert into company.t3 values (i,'aaa'); -> set i=i+1; -> end while; -> END$$ //結束
-
查看存儲過程
mysql> show create procedure autoinsert1\G *************************** 1. row *************************** Procedure: autoinsert1 sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `autoinsert1`() BEGIN declare i int default 1; while (i<50000)do insert into company.t3 values (i,'aaa'); set i=i+1; end while; END character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: latin1_swedish_ci 1 row in set (0.00 sec)
-
調用函數
mysql> call autoinsert1();
創建索引
-
創建表時創建索引
語法:
create table表名(
字段1 數據類型,
字段2 數據類型,
[UNIQUE | FULLTEXT | SPATIAL ] INDEX | KEY [索引名] (字段名[(長度)] [ASC|DESC])
)創建普通索引 create table t1( dep_id int, dep_name varchar(30), comment varchar(30), INDEX ~~別名1~~ (dep_name) ); 創建唯一索引 create table t2( dep_id int, dep_name varchar(30), comment varchar(30), UNIQUE INDEX ~~別名1~~ (dep_name) ); 創建全文索引 create table department1( dep_id int, dep_name varchar(30), comment varchar(30), log text, FULLTEXT INDEX ~~別名1~~ (log) ); 創建多列索引 create table department1( dep_id int, dep_name varchar(30), comment varchar(30), log text, INDEX ~~別名1~~ (dep_name,comment) );
-
在存在的表上創建索引
語法:
create [UNIQUE | FULLTEXT | SPATIAL ] INDEX 索引名 ON 表名 (字段名[(長度)] [asc|desc]);創建普通索引 create index comment_index on t1(comment); 創建唯一索引 create UNIQUE INDEX comment_index on t1(comment); 創建全文索引 create FULLTEXT INDEX log_index on department1(log); 創建多例索引 create INDEX dep_name_comment ON department1(dep_name,comment);
注:創建標的時候創建索引,可以不加索引名,這時索引名爲字段名,添加索引時候,一定要加索引名
查看索引
語法:
show create table 表名\G
mysql> show create table t3;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| t3 | CREATE TABLE `t3` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
-
刪除索引
語法:
drop index 索引名 ON 表名;mysql> drop index id_index on t3; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
查詢對比
沒有索引
看怎麼查
mysql> explain select * from t3 where id=49000\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t3
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 50344
Extra: Using where
1 row in set (0.00 sec)
看查詢時間
mysql> select * from t3 where id=49000;
+-------+------+
| id | name |
+-------+------+
| 49000 | aaa |
+-------+------+
1 row in set (0.01 sec)
有索引
怎麼查
mysql> explain select * from t3 where id = 49000\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t3
type: ref
possible_keys: id_index
key: id_index
key_len: 5
ref: const
rows: 1
Extra: NULL
1 row in set (0.00 sec)
時間
mysql> select * from t3 where id = 49000;
+-------+------+
| id | name |
+-------+------+
| 49000 | aaa |
+-------+------+
1 row in set (0.00 sec)
注:索引查詢會讓查詢速度更快,查詢的時候要帶上索引名條件
mysql 視圖
視圖其實是一個自定義虛擬表,不在數據庫中以存儲的數據形式存在,而是對已存在的數據拼湊出來的新表,便於查詢。
-
創建視圖
語法:create view 視圖名
as select語句;兩張表 mysql> select * from product; +----+--------+-------+ | id | name | price | +----+--------+-------+ | 1 | apple | 10 | | 2 | orange | 5 | | 3 | pear | 12 | +----+--------+-------+ 3 rows in set (0.00 sec) mysql> select * from purchase; +----+--------+----------+---------------------+ | id | name | quantity | gen_time | +----+--------+----------+---------------------+ | 1 | apple | 8 | 2019-07-26 15:28:44 | | 2 | orange | 5 | 2019-07-26 15:28:44 | | 3 | pear | 6 | 2019-07-26 15:28:44 | +----+--------+----------+---------------------+ 3 rows in set (0.00 sec) 創建視圖 mysql> create view u AS select product.name,product.price,purchase.quantity,purchase.quantity * product.price as total from product,purchase where product.name=purchase.name; mysql> show tables; +----------------+ | Tables_in_shop | +----------------+ | product | | purchase | | u | +----------------+ 3 rows in set (0.00 sec) mysql> select * from u; +--------+-------+----------+-------+ | name | price | quantity | total | +--------+-------+----------+-------+ | apple | 10 | 8 | 80 | | orange | 5 | 5 | 25 | | pear | 12 | 6 | 72 | +--------+-------+----------+-------+ 修改視圖: 先刪除視圖,drop view 視圖名 再重新創建
mysql觸發器
- 觸發器案例
觸發器的刪除必須通過主鍵刪除,主鍵是唯一的。
數據的更改,觸發器需要將所有的字段重新獲取一遍,通過主鍵獲取所有字段。