1. 程式人生 > >mysql資料庫簡單一些簡單操作和總結

mysql資料庫簡單一些簡單操作和總結

1. mysql 資料庫操作方法:


進入資料庫 mysql -uroot -p
退出 quite exit
預設引擎 innodb
檢視版本 select verison();
檢視時間 select now();
邏輯運算 select 1+1;
查詢資料庫 show databases ;
建立資料庫 create database 庫名 charset = utf8;
刪除 drop database 庫名;
使用某個資料庫 use 庫名;
查詢庫中所有表 show tables;
查看錶結構 desc 表名;
建立表 create table 表名( 增加欄位和資料型別


例如: create table classes(
id int unsigned(無符號的) primary key(主鍵) auto_increment (自動增加) not null(非空),
name varchar(30) not null default(預設)‘ ’
);
修改表名 alter table 表名 rename to 新表名;
刪除表 drop table;
查詢表內容 select * from 表名;
往表中插入資料 insert into 表名 values();
例如 insert into student values(0,'小明'),(0,'小花') ; 零 是佔位。注:有幾個欄位名就插入幾個資料,比如前面的插入了兩個
刪除資料 delete from 表名 where id = 1;
修改資料 update 表名 set 欄位名 = ‘1234’where id = 1; 1234代表新的內容修改ID為1的資料
增加欄位 :
alter table 'student'
add column 'num1' int(3) null default null;
修改欄位 :
alter table'student'
change column 'num1' 'num2' int(3) null default null;
刪除欄位:
alter table 'classes'
drop column 'num';

一個英文字元等於一個位元組,一箇中文三個位元組

2. 連線查詢 join
join 用於多表中欄位之間的聯絡,語法如下:
from table1 inner或者left或者right join table2 on conditiona

3. 使用 as 給欄位起別名(給長的欄位取別名)
select id as code from student; 可以通過 as 給表起別名

在select後面列前使用distinct可以消除重複的行

4. mysql的聯合主鍵: select 欄位1,欄位2 from 表名 where 條件
邏輯運算子

and or not (is 用來判斷null)
5. 範圍查詢
between ...... and.....
查詢年齡在20歲到30歲之間的學生
select name from student where age between 20 and 30
in表示在一個非連續的範圍內

select id from student where id in (1,2,3)
select distinct num from student ;

6 . 模糊查詢 like
%表示任意多個任意字元
_ 表示一個任意字元
查詢姓黃的學生
select * from student where name like ‘黃%’
7. 聚合函式
mysql中五種常用的聚合函式:
(1)max(列名):求最大值。 select max(salary) from salary_tab;
(2)min(列名):求最小值。
(2)sum(列名):求和。
(4)avg(列名):求平均值。select sum(salary) from salary_tab;
(5)count(列名):統計記錄的條數
①count(*):返回表中滿足where條件的行的數量

select count(*) from salary_tab where salary='1000';


8. 排序
為了方便檢視資料,可以對資料進行排序
語法:order by 欄位 asc 或者 desc
查詢學生資訊,按學號降序
select * from student order by id desc
9.分組
分組SELECT的基本格式:

  select [聚合函式] 欄位名 from 表名

    [where 查詢條件]

    [group by 欄位名]

    [having 過濾條件]
指定一個列進行分組

select salary,count(*) from salary_tab
指定多個分組列,‘大組中再分小組’

select userid,count(salary) from salary_tab
根據表示式分組

select year(payment_date),count(*)
10. having字句
對分組結果進行過濾
having子語句與where子語句區別
where子句在分組前對記錄進行過濾;

  having子句在分組後對記錄進行過濾
11. limit 分段取值

語法 limi m,n select * from student limit 0,2 從第一個開始取,取兩個,如果不寫第一個引數,就是預設從第一個引數取值,取n個
12. mysql外來鍵約束
建表時生成外來鍵 foreign key('sid') references'student'('id');
建表時新增外來鍵 alter table'course_student' add foreige key('sid') references'student' (''id);
刪除外來鍵 alter table'course_student'drop foreign key'course_student_idfk_1';
13. mlsql自關聯
select * from areas as p inner join areas as c on c.pid = p.aid where p.atile = '河南省';
14.自關聯
標量子查詢
select * from arrticle where uid = (select uid from user where status = 1 order by uid desc limit 1);
列子查詢
select * from student where cls id in (select id from classes where id in (1,2))
行子查詢
示例:查詢一個班身高最高,歲數最大的學生

select * from student where(age,height) = (select max(age),max(height) from student);
15.檢視
建立檢視 create view 檢視名稱 as select 語句
建議名稱以v_開頭,用來和普通表區分, 使用show tables 可以顯示檢視
使用檢視 select * from 檢視名稱
刪除檢視 drop view 檢視名稱
修改檢視 create or replace view 檢視名稱 as sql語句
16.事務
開啟事務後執行修改命令
begin 或者start transaction;
提交事務 commit;
回滾事務 rollback
17.索引
使用索引 select * from test_index where name = 'ca-900';
聯合索引 select * from test_index where name = 'ca-900' and id = 'ha-900';

 

以上是學習資料庫以來簡單的總結