MySQL數據庫基礎筆記

數據庫

數據庫就是存儲和管理數據的倉庫,用戶能夠對數據庫中的數據進行增刪改查等操做。mysql

數據庫的分類

  • 關係型數據庫(Oracle、MySQL、SQLite等)sql

  • 非關係型數據庫(Redis、MongoDB等)數據庫

MySQL簡介

MySQL是一個關係型數據庫,由MySQLAB公司開發,目前屬於Oracle旗下。編程

特色:開源、支持大型數據庫、使用標準SQL、適用於多種操做系統以及提供多種編程語言接口。數據庫設計

安裝(Ubuntu中):編程語言

sudo apt-get install mysql-server
sudo apt-get install mysql-client

MySQL客戶端鏈接服務端:函數

mysql -uusername -ppassword

退出:exit、quit性能

經常使用數據類型

int、bit、decimal、varchar、char、date、time、datetime、enum、textui

經常使用數據約束

主鍵primary key、非空not null、惟一unique、默認default、外鍵foreign keyspa

數據庫設計三範式

  1. 原子性

  2. 知足1,表必須有主鍵,非主鍵字段必須徹底依賴於主鍵

  3. 知足2,非主鍵必須直接依賴於主鍵

E-R模型

實體-關係模型,E-R模型就是描述數據庫存儲數據的結構模型

  • 一對一

  • 一對多

  • 多對多

經常使用SQL語句

  1. 基本語句

    • 查看全部數據庫 show databases;

    • 建立數據庫 use database_name;

    • 使用數據庫 use database_name;

    • 查看當前使用的數據庫 selecet database();

    • 刪除數據庫 drop database database_name;

    • 查看當前庫中全部表 show tables;

    • 建表 create table table_name(id int unsigned primary key auto_increment, name varchar(30) not null, age tinyint unsigned not null);

    • 修改表

      • 添加字段 alter table table_name add sexy bit not null;

      • 修改字段類型 alter table table_name modify sexy tinyint unsigned not null;

      • 修改字段名 alter table table_name change sexy gender tinyint unsigned not null;

      • 刪除字段 alter table table_name drop gender;

      • 設置外鍵 alter table table_name add foreign key(cls_id) references classes(id);

      • 刪除外鍵 alter table table_name drop foreign key cls_id;

    • 查看建表(庫)SQL語句 show create table(database) table_name(database_name);

    • 刪除表 drop table table_name

  2. 表數據的基本增刪改查:

      • insert into table_name values();

      • insert into table_name(name) values('a');

      • insert into table_name values(),(),();

    • 刪:delete from table_name where id=33;

      • 邏輯刪除,設置一個bit類型字段表示是否刪除

    • 改: update table_name set name='c', age=18 where id=222;

      • select * from table_name;

      • select name,age from table_name;

  3. as關鍵字(起別名)

    • select t.name,t.age from table_name as t;

  4. distinct關鍵字(去重)

    • select distinct name from table_name;

  5. where條件查詢

    • select * from table_name where id<10 and age>18;

  6. 模糊查詢

    • select * from table_name where name like '郭%' or name like '郭威_';

  7. 範圍查詢

    • select * from table_name where id between 5 and 10 or id in (3, 19);

  8. 空判斷查詢

    • select * from table_name where gender is null;

    • ifnull(age,18)判斷字段是否爲空,爲空則使用提供值

  9. 排序查詢

    • select * from table_name order by age desc;

      desc表示倒序排序,ase升序排序,默認升序

  10. 分頁查詢

    • select * from table_name limit start,count;

  11. 聚合函數查詢

    count()、max()、min()、sun()、avg()、round(avg(age),保留小數位)

    • select avg(age) from table_name where gender=0;

  12. 分組查詢

    • select gender,avg(gender) from table_name group by gender with rollup;

      with rollup 在最後新增一行查詢的記錄

    • select gender,count(*) from table_name group by gender having count(*)>5;

      having 過濾分組數據,只用於group by

  13. 鏈接查詢

    • 內鏈接

      • select name from table1 inner join table2 on table1.age = table2.age;

    • 左鏈接

      • left join

    • 右鏈接

      • right join

    • 自鏈接

      • select a.id,a.name from table as a inner join table as b where a.pid=b.id;

  14. 子查詢

    • select * from table where age > (select avg(age) from table);

      性能較差

  15. 將查詢結果插入其餘表

    • insert into country(name) select hero_country from hero group by hero_country;

  16. 鏈接更新

    • update hero h inner join country c on h.country=c.name set h.country=c.id;

  17. 建表同時添加數據

    • create table country(id int unsigned primary key auto_increment,name varchar(30) not null)select hero_country from hero group by hero_country;

事務

事務就是一系列sql操做做爲一個單元執行,要麼所有執行,要麼所有不執行。

  1. 事務的四大特性

    • 原子性

    • 一致性

    • 隔離性

    • 持久性

  2. MySQL中使用事務需選擇InnoDB存儲引擎,其餘引擎不支持事務

  3. 開啓事務

    • begin;

    • start transaction;

    • set autocommit=0;

      MySQL默認自動提交,該設置關閉默認提交功能

  4. 提交事務

    • commit;

  5. 回滾事務

    • rollback;

索引

  • 建立索引 alter table table_name add index name_index(name);

  • 聯合索引 alter table table_name add index name_index(name,age);

  • 刪除索引 alter table table_name drop index name_index;

引擎

相關文章
相關標籤/搜索