1. 程式人生 > 資料庫 >Mysql 3 —— 建表

Mysql 3 —— 建表

建表:
其實建表的過程就是一個畫表頭的過程,就是一個宣告欄位的過程。

create table 表名(
列1宣告 列1引數,
列2宣告 列2引數,
.....
列n宣告 列n引數
)engine myisam/innodb/bdb charset utf8/gbk;

create table member(
id int unsigned auto_increment primary key,
username char(20) not null default '',
gender char(1) not null default '',
weight tinyint unsigned not null default 0,

birth date not null default '0000-00-00',
salary decimal(8,2) not null default 0.00,
lastlogin int unsigned not null default 0
) engine myisam charset utf8;

修改表的語法
一張表,建立完畢,有了N列
之後還有可能要增加貨刪除或修改列
Alter table 表名 add 列名稱 列引數 列宣告
alter table m1 add birth date not null default '0000-00-00';

alter table 表名 add 列名稱 列型別 列引數 after 某列 [把新列加在某列後]

alter table m1 add gender char(1) not null default '' after username;
alter table m1 add pid int not null default 0 first; #如果想建一個列,且在表的最前面,用first

刪除列
alter table 表名 drop 列名稱
alter table m1 drop pid;

修改列,
alter table 表名 modify 列名稱 新型別和新引數
到了某世紀,性別有男/女/雌雄同體/偽娘
這是我們想把char(1) 改位char(4)

修改列名稱及列型別
alter table 表名 change 舊列名 新列名 新型別 新引數

alter table m1 change id uid int unsigned;


知識點 列型別 
儲存同樣的資料,不同的列型別,所佔的空間和效率是不一樣的,這就是我們建表前要列型別的意義
所以 重點寫列型別的儲存範圍和所佔據的位元組關係。

Mysql 三大列型別
數值型 
整形 tinyint(佔據一個位元組,-128-127,0-255)、smallint(2)、mediumint(3)、int(4)、bigint(8)

tinyint 1個位元組,8個位
[][][][][][][][]
0000 0000 ----> 0
1111 1111 ---->2^8-1=255

新手知道,計算機為了表示一個數是負數,會把最高位(左側)的0/1,當成符號來看,如為0,則是正數,如為1,則是負數

0 0000000 ---->0
0 1111111 ---->127

1 0000000 ---->-0
1 1111111 ---->-127

二進位制補碼問題
如以上理解,+0和-0重複了,浪費了一種儲存的可能性,因此計算機中的負數,不是找著“後面的絕對值直接乘-1得到的“,二十用補碼規則換算的。
所以 負數 = 絕對值位-128
1 1111111 ---->-1
1 0000000 ---->-128

一般而言,設某型別 N位元組
N位元組,8N位
0 ----> 2^8N-1
-2^(8N-1) ----> +2^(8N-1)-1

int 系列的宣告時的引數 (m) unsigned zerofill
int 系列不加特殊說明時,預設是有符號的
加unsigned表示無符號,可以影響儲存的範圍
加一個學分列
alter table class add score tinyint unsigned not null default 0;

分析M引數
zerofill zero是零,fill是填充,代表0代表
M必須和zerofill配合才有意義
加一個學號列
alter table class add snum smallint(5) zerofill unsigned not null default 0;

小數(浮點型/定點型)
float(M,D),decimal(M,D)
M叫"精度"---->代表"總位數",而D是"標度",代表小位數(小數點右邊的位數)
加一個獎金列 alter table salary add bonus float(5,2) unsigned not null default 0.00;
float 能存10^38,10^-38 如果M<=24,佔4個位元組,否則佔8位元組
用來表示資料中的小數,除了float---浮點
還有一種叫定點,定點是把整數部分和小數部分分開儲存,比float精確

字串型
char,varchar,text,blob

char(6)定長字串
查詢行記錄時,如果都是定長,可以通過行數乘行的長度算出檔案指標的偏移量
對於定長N,不論夠不夠指定長度,實際都佔據N個長度,如果不夠N個長度,用空格在末尾補置N個長度
利用率i/M<=100%
00\0\0\0\0\0 (char型,如果不夠M個字元,內部用空格補齊,取出時再把右側空格刪掉)

varchar(100)也是儲存0-100個字元
對於varchar(N),不用空格補齊,但列內容前,有1-2個位元組來標誌該列的內容長度
i/(i+1-2)<100%

速度上定長快些

注意:char(M),varchar(M)限制的是字元,不是位元組
即 char(2) charset utf8, 能存兩個utf8字元,比如'中國'

text 文字型別,一般用來是儲存文章內容,新聞內容等
宣告text列時,不必給預設值。

create table test2(
article text
);

alter table test2 add img blob;

blob,是二進位制型別,用來儲存影象、音訊等二進位制資訊
意義:2進位制,0-255都有可能出現
blob在於防止因為字符集的問題導致資訊丟失

 

日期/時間型別
2020-10-24
date 型別 YYYY-MM-DD 1000-01-01到9999-12-31
time 型別 HH-MM-SS -838:59:59‘和’838:59:59‘
datetime型別,日期時間型別 YYYY-MM-DD HH:MM:SS

create table test3(
-> star varchar(20) not null default '',
-> birth date not null default '0000-00-00'
-> )engine myisam charset utf8;

mysql> insert into test3
-> values
-> ('張國榮','1961-03-12')
-> ;

alter table test3 add sign time not null default '00:00:00';

create table test4(sname varchar(20) not null default '',logintime datetime not null default '0000-00-00 00:00:00')engine myisam charset utf8;

insert into test4 values ('張三','2009-10-13 15:34:45');

date 3個位元組
datetime 8個位元組
time 3個位元組

一個比較有意思的列 timestamp
create table tests4(ts timestamp default CURRENT_TIMESTAMP,id int)engine myisam charset utf8;

year 型別 1個位元組,255個變化 1901-2155 還可以存0000年
mysc1>year型別,還可以簡寫成2位
mysa1>createtable test? (
ya year(2)
)engine myisam charset utf8;

mysq1> insert in test7 values ('95'),('12');