用SQL創建數據庫
2024-07-21 02:07:54
供稿:網友
用sql創建數據庫
首先說說怎么用sql語句創建數據庫,創建數據庫的語句有如下幾種:
1. create table(創建新表)
2. create index(增加索引)
3. drop index(刪除索引)
4. constraint(約束語句)
5. alter table(修改表)
6. drop table(刪除表)
create table語句:
在數據庫中生成新表,表中字段的類型可以為:integer(整型)、long(長整型)、 single(單精度浮點數)、double(雙精度浮點數)、datetime(日期型,也可以寫成date)、bit(布爾型)、 text(字符串型,最大255個字節)、memo(字符串型,最大可達1.2g字節)、 counter(自動遞增長整型,可確定記錄的唯一性)、currency(貨幣型,精確到小數點左邊15位,右邊4位)、 binary(字節型,最大255個)、longbinary(用于ole對象)、guid(全局唯一標識符)。
生成表newtable,該表有文本字段field1和整型字段field2,表名和字段名可以隨便你取,不區分大小寫,但是,有些保留字不能用作表名字段名,比如number create table newtable(field1 text(30), field2 integer); create index語句:
index是為了加快查找記錄的速度,或者是為了增加字段約束關系而設置的。
創建索引語句執行前表中可以有記錄,但存在的記錄必須滿足該索引語句的約束關系,否則語句不能執行,另外要注意的是在同一個數據庫中(而不僅僅是在同一個表中),索引名不能相同,否則語句也會失敗。
生成字段field1的索引字段newindex,兩條語句作用相同
生成后field1字段可以有相同的值,可以有空值(null) create index newindex on newtable (field1);
create index newindex on newtable (field1) with ignore null;
生成字段field1的索引字段newindex,注意,每個表里只能有一個主索引(primary)。生成后field1字段不能有相同的值,不能有空值(當然,如果是text類型,可以有一個空串,但是空串不是空值) create index newindex on newtable(field1) with primary;
字段field1不能有相同的值,但可以有空值(兩個空值不算相同的值) create unique index newindex on newtable(field1);
字段field1可以有相同的值,但不能有空值 create index newindex on newtable(field2) with disallow null
可以在索引語句中加入asc(升序)或desc(降序)來控制記錄排列順序如果不使用順序字,sql則默認使用asc順序 create index newindex on newtable(field1 asc, field2 desc); drop index語句:刪除表newtable中的索引newindex,語句執行前索引newindex必須存在 drop index newindex on newtable;
constraint語句:
constraint子句用于創建數據庫完整性的索引,它和index語句作用一樣,有些地方可以互相替代,它可以使用primary key(主關鍵字),unique(唯一)和foreign key(外部關鍵字),和index相比不能使用ignor null和disallow null,但多了foreign key(這也是它最強大的地方)。另外, constraint語句必須和create table或alter table語句一起使用。
生成表newtable,主關鍵字段是field1,主索引是newpk create table newtable(field1 long constraint newpk primary key, field2 memo, field3 datetime);
生成索引為newuk的表newtable,field1不能有相同值,可以有空值 create table newtable(field1 integer constraint newuk unique);
生成多列的主索引,兩條記錄的field1和field2不能全部相同,也不能為空值 create table newtable(field1 integer, field2 currency, constraint newpk primary key(field1, field2));
生成多列的unique索引,兩條記錄的field1和field2不能全部相同注意,如果兩條記錄其中一個字段相同而另一個字段都是空值,那也算兩個字段不同 create table newtable(field1 integer, field2 currency, constraint newuk unique(field1, field2));
要在幾個不同的表之間建立聯系,就要使用foreign key references子句,它可以限定某個表的字段內容必須存在于另外一個表中。
第一個例子:
首先,生成主關鍵字段為field1的表newtable1 create table newtable1(field1 integer constraint newpk primary key);
然后,再生成外部索引,兩個表的field1必須類型相同,并且第一個表的field1是主關鍵字段或unique字段。生成外部索引后,表newtable2要增加記錄,它的field1字段值必須已經存在于表newtable1的field1字段中。
下面兩條語句作用相同,因為field1是newtable1的主關鍵字段,可以省略不寫 create table newtable2(field1 integer constraint newfk references newtable1);
create table newtable2(field1 integer constraint newfk references newtable1(field1));
第二個例子:
首先,生成主關鍵字段為field1和field2的表newtable1 create table newtable1(field1 integer, field2 text(20), constraint newpk primary key(field1, field2));
然后,生成多列外部索引 create table newtable2(field1 integer, field2 text(20), constraint newfk foreign key(field1, field2) references newtable1(field1, field2)); alter table語句:
在表生成之后,如果想修改表的結構,就使用這條語句,它能增加或刪除字段以及約束關系。
給表newtable增加日期型字段field3,語句執行前表newtalbe必須沒有字段field3 alter table newtable add column field3 date;
刪除表newtable中的字段field3,語句執行前字段field3必須存在表newtable中 alter table newtable drop column field3;
給表newtable增加newuk約束關系 alter table newtable add constraint newuk unique(field1,field2);
刪除表newtable的newuk約束關系 alter table newtable drop constraint newuk; drop table語句:刪除表newtable,語句執行前表newtable必須存在 drop table newtable;