SQL語句建立資料庫及操作

2021-09-30 08:36:29 字數 2209 閱讀 4144

新建資料庫: create database database_name [ on [ primary ] [

[ ,...n ] [ ,

[ ,...n ] ] ] [ [ log on ] ] [ collate collation_name ] [ for ] [ with

] ] [;]

::= ] [ , filegrowth = growth_increment [ kb | mb | gb | tb | % ] ] ) [ ,...n ] }

::=

::= | trustworthy }

::= 新建表: 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/') /'f:/*.jpg'/日期 '2010.07.10' 刪除資料: 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 server 中的日誌,減小資料庫檔案大小 dump transaction 資料庫名 with no_log backup log 資料庫名 with no_log dbcc shrinkdatabase(資料庫名) exec sp_dboption /'資料庫名/', /'autoshrink/', /'true/' ///'新增字段通用函式 sub addcolumn(tablename,columnname,columntype) conn.execute(/"alter table /"&tablename&/" add /"&columnname&/" /"&columntype&/"/") end sub ///'更改字段通用函式 sub modcolumn(tablename,columnname,columntype) conn.execute(/"alter table /"&tablename&/" alter column /"&columnname&/" /"&columntype&/"/") end sub ///'檢查表是否存在 sql=/"select count(*) as dida from sysobjects where id = object_id(n/'[所有者].[表名]/') and objectproperty(id, n/'isusertable/') = 1/" set rs=conn.execute(sql) response.write rs(/"dida/")/'返回乙個數值,0代表沒有,1代表存在 判斷表的存在: select * from sysobjects where id = object_id(n/'[dbo].[tablename]/') and objectproperty(id, n/'isusertable/') = 1 某個表的結構 select * from syscolumns where id = object_id(n/'[dbo].[你的表名]/') and objectproperty(id, n/'isusertable/') = 1

SQL語句建立資料庫

1.create database dataname 這是建立資料庫最簡單的方法 資料庫的各個屬性都是預設 如資料庫檔案與日誌檔案儲存目錄 資料庫大小等 下面介紹下常用決定資料庫屬性的子句 on 簡單理解為定義儲存資料庫檔案的位置,看下面 filename 資料庫的邏輯別名 size 資料庫初始大小...

sql語句建立資料庫

1 建立資料庫 系統資料庫master中儲存了所有本地資料庫的名字 use master goif exists select from sysdatabases where name student drop database student 如果資料庫存在就刪除 gocreate databas...

sql語句建立資料庫

1 建立資料庫 系統資料庫master中儲存了所有本地資料庫的名字 use master goif exists select from sysdatabases where name student drop database student 如果資料庫存在就刪除 gocreate databas...