Linux—mysql的sql語言單表操作
表操作
- DROP TABLE [IF EXISTS] 'tbl_name';
- ALTER TABLE 'tbl_name' ###增加欄位
欄位:
- 新增欄位: add
- ADD col1 data_type [FIRST|AFTER col_name]
刪除欄位: drop
修改欄位:
- alter(預設值) , change(欄位名) , modify(欄位屬性)
索引:
- 新增索引: add index
- 刪除索引: drop index
表選項
修改:
- 查看錶上的索引: SHOW INDEXES FROM [db_name.]tbl_name;
- 檢視幫助: Help ALTER TABLE
例子: 原表
MariaDB [db1]> desc students; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(50) | NO | | NULL | | | sex | enum('f','m') | YES | | NULL | | | age | tinyint(3) unsigned | YES | | 20 | | +-------+---------------------+------+-----+---------+----------------+
增加欄位後的表:
MariaDB [db1]> alter table students add mobile char(11) after name; MariaDB [db1]> desc students; +--------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(50) | NO | | NULL | | | mobile | char(11) | YES | | NULL | | | sex | enum('f','m') | YES | | NULL | | | age | tinyint(3) unsigned | YES | | 20 | | +--------+---------------------+------+-----+---------+----------------+ MariaDB [db1]> show indexes from students\G ###查看錶上的索引資訊 MariaDB [db1]> alter table students add index(name); ###新增索引資訊
DML語句 資料表的操作
DML: INSERT, DELETE, UPDATE
insert 增加資料庫
delete 刪除資料庫
update 修改資料庫
INSERT:
一次插入一行或多行資料
語法
INSERT [L OW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] 優先順序
[INTO] tbl_name [(col_name,...)] 賦值欄位的順序
{VALUES | VALUE} ({expr | DEFAULT},...),(...),... 值的順序
[ ON DUPLICATE KEY UPDATE 如果重複更新之
col_name=expr
[, col_name=expr] ... ]
簡化寫法:
INSERT tbl_name [(col1,...)] VALUES (val1,...), (val21,...)
例子:
MariaDB [db1]> insert into students(name,mobile,sex,class,age)values('mage','10010','m',18,25);
MariaDB [db1]> insert into students (name,mobile,sex,class,age)values('馬哥','120','f',18,45);
+----+--------+---------+------+-------+------+
| id | name | mobile | sex | class | age |
+----+--------+---------+------+-------+------+
| 1 | mage | 10010 | m | 127 | 25 |
| 2 | mage | 10010 | m | 18 | 25
6 | wang | 1380000 | m | 127 | 48 |
| 7 | 馬哥 | 120 | f | 18 | 45 |
+----+--------+---------+------+-------+------+
跨資料庫表複製資料方法:
MariaDB [db1]> insert into students(name,mobile,sex,age,)select name,mobile,sex,age from hellodb.students;
增加資料的另一中寫法:
MariaDB [db1]> insert into students set name='han',sex='m';
MariaDB [db1]>select * from students;
UPDATE:修改語句
UPDATE:修改資料庫
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
注意:一定要有限制條件,否則將修改所有行的指定欄位
限制條件:WHERE LIMIT
Mysql 選項: -U|--safe-updates| --i-am-a-dummy
MariaDB [db1]> update students set sex='f' where id=9;
| 9 | han | NULL | f | NULL | 20 |
+----+--------+---------+------+-------+------+
注意:如果不指定 where 條件 則所有欄位內容全部修改
修改多個欄位
MariaDB [db1]> update students set name='li',sex='m',class='16' where id=3;
****為了避免使用者修改檔案是忘記加where條件導致資料全部修改 我們可以修改配置檔案;***
[[email protected] my.cnf.d]# vim mysql-clients.cnf
[mysql]
safe-updates
delete 刪除語句
DELETE:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition]
[ORDER BY ...] 排序prder by
[LIMIT row_count] 刪除的指定條數;
可先排序再指定刪除的行數
注意:一定要有限制條件,否則將清空表中的所有資料
限制條件:WHERE LIMIT
TRUNCATE TABLE tbl_name; 清空表
rruncate table students;
例子:
MariaDB [db1]> delete from students where id=9;
limit的用法
MariaDB [db1]> delete from students limit 3 ;
order by 的用法 先排序後刪除
MariaDB [db1]> delete from students order by age limit 1 ;
先按照年齡排序,排完序,排完之後在刪除。
SELECT查詢語句
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[SQL_CACHE | SQL_NO_CACHE]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[FOR UPDATE | LOCK IN SHARE MODE]
- 欄位顯示可以使用別名:
col1 AS alias1, col2 AS alias2, ...
例子:
MariaDB [hellodb]> select stuid as 學員編號,name 姓名,age 年齡 from students;
+--------------+---------------+--------+
| 學員編號 | 姓名 | 年齡 |
+--------------+---------------+--------+
| 1 | Shi Zhongyu | 22 |
| 2 | Shi Potian | 22 |
| 3 | Xie Yanke | 53 |
| 4 | Ding Dian | 32 |
| 5 | Yu Yutong | 26 |
| 6 | Shi Qing | 46 |
| 7 | Xi Ren | 19 |
- WHERE子句:指明過濾條件以實現“選擇”的功能:
過濾條件:布林型表示式
算術操作符: +, -, *, /, %
比較操作符: =,<=>(相等或都為空) , <>, !=(非標準SQL), >, >=, <, <=
BETWEEN min_num AND max_num
IN (element1, element2, ...)
IS NULL
IS NOT NULL
例子:
where 用法
MariaDB [hellodb]> select * from students where classid=1;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
+-------+--------------+-----+--------+---------+-----------+
MariaDB [hellodb]> select * from students where age > 30;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+--------------+-----+--------+---------+-----------+
> < 大於小於號用法
MariaDB [hellodb]> select * from students where age > 30 and age <50;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
+-------+--------------+-----+--------+---------+-----------+
between
MariaDB [hellodb]> select * from students where age between 30 and 50;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
+-------+--------------+-----+--------+---------+-----------+
in
MariaDB [hellodb]> select * from students where classid in (1,2,3);
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
is null
MariaDB [hellodb]> select * from students where teacherid is null ;
MariaDB [hellodb]> select * from students where teacherid is not null ;
2、DISTINCT 去除重複列
SELECT DISTINCT gender FROM students;
MariaDB [hellodb]> select distinct classid from students; ##去重
+---------+
| classid |
+---------+
| 2 |
| 1 |
| NULL |
+---------+
3、LIKE:
- %: 任意長度的任意字元
- _:任意單個字元
例子:
MariaDB [hellodb]> select * from students where name like 's%';
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+-------------+-----+--------+---------+-----------+
MariaDB [hellodb]> select * from students where name like '%o';
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
+-------+-----------+-----+--------+---------+-----------+
4、RLIKE:正則表示式,索引失效,不建議使用
MariaDB [hellodb]> select * from students where name rlike '^s';
5、REGEXP:匹配字串可用正則表示式書寫模式,同上
6、邏輯操作符:
NOT
AND
OR
XOR
order by 排序
ORDER BY: 根據指定的欄位對查詢結果進行排序
升序: ASC
降序: DESC
例子:
MariaDB [hellodb]> select * from students order by age;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
6 | Shi Qing | 46 | M | 5 | NULL |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
MariaDB [hellodb]> select * from students order by age desc; ##倒序排序
LIMIT [[offset,]row_count]:對查詢的結果進行輸出行數數量限制
MariaDB [hellodb]> select * from students order by name limit 3;
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 15 | Duan Yu | 19 | M | 4 | NULL |
+-------+-----------+-----+--------+---------+-----------+
MariaDB [hellodb]> select * from students order by age asc limit 2,3;
****** 跳過前2個只顯示後三個****
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
+-------+--------------+-----+--------+---------+-----------+
group的用法
GROUP:根據指定的條件把查詢結果進行“分組”以用於做“聚合”運算
avg(), max(), min(), count(), sum()
HAVING: 對分組聚合運算後的結果指定過濾條件
count(*) ##檢索到行中非空值的數目。
MariaDB [hellodb]> select classid,count(*) from students group by classid ;
+---------+----------+
| classid | count(*) |
+---------+----------+
| NULL | 2 |
| 1 | 4 |
| 2 | 3 |
| 3 | 4 |
| 4 | 4 |
| 5 | 1 |
| 6 | 4 |
| 7 | 3 |
+---------+----------+
例子:
MariaDB [hellodb]> select count(*) from students; 統計students表中的行數
MariaDB [hellodb]> select max(age) from students; 統計最大年齡
MariaDB [hellodb]> select min(age) from students; 統計最小年齡
MariaDB [hellodb]> select courseid,avg(score) from scores where courseid=2 ;
+----------+------------+
| courseid | avg(score) |
+----------+------------+
| 2 | 75.2500 |
+----------+------------+
求和
MariaDB [hellodb]> select courseid,sum(score) from scores where courseid=2 ;
+----------+------------+
| courseid | sum(score) |
+----------+------------+
| 2 | 301 |
+----------+------------+
MariaDB [hellodb]> select classid,age,avg(age) from students group by classid ;
先對班級進行分組,分完組之後在在對年齡進行求平均值 分組可以理解成去重
+---------+-----+----------+
| classid | age | avg(age) |
+---------+-----+----------+
| NULL | 27 | 63.5000 |
| 1 | 22 | 20.5000 |
| 2 | 22 | 36.0000 |
| 3 | 26 | 20.2500 |
| 4 | 32 | 24.7500 |
| 5 | 46 | 46.0000 |
| 6 | 20 | 20.7500 |
| 7 | 17 | 19.6667 |
+---------+-----+----------+
having的用法;適合在查詢表中做了group by 分組後使用
MariaDB [hellodb]> select classid,avg(age) from students group by classid having classid in (1,2) ;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| 1 | 20.5000 |
| 2 | 36.0000 |
+---------+----------+
檢視students表中classid和年齡 ,並對年齡進行平均計算,並對classid進行分組,分組後對平均年齡進行別名的明名,最後顯示平均年齡大於30的班級。
MariaDB [hellodb]> select classid,age,avg(age) as avg from students group by classid having avg > 30 ;
+---------+-----+---------+
| classid | age | avg |
+---------+-----+---------+
| NULL | 27 | 63.5000 |
| 2 | 22 | 36.0000 |
| 5 | 46 | 46.0000 |
+---------+-----+---------+
(5) 顯示TeacherID非空的同學的相關資訊
(6) 以年齡排序後,顯示年齡最大的前10位同學的資訊
(7) 查詢年齡大於等於20歲,小於等於25歲的同學的資訊
第五題:
MariaDB [hellodb]> select stuid,name,teacherid from students where teacherid is not null ;
第六題:
MariaDB [hellodb]> select stuid,name,age from students order by age desc limit 10 ;
+-------+--------------+-----+
| stuid | name | age |
+-------+--------------+-----+
| 25 | Sun Dasheng | 100 |
| 3 | Xie Yanke | 53 |
| 6 | Shi Qing | 46 |
| 13 | Tian Boguang | 33 |
| 4 | Ding Dian | 32 |
| 24 | Xu Xian | 27 |
| 5 | Yu Yutong | 26 |
| 17 | Lin Chong | 25 |
| 23 | Ma Chao | 23 |
| 18 | Hua Rong | 23 |
+-------+--------------+-----+
第七題;
MariaDB [hellodb]> select stuid,name,age,avg(age) from students where age >= 20 and age <=25 ;
+-------+-------------+------+----------+
| stuid | name | age | avg(age) |
+-------+-------------+------+----------+
| 1 | Shi Zhongyu | 22 | 22.1000 |
+-------+-------------+------+----------+
MariaDB [hellodb]> select classid,gender,count(*) from students group by classid,gender;
+---------+--------+----------+
| classid | gender | count(*) |
+---------+--------+----------+
| NULL | M | 2 |
| 1 | F | 2 |
| 1 | M | 2 |
| 2 | M | 3 |
| 3 | F | 3 |
| 3 | M | 1 |
| 4 | M | 4 |
| 5 | M | 1 |
| 6 | F | 3 |
| 6 | M | 1 |
| 7 | F | 2 |
| 7 | M | 1 |
+---------+--------+----------+