1.mysql常用命令
顯示當前mysql服務器上的所有資料庫
show databases;
選擇要使用的資料庫
use 資料庫名;
顯示當前資料庫中的所有表
show tables;
顯示當前使用的資料庫
select database();
查看表結構
desc 表名;
查看表中所有資料
select * from 表名;
2.資料庫操作命令
創建資料庫同時指定默認的字符集
create database 資料庫名 default CHARACTER set utf8;
洗掉資料庫
drop database 資料庫名;
3.mysql常用資料型別
整數: int bigint
小數 decimal 長度 精度 decimal(18,2)
字符 char(長度) varchar(長度)
char(8) 固定保存8位長度的字串
varchar(8) 最多保存長度為8的字串
日期 date time datetime
二進制 blob
列舉 enum 從固定取值范圍的資料中取一個 比如性別
集合 set 從固定取值范圍的資料中取一個或多個 比如不定項選擇題的答案
4.表操作命令
建表
create table 表名
(
列名 資料型別,
列名 資料型別,
.......
列名 資料型別
)
洗掉表
drop table 表名;
5.資料操作命令
5.1 增加資料
給表中指定的列賦值
insert into 表名(列名,列名,列名) values(值,值,值);
給表中的所有列賦值
insert into 表名 values(值,值,值,值,值);
5.2 洗掉資料
delete from 表名 where 條件
例子:
洗掉所有答案是A的記錄
delete from question where answer='A'
洗掉question表中的所有記錄
delete from question;
洗掉答案帶A的記錄
delete from question where ansswer like '%A%'
洗掉答案不帶A的記錄
delete from question where ansswer not like '%A%'
5.3 運算子
關系運算子
> < >= <= = != <>
邏輯運算子
and or not
like 模糊匹配
% 任意字符 '張%' 所有姓張的
_ 一個任意字符 ‘張_’ 姓張的姓名只有兩個字的
5.4修改資料
update 表名 set 列名=值,列名=值 where 條件
修改題目位3+2=?的A選項和B選項
update question set optiona='aaaaaa',optionb='bbbbb' where title='3+2=?'
5.4 查詢資料
1 簡單篩選
select 列名,列名,列名 from 表名 where 條件
2.排序
select 列名,列名.... from 表名 where 條件 order by 列名 asc/desc;
3.模糊匹配
like
in
select * from employees where emp_no in(499999,499572,499525);
select * from employees where emp_no=499999 or emp_no=499572 or emp_no=499525;
between and
查詢出生日期在1960-1-1 到 1960-7-1 之間的員工資訊
select * from employees where birth_date between '1960-1-1' and '1960-7-1'
4.聚合函式
max 最大值 max(列名) max(運算式)
min 最小值
sum 總和
avg 平均值
count 計數
select max(列名) from 表名 where 條件
error: select * from 表名 where max(emp_no);
error: select * from 表名 where emp_no=max(emp_no)
select * from employees where emp_no =
(
select max(emp_no) from employees
)
5. 分組
select count(*),gender from employees group by gender;
6.復雜查詢(嵌套)
select ....
from .....
where ..... 在分組之前對資料進行篩選(不能使用聚合函式)
group by ....
having ..... 對分組后的資料進行篩選(可以使用聚合函式)
order by.......
7.聯合查詢
1. 內部聯結
A
select first_name,last_name,dept_name,from_date,to_date
from employees,departments,dept_emp
where employees.emp_no =dept_emp.emp_no
and dept_emp.dept_no=departments.dept_no
and employees.emp_no=10003
and now() between from_date and to_date;
B
select first_name,last_name,dept_name,from_date,to_date
from employees
inner join dept_emp on employees.emp_no=dept_emp.emp_no
inner join departments on departments.dept_no=dept_emp.dept_no
where employees.emp_no=10003
and now() between from_date and to_date;
2.左外部聯結
left outer join
3.右外部聯結
right outer join
4 完整外部連接 full join
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/226941.html
標籤:其他
