mysql建表建索引6 mysql建表建索引

2021-10-20 23:10:03 字數 2296 閱讀 3809

建表:

create table `sj_projects` (

`id` int(11) not null auto_increment,

`title` varchar(255) not null default 『『 comment 『專案名稱『,

`platform_id` int(11) not null default 『0『 comment 『平台id『,

`unique_id` varchar(255) not null default 『『 comment 『專案和資料的唯一id『,

`repayway` varchar(255) not null default 『『 comment 『還款方式『,

`profit` decimal(5,2) not null default 『0.00『 comment 『年化收益『,

`speed` decimal(5,2) not null default 『0.00『 comment 『進度『,

`frequency` int(11) not null default 『0『 comment 『投資人次『,

`amount` decimal(20,2) not null default 『0.00『 comment 『融資金額『,

`res_amount` decimal(20,2) not null default 『0.00『 comment 『剩餘可投金額『,

`invtime` timestamp not null default 『0000-00-00 00:00:00『 comment 『專案投資起始時間『,

`endtime` timestamp not null default 『0000-00-00 00:00:00『 comment 『專案投資結束時間『,

`turntime` timestamp not null default 『0000-00-00 00:00:00『 comment 『專案還款時間(客戶收款時間)『,

`term` varchar(50) not null default 『『 comment 『期限『,

`type` int(11) not null default 『0『,

`pageurl` text comment 『注意大小寫pageurl『,

`insert_time` timestamp not null default current_timestamp comment 『新增時間『,

`orderby_id` int(11) not null default 『1『 comment 『排序『,

`status` tinyint(1) not null default 『1『 comment 『狀態 0為關閉,1為開啟『,

primary key (`id`),

key `unique_id` (`unique_id`) using btree

) engine=innodb auto_increment=2383 default charset=utf8 row_format=dynamic comment=『專案表『;

如果為唯一索引: unique key `unique_id` (`unique_id`) using btree

也可以直接這樣:(唯一索引)

unique_id varchar(255) unique not null default 『『 comment 『專案和資料的唯一id『,

查詢:show index from sj_projects;  -- 查詢sj_projects的所有索引

show create table sj_projects\g

新增索引:

alter table `sj_projects` add unique `unique_id` (`unique_id`);  -- 唯一索引

alter table `sj_projects` add index `unique_id` (`unique_id`);    -- 普通索引

alter table `sj_projects` add index `uniqueid` (`unique_id`);    -- 普通索引可以修改名字

刪除索引:

alter table `sj_projects` drop index `unique_id`;

刪除自增id索引:

需要先將id鍵的自動增長取消:

alter table `sj_projects`  modify `id`  int(10) not null comment 『id『;

再次執行: alter table `sj_projects` drop primary key;

原文:

mysql建表建索引6 Mysql建表 建立索引

建立表時可以直接建立索引,這種方式最簡單 方便。其基本形式如下 create table 表名 屬性名 資料型別 完整性約束條件 屬性名 資料型別 完整性約束條件 屬性名 資料型別 unique fulltext spatial index key 別名 屬性名1 長度 asc desc uniqu...

mysql建表索引語句 Mysql建表 建立索引

建立表時可以直接建立索引,這種方式最簡單 方便。其基本形式如下 create table 表名 屬性名 資料型別 完整性約束條件 屬性名 資料型別 完整性約束條件 屬性名 資料型別 unique fulltext spatial index key 別名 屬性名1 長度 asc desc uniqu...

MySql建表與索引

常用命令 show databases 檢視資料庫 create database test 建立資料庫 usetest 選擇資料庫 s 檢視資料庫狀態 create table 建立表的幫助 show tables 檢視表 desc test 檢視表結構 c 退出建立表 一 建立表的基本模型 為可...