首页 > 开发 > 综合 > 正文

用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;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表