1.新建一資料表,裡面有欄位id,將id設為為主鍵
**如下:
create table tb(id int,constraint pkid primary key (id)) create table tb(id int primary key )
2.新建一資料表,裡面有欄位id,將id設為主鍵且自動編號
**如下:
create table tb(id int identity(1,1),constraint pkid primary key (id)) create table tb(id int identity(1,1) primary key )
3.已經建好一資料表,裡面有欄位id,將id設為主鍵
**如下:
alter table tb alter column id int not null
alter table tb add constraint pkid primary key (id)
4.刪除主鍵
**如下:
declare @pk varchar(100); select @pk=name from sysobjects where parent_obj=object_id('tb') and xtype='pk'; if @pk is not null exec('alter table tb drop '+ @pk)
mysql 主鍵自增語句 MySQL 自增主鍵
以下僅考慮 innodb 儲存引擎。自增主鍵有兩個性質需要考慮 單調性每次插入一條資料,其 id 都是比上一條插入的資料的 id 大,就算上一條資料被刪除。連續性插入成功時,其資料的 id 和前一次插入成功時資料的 id 相鄰。自增主鍵的單調性 為何會有單調性的問題?這主要跟自增主鍵最大值的獲取方式...
MySQL設定主鍵自增和非主鍵自增
mysql 每張表只能有1個自動增長字段,這個自動增長字段即可作為主鍵,也可以用作非主鍵使用,但是請注意將自動增長字段當做非主鍵使用時必須必須為其新增唯一索引,否則系統將會報錯。例如 將自動增長字段設定為主鍵 create table t1 id int auto increment primary...
Oracle主鍵自增
1.建立資料表 create table test increase userid number 10 primary key,主鍵,自動增加 username varchar2 20 2.建立自動增長序列 create sequence testincrease sequence incremen...