mysql 基本操做

1、 數據庫及對象

一、系統數據庫

  • infomation_schema: 存放數據庫對象信息,表信息、列信息,權限信息
  • performance_schema: 數據庫服務器性能參數
  • mysql: 用戶權限信息
  • sys : mysql5.7增長了sys 系統數據庫,經過這個庫能夠快速的瞭解系統的元數據信息

二、操做

- 建立數據庫 :  create database learn;
- 查詢數據庫 :  show databases;
- 選擇數據庫 :  use learn;
- 刪除數據庫 :  drop database learn;

2、 引擎與數據類型

一、引擎

查看引擎:show engines ; -- show engines Gmysql

  • Transactions: 是否支持事務
  • XA: YES 分佈式是否符合XA規範
  • Savepoints: 是否支持事務處理中的保存點

二、數據類型

- 整數類型 :
   - tinyint
   - smallint
   - mediumint
   - int,integer
   - bigint
- 浮點 : 
   - float
   - double
- 定點數類型 :
    - decimal->字符串存儲
    - dec
- 日期 :
    - date 4字節
    - datetime 8字節
    - timestamp 4
    - time 3
    - year 1
- 字符串:
    - char 4
    - varchar 可變0~65535
    - tinytext -> text -> mediumtext -> longtext
    - tinyblob -> blob -> mediumblob -> longblob

3、 表操做

CREATE TABLE t_dept (
  dept_no   INT,
  dept_name VARCHAR(40),
  location  VARCHAR(80)
);
##描述表結構
desc t_dept;
## 查看建立語句
show create table t_dept;
##刪除表
drop table t_dept;
##修改表名
alter table t_dept rename to t_deptartment;

##增長字段
alter table t_deptartment add dept_name_us VARCHAR(40);
##第一個位置增長字段
alter table t_deptartment add first_col VARCHAR(40) first;
## 某列後面添加字段
alter table t_deptartment add after_dept_name VARCHAR(80) after dept_name;
## 刪除字段
alter table t_deptartment drop first_col;
##修改字段 數據類型
alter TABLE t_deptartment MODIFY after_dept_name VARCHAR(100);
## 修改字段名字 能夠一同修改字段屬性
ALTER TABLE t_deptartment CHANGE after_dept_name dept_name_after VARCHAR(10);

4、約束

##建立表的時候直接加入約束
CREATE TABLE t_dept (
  dept_no   INT(20) PRIMARY KEY NOT NULL, ##  非空約束
  type      VARCHAR(20) DEFAULT 'Co., Ltd', ##默認值
  dept_name VARCHAR(100) UNIQUE ##惟一值
);
desc t_dept  ;

##建立有名字的約束值
CREATE TABLE t_dept (
  dept_no   INT(20) AUTO_INCREMENT, ##自增
  type      VARCHAR(20),
  dept_name VARCHAR(100), 
  CONSTRAINT uk_dept_name UNIQUE (dept_name), ##惟一值
  CONSTRAINT pk_dept_no PRIMARY KEY (dept_no) ##一個字段主鍵
);

## 聯合主鍵
CREATE TABLE t_dept1 (
  dept_no   INT(20) AUTO_INCREMENT, ##自增
  type      VARCHAR(20),
  dept_name VARCHAR(100), ##惟一值
  CONSTRAINT uk_dept_name1 UNIQUE (dept_name), ##惟一值
  CONSTRAINT pk_dept_no_name1 PRIMARY KEY (dept_no, dept_name) ##聯合主鍵
);

#外鍵
CREATE TABLE t_employee (
  emp_no int(30) PRIMARY KEY,
  emp_name VARCHAR(20),
  dept_no  INT(20),
  CONSTRAINT fk_dept_no FOREIGN KEY (dept_no) REFERENCES t_dept(dept_no)
);

desc t_employee;

5、索引

1.索引類型

普通索引,惟一索引,全文索引,單列索引,多列索引,空間索引sql

適用: 常常被查詢的字段,分組字段,主鍵或者外鍵。惟一完整性約束數據庫

不適用: 不多查詢;擁有許多重複值的字段服務器

2.語句
#普通索引
CREATE TABLE t_dept_2 (
  dept_no   INT,
  dept_name VARCHAR(20),
  loc       VARCHAR(40),
  INDEX index_dept_no(dept_no)
);

SHOW CREATE TABLE t_dept_2;
## 查看解析計劃
EXPLAIN SELECT *
        FROM t_dept_2
        WHERE dept_no = 1;

## 單首創建索引
CREATE INDEX idx_dept_name
  ON t_dept_2 (dept_name);

ALTER TABLE t_dept_2
  ADD INDEX idx_loc(loc DESC);

## 惟一索引
CREATE TABLE t_dept_3 (
  dept_no INT UNIQUE,
  UNIQUE INDEX idx_dept_no(dept_no)
);
CREATE UNIQUE INDEX idx_dept_name
  ON t_dept_2 (dept_name);
