wordpress代碼上傳到服務(wù)器深圳優(yōu)化seo排名
創(chuàng)建
create table 表名(字段1 字段類型 [約束] [comment 字段1注釋],...) [comment 表注釋];
約束是作用于表中字段上的規(guī)則,用于限制存儲在表中的數(shù)據(jù)。它的目的是保證數(shù)據(jù)庫中數(shù)據(jù)的正確性、有效性和完整性。
約束 | 描述 | 關(guān)鍵字 |
---|---|---|
非空約束 | 限制該字段不能為null | not null |
唯一約束 | 保證字段的所有數(shù)據(jù)都是唯一、不重復(fù)的 | unique |
主鍵約束 | 主鍵是一行數(shù)據(jù)的唯一標(biāo)識,要求非空且唯一 | primary key |
默認約束 | 保存數(shù)據(jù)時,如果未指定該字段,則采用默認值 | default |
外鍵約束 | 讓兩張表的數(shù)據(jù)建立連接,保證數(shù)據(jù)的一致性和完整性 | foreign key |
create table tb_user (id int primary key auto_increment comment 'ID,唯一標(biāo)識',username varchar(20) not null unique comment '用戶名',name varchar(10) not null comment '姓名',age int comment '年齡',gender char(1) default '男' comment '性別') comment '用戶表';
MySql 數(shù)據(jù)類型
查詢
-
查詢當(dāng)前數(shù)據(jù)庫所有表:
show tables;
-
查詢表結(jié)構(gòu):
desc 表名;
-
查詢建表語句:
show create table 表名;
修改
-
添加字段:
alter table 表名 add 字段名 類型(長度) [comment 注釋] [約束];
-
修改字段類型:
alter table 表名 modify 字段名 新數(shù)據(jù)類型(長度);
-
修改字段名和字段類型:
alter table 表名 change 舊字段名 新字段名 類型(長度) [comment 注釋] [約束];
-
刪除字段:
alter table 表名 drop column 字段名;
-
修改表名:
rename table 表名 to 新表名;