1. 程式人生 > 實用技巧 >mysql資料庫開發重點知識總結

mysql資料庫開發重點知識總結

一、儲存引擎

1.什麼是儲存引擎?

庫就是資料夾,表就是檔案,檔案都會有檔案的格式,儲存引擎就是檔案格式.

儲存引擎就是檔案的型別(格式),文字檔案有TXT型別、圖片有JPG型別、視訊有MP4格式.

mysql中最常用的是InnoDB儲存引擎(格式)

2.使用儲存引擎

1 # 建立表是指定儲存引擎
2 create table t1(
3     id int,
4     name char
5 )engine=innodb;
6 
7 # 查看錶達儲存引擎
8 show create table t1;

二、資料型別

1.數字

1.1整型:tinyinit, int, bigint

儲存年齡,等級,ID,各種號碼等

1.2小數(浮點型):float, double, decimal

儲存薪資,身高,體重,體質引數等

2.字串

2.1char(10)

簡單粗暴,浪費空間,存取速度快

2.2varchare

精準,節省空間,存取速度慢

2.3sql對字串的優化

建立表時,定長(比如性別)的型別往前放,變長(比如地址)的往後放

超過255個字元,就把檔案路徑存放到資料庫中,比如圖片,視訊等找一個檔案伺服器,資料庫中只存路徑或URL.

3.時間型別:datetime

date, time, datetime, temestamp, year

now()

4.列舉型別與集合型別

enum單選

set多選

三、庫

 1
# 1. 新增庫 2 create database db10 charset utf8; 3 4 # 2. 刪除庫 5 drop database db10; 6 7 # 3. 修改資料庫 8 alter database db10 charset utf8; 9 10 # 4. 檢視資料庫 11 show databases; 12 show create database db10; 13 select database(); # 檢視當前已經選擇的資料庫 14 15 # 5. 選擇資料庫 16 use db10;

四、表

1.增刪改查

 1 # 1. 建立表
 2
create table t1( 3 id int, 4 name varchar(50), 5 sex enum('male', 'female'), 6 age int(3) 7 ); 8 9 # 2. 刪除表 10 drop table t1; 11 12 # 3. 修改表 13 # 3.1 修改儲存引擎 14 alter table t1 15 engine = innodb; 16 # 3.2 新增欄位 17 alter table t1 18 add name varchar(20) not null, 19 add age int(3) not null default 22; 20 alter table student10 21 add stu_num varchar(10) not null after name; //新增name欄位之後 22 alter table student10 23 add sex enum('male', 'female') default 'male' first; //新增到最前面 24 # 3.3 刪除欄位 25 alter table student10 26 drop sex; 27 # 3.4 修改欄位型別 28 alter table student10 29 modify age int(3); 30 alter table student10 31 modify id int(11) not null primary key auto_increment; //修改為主鍵 32 # 3.5 增加約束(針對已有的主鍵增加auto_increment) 33 alter table student10 34 modify id int(11) not null primary key auto_increment; 35 alter table student10 36 modify id int(11) not null auto_increment; 37 # 3.6 對已經存在的表增加複合主鍵 38 alter table service2 39 add primary key (host_ip, port); 40 # 3.7 增加主鍵 41 alter table student1 42 modify name varchar(10) not null primary key ; 43 # 3.8 增加主鍵和自動增長 44 alter table student1 45 modify id int not null primary key auto_increment; 46 # 3.9 刪除主鍵 47 alter table student10 48 modify id int(10) not null ; //刪除自增約束 49 alter table student10 50 drop primary key ; //刪除主鍵 51 52 # 4. 查看錶結構 53 describe t1; 54 desc t1; //簡寫形式 55 show create table t1\G; //查看錶詳細結構,可加\G 56 57 # 5. 複製表 58 create table new_service 59 select * from service; //複製表結構+記錄(key不會複製:主鍵,外來鍵,和索引) 60 create table new_service 61 select * from service where 1=2; //只複製表結構,因為條件為假,查不到任何記錄.

2.完整性約束條件

約束性條件和資料型別的寬度一樣,都是可選引數

作用:用於保證資料的完整性和一致性

主要分為:

  primary key (pk) 標識該欄位為該表的主鍵,可以唯一的標識記錄

  foreign key (fk) 標識該欄位為該表的外來鍵

  not null 標識該欄位不能為空

  unique key (uk) 標識該欄位的值是唯一的

  auto_increment 標識該欄位的值自動增長(資料型別,而且為主鍵)

  default 為該欄位設定預設值

  unsigned 無符號

  zerofill 使用0填充

說明:

  1.是否允許為空,預設null,可設定為not null,欄位不允許為空,必須賦值

  2.欄位是否有預設值,預設的預設值是null,如果插入記錄時不給欄位賦值,此欄位使用預設值

    sex enmu('male', 'female') not null default 'male'

    age int usigned not null default 20 必須為正值(無符號),不允許為空,預設20

  3.是否是KEY

    主鍵 primary key

    外來鍵 foreign key

    索引 (index, unique...)

3.完整性約束條件操作例項

3.1unique設定唯一約束

 1 # 方法一:
 2 create table t2(
 3     id int,
 4     name varchar(20) unique,
 5     comment varchar(100)
 6 );
 7 # 方法二:
 8 create table t3(
 9     id int,
10     name varchar(20),
11     comment varchar(100),
12     constraint uk_name unique (name)
13 );
1 # 聯合唯一
2 create table service(
3     id int primary key auto_increment,
4     name varchar(20),
5     host varchar(15) not null,
6     port int not null ,
7     unique (host, port)
8 );

3.2primary key欄位的值不為空且唯一

一個表中可以:

  單列做主鍵,not null + unique 或 primary key

  多列做主鍵(符合主鍵),

但一個表內只能有一個主鍵primary key

1 # 多列主鍵
2 create table t4(
3     ip varchar(15),
4     port char(5),
5     service_name varchar(10) not null,
6     primary key (ip, port)
7 );

3.3auto_increment自增長欄位

1 create table student(
2     id int primary key auto_increment,
3     name varchar(20),
4     sex enum('male', 'female') default 'male'
5 );

3.4foreign key關聯其它表

先建父表,再建關聯子表,刪除父表記錄,子表中對應的記錄跟著刪.

表之間的關係:一對一,多對一,多對多

五、記錄

1.增

2.刪

3.改

4.查