ALTER TABLE t_dept_2
  ADD UNIQUE INDEX idx_loc(loc);

##全文索引
####默認不區分大小寫,只能在數據類型char,varchar,text上建立
CREATE TABLE t_dept_4 (
  dept_no   INT,
  dept_name VARCHAR(20),
  FULLTEXT INDEX idx_dept_no (dept_name)
);
CREATE FULLTEXT INDEX idx_dept_name
  ON t_dept_2 (dept_name);
ALTER TABLE t_dept_2
  ADD FULLTEXT INDEX idx_loc(loc);

##多列索引
CREATE TABLE t_dept_5 (
  dept_no   INT,
  dept_name VARCHAR(20),
  INDEX idx_dept_no (dept_no, dept_name)
);
CREATE INDEX idx_dept_name
  ON t_dept_2 (dept_name);
ALTER TABLE t_dept_2
  ADD INDEX idx_loc(loc);

##刪除索引
DROP INDEX idx_loc
ON t_dept_2;

6、視圖

###建立視圖
CREATE VIEW view_dept_no AS
  SELECT dept_no
  FROM t_dept;
  
select * from t_dept;
insert into t_dept (dept_no, dept_name) VALUES (1,'ccdata');

select * from view_dept_no ;

#查看view
show tables;
show table status from learn like 'view_dept_no';
use information_schema;
select * from views where table_name = 'view_dept_no' \G;

##刪除view
drop view view_dept_no;

##修改視圖
CREATE OR REPLACE VIEW view_dept_name
  AS
    SELECT dept_name
    FROM t_dept;
    
ALTER VIEW view_dept_name AS
  SELECT
    dept_no,
    dept_name
  FROM t_dept;
  
select * from   view_dept_name;
SELECT *
FROM t_dept;

##對視圖的操做
insert into view_dept_name VALUES (2,'apple');
UPDATE view_dept_name set dept_name = 'Google' WHERE dept_no = 2;
delete from view_dept_name WHERE dept_no = 1;

7、觸發器

## 語句
CREATE TRIGGER TRIGGER_NAME
  BEFORE | AFTER TRIGGER_EVENT
    ON TABLE_NAME FOR EACH ROW TRIGGER_STMT
    
## before|after 制定觸發器觸發的時間
## trigger_event 觸發的事件(條件)
## for each row 每行改變都觸發
## trigger_stmt 觸發的語句

##準備
CREATE TABLE department (
  no       INT(12),
  name     VARCHAR(40),
  location VARCHAR(60)
);

CREATE TABLE diary (
  diary_no   INT(12) AUTO_INCREMENT PRIMARY KEY,
  table_name VARCHAR(50),
  diary_time DATETIME
);

## 建立觸發器
CREATE TRIGGER trigger_diary_time
BEFORE INSERT ON department
FOR EACH ROW
  INSERT INTO diary (table_name, diary_time) VALUES ('department', now());

## 測試
INSERT INTO department VALUES (1, 'apple', 'Peking');
SELECT * FROM diary;

## 觸發器中包含多個語句
CREATE TRIGGER trigger_diary_time
BEFORE INSERT ON department
FOR EACH ROW
  BEGIN
    INSERT INTO diary (table_name, diary_time) VALUES ('department', now());
    INSERT INTO diary (table_name, diary_time) VALUES ('department2', now());
  END;

## 建立多個語句時候,書上說;可能被當作結束符?(沒碰到這種狀況)
DELIMITER $$
CREATE TRIGGER trigger_diary_time
BEFORE INSERT ON department
FOR EACH ROW
  BEGIN
    INSERT INTO diary (table_name, diary_time) VALUES ('department', now());
    INSERT INTO diary (table_name, diary_time) VALUES ('department2', now());
  END;
$$
DELIMITER ;

## 查看
SHOW TRIGGERS;

USE information_schema;
SELECT * FROM TRIGGERS;

##刪除
DROP TRIGGER trigger_diary_time;

8、數據操做

##插入數據
INSERT INTO t_dept_2 (dept_no, dept_name, loc) VALUES ('1', 'apple', 'Peking');
INSERT INTO t_dept_2 (dept_no, dept_name) VALUES ('2', 'Google');
INSERT INTO t_dept_2 VALUES ('3', 'Amazon', 'USA');

## 插入多個值
INSERT INTO t_dept_2 VALUES
  ('4', 'Tencent', 'Guangzhou'),
  ('5', '網易', '杭州'),
  ('6', '餓了嗎', '北京');

## 別的錶轉移
INSERT INTO t_dept_2 (dept_name)
  SELECT dept_name
  FROM t_deptartment;

##### 更新
UPDATE T_DEPT_2 SET LOC= '美國' WHERE DEPT_NAME = 'GOOGLE';

