可以使用asp在程式当中直接建立修改删除表
与表有关的sql语法如下:
建立一个表:create table表达式。
修改一个表:alter table表达式。
删除一个表:drop table表达式。
建立一个索引:使用create index 或 alter table表达式。
删除一个索引:drop index表达式。
详细介绍如下:
create table表达式
create table表达式,建立一个新的表。
语法如下:
create table 新表(新字段名称1 字段类型 [(长度)] [not null] [索引1] [,新字段名称2 字段类型 [(长度)] [not null] [索引2] [, …]] [, constraint multifieldindex [, …]])
长度为字符类型及字符长度。
索引1, 索引2 constraint子句定义单一字段索引。
multifieldindex定义一多重字段索引。
使用create table表达式,如果将一字段指定为not null,则在此字段中新增的记录资料内容必须是有效的资料。
constraint子句可在一字段上建立不同的限制,并且也可以用来建立主索引。
您可以使用create index表达式,在现有的表上建立一个主索引或多个索引。
让我们看一个在asp程式码当中使用这个sql指令的例子。
譬如asp程式码rs61.asp如下,[create table 学员 (姓名 text(10), 年龄 int)] 先建立一个学员的表,包括10个位元长度text类型的姓名字段,和整数类型的年龄字段:
<%
set conn1 = server.createobject("adodb.connection")
conn1.open "dbq="& server.mappath("ntopsamp.mdb") &";driver={microsoft access driver (*.mdb)};driverid=25;fil=ms access;"
sql = "create table 学员 (姓名 text(10), 年龄 int)"
set a = conn1.execute(sql)
response.write "create table ok<p>"
conn1.close
%>
执行后,使用access打开ntopsamp.mdb文件时,可看到新建立一个学员的表。
alter table表达式
alter table表达式,修改已建立好的表。
语法如下:
alter table 表 {add {column 字段名称 字段类型[(长度)] [not null] [constraint 索引] |
constraint multifieldindex} |
drop {column 字段名称 i constraint 多重字段索引名称} }
使用alter table表达式,您可以利用多种不同方法,变更目前已存在的表:
使用add column新增字段到表。
使用drop column删除一字段。只需指定欲删除之字段名称即可。
使用add constraint新增多重字段索引。
使用drop constraint删除多重字段索引。只需指定紧接在constraint之后的索引名称即可。
使用add column新增字段到表时,必须指定字段名称、所属类型、以及类型和长度。
例如,下例增加一个2个字符长度,名为性别的字符类型字段到学员表中:
alter table 学员 add column 性别 text(2)
您也可以对此字段定义索引。
如果您对一字段指定 not null,则在此字段中所新增的记录资料必须是有效的资料。
您不能同时新增或删除多个字段或索引。
譬如asp程式码rs63.asp如下,[create table 学员1 (姓名 text(10), 年龄 int)] 先建立一个学员1的表,然后使用 [alter table 学员1 add column 性别 text(2)] 增加一个2个字符长度,名为性别的字符类型字段到学员1表中:
<%
set conn1 = server.createobject("adodb.connection")
conn1.open "dbq="& server.mappath("ntopsamp.mdb") &";driver={microsoft access driver (*.mdb)};driverid=25;fil=ms access;"
sql = "create table 学员1 (姓名 text(10), 年龄 int)"
set a = conn1.execute(sql)
response.write "create table ok<p>"
sql = "alter table 学员1 add column 性别 text(2)"
set a = conn1.execute(sql)
response.write "alter table ok<p>"
conn1.close
%>
执行后,使用access打开ntopsamp.mdb文件时,可看到表学员1新建立一个姓别的字段。
drop表达式
drop表达式从一个数据库中删除一个已存在的表,或从一个表中删除一个已存在的索引。
语法如下:
drop {table 表 | index 索引 on 表}
表必须先关闭,才能删除此表或由此表中的索引。
若要删除索引,除了使用drop index 索引 on 表,也可以使用alter table。
譬如asp程式码rs62.asp如下,首先使用 [create table 学员2 (姓名 text(10), 年龄 int)] 先建立一个学员2的表,包括10个字符长度text类型的姓名字段,和整数类型的年龄字段,然后使用 [drop table 学员2] 删除学员2的表:
<%
set conn1 = server.createobject("adodb.connection")
conn1.open "dbq="& server.mappath("ntopsamp.mdb") &";driver={microsoft access driver (*.mdb)};driverid=25;fil=ms access;"
sql = "create table 学员2 (姓名 text(10), 年龄 int)"
set a = conn1.execute(sql)
response.write "create table ok<p>"
sql = "drop table 学员2"
set a = conn1.execute(sql)
response.write "drop table ok<p>"
conn1.close
%>
转载http://asp123.on.net.cn
