如今,數(shù)據(jù)庫管理系統(tǒng)的圖形化操作使用起來非常的便捷。但是,作為一名計算機開發(fā)人員,掌握一些sql語句,也是很有必要的。因此,小王整理了一些有關Mysql數(shù)據(jù)庫的sql語句。
數(shù)據(jù)庫相關操作
-
create database 數(shù)據(jù)庫名稱 default charset 'UTF8';
作用:創(chuàng)建數(shù)據(jù)庫,指定字符編碼
-- 創(chuàng)建指定命令、指定編碼的數(shù)據(jù)庫
create database xiaowangdb01 default charset 'UTF8';
-
drop database 數(shù)據(jù)庫名稱;
作用:刪除指定數(shù)據(jù)庫
-- 刪除數(shù)據(jù)庫
drop database xiaowangdb01;
-
use 數(shù)據(jù)庫名稱;
作用:指定數(shù)據(jù)庫
-- 指定數(shù)據(jù)庫
use xiaowangdb01;
-
create table 數(shù)據(jù)表名稱(...);
作用:創(chuàng)建數(shù)據(jù)表
-- 創(chuàng)建專題表
create table subjects(
-- int類型
-- primary key主鍵:可以表示一個數(shù)據(jù)的屬性
-- auto_increment:自動增長
id int primary key auto_increment comment '專題編號';
-- not null:表示這個屬性增加數(shù)據(jù)時不能為空
title varchar(50) not null comment '專題標題';
intro text comment '專題信息'
)
-
drop table 數(shù)據(jù)表名稱;
作用:按照指定名稱,直接刪除數(shù)據(jù)表
-- 刪除專題數(shù)據(jù)表
drop table subjects;
-
insert into users(....) values(...);
作用:增加數(shù)據(jù)
-- 增加用戶數(shù)據(jù)
insert into users(username,userpass,nickname) values("tom","123","湯姆");
-
delete from 數(shù)據(jù)庫名稱 where key = value;
作用:刪除符合條件的數(shù)據(jù)
-- 刪除用戶表中編號為1的數(shù)據(jù)
delete from users where id=1;
-
update 數(shù)據(jù)表 set key1 = value1,key2 = value2 where key = value;
作用:修改語句
注意:慎重使用,必須非常明確where后面的條件是否準確!
-- 修改用戶表中`tom`的相關數(shù)據(jù)
update users set age = 18,email = "tom@163.com" where username = "tom";
-
select * from 表名稱;
作用:查詢表中的所有數(shù)據(jù)
-- 查詢emp表中的所有數(shù)據(jù)
select * from emp;
-
select 列1,列2,列3... from 數(shù)據(jù)表名稱;
作用:查詢表中的指定屬性/列數(shù)據(jù)
-- 查詢emp中的員工名稱、薪水、獎金
select ename, salary, comm from emp;
-
select 列 as 別名 from 表名稱;
作用:查詢數(shù)據(jù)時,可以讓查詢結果按照指定名稱顯示(as可以省略)
-- 查詢emp表中的姓名,工資并將其作為列名稱展示
select ename as 姓名, salary as 工資 from emp;
-
select * from 數(shù)據(jù)表名稱 order by 列(需要排序的列)desc(倒序) | asc(順序);
作用:按照想要的順序進行排列
-- 將emp表按照薪水倒序排列
select * from emp order by salary desc;
-
select * from 表名稱 limit 起始id, 查詢條數(shù);
作用:數(shù)據(jù)量較大的情況下,可以每次查詢一部分數(shù)據(jù),按照分頁的方式查看
-- 起始id = (當前頁碼 - 1)*查詢條數(shù)
-- 第一頁,每頁顯示5條數(shù)據(jù)
select * from emp limit 0, 5;
-
select * from 數(shù)據(jù)表名稱 where 列 like '%值%';
作用:按照數(shù)據(jù)的一部分進行模糊搜索
-- %:模糊匹配任意一個或者多個字符
-- _:模糊匹配任意一個字符
select * from emp where ename like '%王%'
-
select * from 數(shù)據(jù)表名稱 where 條件;
作用:查詢數(shù)據(jù)的同時,附加一定的條件進行數(shù)據(jù)過濾
-- 單條件查詢
-- 工資大于10000的人
select * from emp where salary > 10000
-- 多條件查詢
-- 工資大于10000同時獎金超過10000的人->and
select * from emp where salary > 10000 and comm > 10000;
-- 工資大于等于20000或者獎金大于等于18000的人-> or
select * from emp where salary >= 20000 or comm >= 18000
-
select * from 表1, 表2 where 關聯(lián)條件 and 其他條件;
作用:查詢兩張表的數(shù)據(jù),得到一個完整信息
-- 查詢小王屬于哪個部門
-- select depid from emp where ename = "林沖";
-- select * from dept where id = 4;
-- 完整語法
select emp.id, emp.ename, dept.dname, dept.dloc from emp, dept where emp.deptid = dept.id and emp.ename = "林沖";
-- 別名改造
select e.id, e.ename, d.dname, d,dloc from emp e, dept d where e.deptid = d.id and e.ename = "林沖";
-
select * from A left join B on 條件A = 條件B;
select * from A right join B on 條件A = 條件B;
作用:通過左連接或者右鏈接查詢兩張表的完整數(shù)據(jù)
注意:Left Join影響到的是右邊的表,Right Join影響到的是左邊的表。
-- 左(外)連接查詢:以左表為主
select * from dept d left join emp e on d.id = e.depid
-- 右(外)連接查詢:以右表為主
select * from emp e right join dept d on d.id = e.deptid
-
select * from 表1, 表2;
注意:這是笛卡爾乘積現(xiàn)象,是多表關聯(lián)時出現(xiàn)的問題,如果關聯(lián)條件不變,查詢的結果數(shù)據(jù)就會出現(xiàn)a表數(shù)據(jù)總數(shù)乘以b表數(shù)據(jù)總數(shù)的結果!
-- 笛卡爾乘積
select * from emp,dept;
關于聚合查詢
作用:主要用于統(tǒng)計數(shù)據(jù)
-- 員工總數(shù)
select count(1) as cnt from emp;
-- 最高工資
select max(salary) from emp;
-- 平均工資
select avg(salary) from emp;