##### 刪除
delete from t_dept_2 WHERE dept_no = 6;

9、記錄查詢

## 去重複
SELECT DISTINCT
  DEPT_NAME,
  LOC
FROM T_DEPT_2;

## 數學運算
CREATE TABLE employee (
  no     INT(10),
  name   VARCHAR(20),
  salary DOUBLE(10, 2)
);

INSERT INTO employee VALUES
  (1, 'a', 1234),
  (2, 'b', 2500),
  (3, 'c', 3600),
  (4, 'd', 4500),
  (5, 'e', 7800);

SELECT
  name,
  salary + 100,
  salary - 200,
  salary * 1.2,
  salary / 1000,
  salary % 1000
FROM employee;
關係運算符
運算符 描述

| 大於
< | 小於
= | 等於
!=(<>) | 不等於
= | 大於等於
<= | 小於等於app

邏輯運算符
運算符 描述
AND (&&)
OR (` `)
XOR 異或
NOT(!)
## 兩側都包括
select * from employee WHERE salary BETWEEN 2500 and 4500;
## 空值判斷
select * from employee where salary is null;
## in 語句
select * from employee WHERE name in ('a','b');

## like 語句

select * from  employee WHERE salary like '4%';
select * from  employee WHERE salary like '4%00';
## 這樣查不出來 why? (double 不太同樣?)
select * from  employee WHERE salary like '4_00';
select * from  employee WHERE name like '_bc';

## 排序
select * from employee order by salary ;
select * from employee order by salary DESC;
SELECT * FROM EMPLOYEE ORDER BY SALARY DESC ,NO;

## 限制查詢條數
select * from employee LIMIT 2;
## 指定初始位置 ,顯示條豬
select * from employee LIMIT 2,4;

統計函數

select count(*) from employee;
select count(1) from employee;
select count(salary) from employee; -- 將忽略null值,不參與計數


## 平均值,null值既不計入和值也不計入 個數
select avg(salary) from employee;

select sum(salary) from employee;
select max(salary) from employee;
select min(salary) from employee;

## 分組
SELECT
  substr(salary, 1, 1),
  count(*)
FROM employee
GROUP BY substr(salary, 1, 1);

SELECT
  substr(salary, 1, 1),
  count(*)
FROM employee
GROUP BY substr(salary, 1, 1)
HAVING count(*) > 1;

10、多表查詢

## 內鏈接
SELECT
  a.name,
  b.name,
  a.salary
FROM employee a
  JOIN employee b ON a.salary = b.salary AND a.name != b.name;

SELECT
  a.name,
  b.name,
  a.salary
FROM employee a, employee b
WHERE a.salary = b.salary
      AND a.name != b.name;

## 外連接
### 左外聯結 left join on
### 右外聯結 right join on
### 全外聯結 full join on
select * from employee;
select * from t_dept_2 ;

select * from t_dept_2 a LEFT JOIN employee b on a.dept_no = b.dept_no;
select * from t_dept_2 a RIGHT JOIN employee b on a.dept_no = b.dept_no;
#####  mysql 不支持。。。 select * from t_dept_2 a FULL JOIN  

## 合併
#### union 合併重複記錄
SELECT DEPT_NO FROM T_DEPT_2
UNION
SELECT NO FROM EMPLOYEE;

SELECT DEPT_NO FROM T_DEPT_2
UNION ALL
SELECT NO FROM EMPLOYEE;

## 子查詢

SELECT *
FROM employee
WHERE (no, dept_no) IN (
  SELECT
    no,
    dept_no
  FROM employee
  WHERE salary > 4000);
  
##any
## 相似於 in
select * from employee WHERE salary = ANY (select salary from employee WHERE dept_no = 2);
## 大於最小值
select * from employee WHERE salary > ANY (select salary from employee WHERE dept_no = 2);
## 小於最大值
select * from employee WHERE salary < ANY (select salary from employee WHERE dept_no = 2);

##all
## 大於最大值
select * from employee WHERE salary > ALL (select salary from employee WHERE dept_no = 2);
## 小於最小值
select * from employee WHERE salary < ALL (select salary from employee WHERE dept_no = 2);


## exists
SELECT *
FROM employee a
WHERE exists(
    SELECT *
    FROM t_dept_2 b
    WHERE a.dept_no = b.dept_no
);

##  not exists
SELECT *
FROM employee a
WHERE NOT exists(
    SELECT *
    FROM t_dept_2 b
    WHERE a.dept_no = b.dept_no
);

11、運算符

## 算數運算符
SELECT
  6 + 4,
  6 - 4,
  6 / 2,
  6 DIV 2,
  6 % 4,
  6 MOD 4;
  
 ## 結果: 10,2,3.0000,3,2,2
 ## Why?
 ###  The MySQL DIV function is used for integer division where n is divided by m and an integer value is returned.
相關文章
相關標籤/搜索