常用的SQL建表語句

2021-05-23 08:19:41 字數 1893 閱讀 1503

(1) 更改錶子字段的長度:alter table table_name modify(col_name varchar2(6));

(2) 更改表字段的名字:alter table table_name rename column col_name1 to col_name2 ;

(3) 刪除表字段: alter table table_name drop column col_name;

(4) 新增表字段: alter table table_name add col_name varchar2(20) null ;

新建表:

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 建表語句模板常用

funnel name varchar 100 default null comment 漏斗名 events string varchar 200 default null comment 事件字串 create time varchar 30 default null comment 建立時間 ...

SQL建表語句 約束

create table stu id number 6 name varchar2 20 constrait stu name nn not null,constraint約束 字段約束 number 1 age number 3 sdate date,grade number default 1...

sql建表語句轉成mysql sql語句建立成登入

sql語句建立成登入 1 windows登入賬戶口 exec ap grantlogin windows網域名稱 域賬戶 2 sql 登入賬戶 exec sp addlogin 賬戶名 密碼 3 建立資料庫使用者 exec spgrantdbaccess 登入賬戶 資料庫使用者 二 給資料庫使用者授...