SQl 語句 常見 新建,刪除,修改表結構

2021-07-11 06:47:06 字數 1849 閱讀 7912

2006-6-15 15:58:25

新建表:

create table [表名] (

[自動編號字段] int identity (1,1) primary key ,

[欄位1] nvarchar(50) default '預設值' null ,

[欄位2] ntext null ,

[欄位3] datetime,

[欄位4] money null ,

[欄位5] int default 0,

[欄位6] decimal (12,4) default 0,

[欄位7] image null , )

刪除表:

drop table [表名]

插入資料:

insert into [表名] (欄位1,欄位2) values (100,'51windows.net')

刪除資料:

delete from [表名] where [欄位名]>100

更新資料:

update [表名] set [欄位1] = 200,[欄位2] = '51windows.net' where [欄位三] = 'haiwa'

新增字段:

alter table [表名] add [欄位名] nvarchar (50) null

刪除字段:

alter table [表名] drop column [欄位名]

修改字段:

alter table [表名] alter column [欄位名] nvarchar (50) null

重新命名表:(access 重新命名表,請參考文章:在access資料庫中重新命名表)

sp_rename '表名', '新錶名', 'object'

新建約束:

alter table [表名] add constraint 約束名 check ([約束字段] <= '2000-1-1')

刪除約束:

alter table [表名] drop constraint 約束名

新建預設值

alter table [表名] add constraint 預設值名 default '51windows.net' for [欄位名]

刪除預設值

alter table [表名] drop constraint 預設值名

另外以上的只是sql的語法在 access 下大部份也都是一樣的

項一項查閱,並經自己使用驗證,確認在access 資料庫中新增自動編號字段使用以下方法比較合適: create table 資料表名稱 (id counter constraint primarykey primary key) 需要注意的地方是:第二個primary中間有空格,另外,關鍵字不區分大小寫. 另外自己最近發現的一種方法是: sql="create table mytb (id autoincrement(25,1) primary key,age int)" sql2="create table testtb (id autoincrement,age int,email char, primary key (id))" 其中在access中,autoincrement為自動編號型別字段,(25,1)分別為初始值及步長值,如果不寫的話,預設是1,1,primary key指定了主鍵,以上示例,兩種指定方法都可以

SQl 語句 常見 新建,刪除,修改表結構

新建表 create table 表名 自動編號字段 int identity 1,1 primary key 欄位1 nvarchar 50 default 預設值 null 欄位2 ntext null 欄位3 datetime,欄位4 money null 欄位5 int default 0,...

SQl 語句 常見 新建,刪除,修改表結構

新建表 create table 表名 自動編號字段 int identity 1,1 primary key 欄位1 nvarchar 50 default 預設值 null 欄位2 ntext null 欄位3 datetime,欄位4 money null 欄位5 int default 0,...

SQl 語句 常見 新建,刪除,修改表結構

新建表 create table 表名 自動編號字段 int identity 1,1 primary key 欄位1 nvarchar 50 default 預設值 null 欄位2 ntext null 欄位3 datetime,欄位4 money null 欄位5 int default 0,...