關於建立資料表的問題

2021-09-02 07:14:32 字數 1489 閱讀 1237

比如這條建立表的sql

create table `db_goods` (

`id` bigint unsigned not null auto_increment comment '主鍵',

`goods_id` int(11) unsigned not null comment '商品id',

`goods_title` varchar(225) not null default '' comment '商品名稱',

`goods_price` decimal(10,2) not null comment '商品**',

`create_time` timestamp not null default current_timestamp comment '建立時間',

`update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新時間',

primary key (`id`)

) engine=innodb default charset=utf8mb4 collate=utf8mb4_unicode_ci comment='商品表';

今天大佬突然說讓我們把要上線的sql改一下,我一直不明白為啥,首先把主鍵id變成bigint,並且為無符號的,今天從網上查了一下,發現了中間的不同。

bigint

從 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型資料(所有數字)。儲存大小為 8 個位元組。 

p.s. bigint已經有長度了,在mysql建表中的length,只是用於顯示的位數 

int

從 -2^31 (-2,147,483,648) 到 2^31 – 1 (2,147,483,647) 的整型資料(所有數字)。儲存大小為 4 個位元組。int 的 sql-92 同義字為 integer。 

smallint

從 -2^15 (-32,768) 到 2^15 – 1 (32,767) 的整型資料。儲存大小為 2 個位元組。 

tinyint

從 0 到 255 的整型資料。儲存大小為 1 位元組。 

基本小的型別什麼的,如果只有是或者否的型別,那麼就可以用tinyint,儲存小。

如果資料表不是很常用,那麼我們就可以用int,如果是使用者表或者日誌表,就可以用無符號的bigint

int型別在資料庫中佔4個位元組(byte),即32位

int有符號型別 取值在 - 2147483648 ~ 2147483647之間

int無符號型別 取值在 0 ~ 4294967295之間

建立資料表注意問題

在很多情況下,我對知識都是掌握的模稜兩可,一到用到的時候,自己就不能肯定的說自己作對了。我總結到,歸於知識細節掌握的不紮實。正如今天軟考關於資料表建立的一些小問題。在建立表的 那肯定是錯誤的。經此我自己感覺到學習知識要深入到細節,認真思考和踏踏實實做事。建立表 use 資料庫名 create tab...

建立資料表

語法 create table 表名 欄位名稱 字段型別 字段特徵 是否為null,預設值 標識列 主鍵 唯一鍵 外來鍵 check約束 欄位名稱 字段型別 字段特徵 是否為null,預設值 標識列 主鍵 唯一鍵 外來鍵 check約束 建立老師表teacher id name gender age...

建立資料表

class users models.model uphone models.charfield max length 20,verbose name 號碼 upwd models.charfield max length 20,verbose name 密碼 uemail models.email...