-
ASP + Access 初阶笔记 By Shawl.qiu
说明:
这是 shawl.qiu 的个人笔记, 一点一滴都是敲出来, 一大堆, 杂乱无序, 不过初级 Access 的操作内容应该都涉及到了.
所以, 转载什么的请注明一下来源, 多次见着鄙人写的一些不成文的文章被转到这呀那呀, 某些人对作者很不尊重, 连注明出处都没.
这些内容只适合初阶水平的人阅读, 大侠类人物请无视.
shawl.qiu
http://blog.csdn.net/btbtd
- linenum
- CREATED BY STABX, AT 2006-4-12.
- ASP and Access Database Notes
- ---/---------------------------------------------------------
- general operate
- 11. 获得新插入数据的 ID
- 11.1 获取单个 ID
- ------------------------------------------------------------------
- sql = "INSERT INTO ctarticle (classid,nclassid,title,content,dateandtime,writer,email,www,url,reoff,htmloff,ubboff ) VALUES ("&cid_&","&ncid_&",'"&trim(tt_)&"','"&ct_&"','"&adt_&"','"&trim(au_)&"','"&trim(em_)&"','"&trim(www_)&"','"&trim(url_)&"',"&ro_&","&ho_&","&uo_&") " '插入数据操作
- rsData.Execute(sql)
- set rs1=rsData.Execute("select max(articleid) from ctarticle")
- response.write rs1(0)
- rs1.close : set rs1=nothing
- ------------------------------------------------------------------
- 11.2 获取多个 ID
- ------------------------------------------------------------------
- sql = "INSERT INTO ctarticle (classid,nclassid,title,content,dateandtime,writer,email,www,url,reoff,htmloff,ubboff ) VALUES ("&cid_&","&ncid_&",'"&trim(tt_)&"','"&ct_&"','"&adt_&"','"&trim(au_)&"','"&trim(em_)&"','"&trim(www_)&"','"&trim(url_)&"',"&ro_&","&ho_&","&uo_&") " '插入数据操作
- rsData.Execute(sql)
- set rs1=rsData.Execute("select top 1 * from ctarticle order by articleid desc")
- response.write rs1("articleid")
- response.write " "
- response.write rs1("classid")
- response.write " "
- response.write rs1("nclassid")
- rs1.close : set rs1=nothing
- ------------------------------------------------------------------
- 10. 日期
- 9. 列出不是 汉字 和 字母 和 号码 的数据
- 8. 为不同数据库表中的相同列赋值, 并进行条件查询
- 7. 统计记录数
- 6. 以两个列进行排列
- 5. 查询一个范围内的数据
- 4. 查询多个数据表
- 3. 计数器加 1
- 2. 用于在某页面显示某分类的查询法, 并用 ORDER BY 排序
- 1. DW 链接 ACCESS 数据库
- //----------------------
- 10.1 查找下一年中的第一条记录
- sql="SELECT top 1 dateandtime FROM ctarticle where year(dateandtime)=(year(#"&date&"#))"&norp
- 10. 日期
- 9. 列出不是 汉字, 字母, 数字 开头的数据
- sql="select * from ctarticle where (title not between '阿' and '做祚') and (title not like '[0-9a-z]%')"
- //Access 中 用 * 代替 % 号
- 8. 为不同数据库表中的相同列赋值, 并进行条件查询
- "SELECT article.*, class.* FROM article, class WHERE article.classid=class.classid and article.classid="&request("classid")
- // SELECT 表1所有列,表2所有列 FROM 表1,表2 WHERE 表1.栏1=表2.栏1 AND 表1.栏1=条件
- 7. 统计记录数
- <%
- set rsC=server.CreateObject("adodb.recordset")
- rsC.ActiveConnection=MM_conn_STRING
- rsC.open "select count(*) as totalC from article",MM_conn_STRING
- %>
- <%= rsC("totalC") %>
- <%
- rsC.Close()
- set rsC=nothing
- %>
- 6. 以两个列进行排列
- "SELECT * FROM nclass order by classid,nclassid "
- // SELECT 所有 FROM 数据库表 ORDER BY 先按 classid 进行排序, 再按 nclassid 进行排序
- 5. 查询一个范围内的数据
- Recordset1.Source = "SELECT articleid, content, nclassid FROM article where (articleid<=2064 and articleid>=2037) "
- //此语句查询 articleid 2037 至 2064 之间的所有数据
- 4. 查询多个数据表
- "SELECT class.*,nclass.*,article.* FROM class,nclass,article WHERE article.classid=class.classid and article.nclassid=nclass.nclassid and article.articleid="&Request.QueryString("articleid")
- //先用 "SELECT 表.*" 查询表中所有列, 再用 "FROM 数据表" 选择查询的数据表, 再用 "WHERE 表.列=表.列" 合并各个数据表相同中的名称
- 3. 计数器加 1
- "UPDATE article SET hits=hits+1 WHERE articleid="&request("articleid")
- 2. 用于在某页面显示某分类的查询法, 并用 ORDER BY 排序
- "SELECT articleid, classid, title FROM article where classid="& Request.QueryString("classid") & " ORDER BY articleid DESC"
- // 注意 ORDER BY 的空格!!! 这个地方: " ORDER BY articleid DESC"
- 1. DW 链接 ACCESS 数据库
- conn
- "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("xxx.mdb")
- ---/---------------------------------------------------------
- important skill
- 2. 数据查询
- 1. 翻页 / 问题解决 2006-4-23
- ---/---------------------------------------------------------
- GENERAL KNOWLEDGE
- 4. 外部连接和自联接
- inner join(等值连接) 只返回两个表中联结字段相等的行
- left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
- right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
- on 指定表间联结字段及其关系的等号 "=" 表达式, 返回 true 或 false. 当表达式返回 true 时, 则查询中包含该记录.
- ! 外部连接只能操作已存在于数据库中的数据
- 3. insert into 表 (列1,列2) values(数字,'字符')
- 例: "INSERT INTO article (classid,nclassid,title,content ) VALUES (1,1,'whattttttt','cr') "
- // 插入数据到所选列
- 2. drop table 表
- // 删除表
- 1.1 查找不是 A-Z 和 0-9 开头的内容
- select * from ctglossary where gname not like '[A-Z0-9]%[a-z0-9]' order by gname desc
- 1. SELECT 列 FROM 表 WHERE 行 ORDER BY 序
- ---/---------------------------------------------------------
- 数据库操作语句
- 16. having 子句 / 筛选结果
- 15. 文本函数
- 14. Group By / 总结数据
- 13. 时间日期
- 12. nz 行函数 / 替换空值
- 11.1 视图操作例 一
- 11. ASP 操作 Access 视图
- 10. iif 函数
- 9. 自连接
- 8. union | union all / 联合查询
- 7. 外连接——交叉查询
- 7.1 查询
- 7.2 等值连接
- 7.3 右外连接
- 7.4 左外连接
- 7.5 更新操作
- 6. 内连接 | Select 别名1.列, 别名2.列 FROM 表1 别名1, 表2 别名2
- 5. 列操作 | connection&alter table 表名 add/drop column 列名
- 4. 删除数据 | connection&delte from
- 3. 更新数据 | connection&update
- 2. 插入数据 | connection&Insert into
- 1. 连接数据库 | Recordset&Select
- 0. 查找数据库中的表/视图
- -1. 创建 B表, 并从 A表中 选择数据插入 B表
- //----------------------
- 16.1 查找标题重复 大于或等于 2 的文章
- select title, count(articleid) as ct from ctarticle group by title having count(articleid)>1
- //解读
- //选择 标题, 计算(文章ID) 赋 别名 从 表 总结 标题 有 计算(文章ID) 大于 2
- 注: 计算别名在 having 子句里没有意义
- 16. having 子句 / 筛选结果
- 15.3 trim(gname) //格式函数
- UPDATE tgg SET gname = trim(gname);
- //解读
- //更新 表 设置 列=删除两边空格(列)
- 15.2.1 从上例中加入逻辑判断
- update tgg set subgname=replace(subgname,'<' or '{','')
- //实例操作中此语句运行无效, 还是下面方法管用
- update tgg set subgname=replace(subgname,'{','')
- 15.2 replace(字段名,查找字符,替换字符) //替换函数
- update tgg set subgname=replace(subgname,'<','')
- //解读
- //更新 表 设置 列=替换(列,字符a,字符b)
- 15.1.2 有 mid 就有 left, right
- 15.1.2.2
- UPDATE tgg SET subgname = left(gname,15);
- 15.1.2.1
- UPDATE tgg SET subgname = right(gname,15);
- 15.1 mid(name,start by num,end by num) //提取字段中的字符
- UPDATE ctglossary SET subgname = mid(gname,1,20)
- WHERE subgname='unsubtitle';
- //更新 表 设 列1 = MID(列2,1,20) //提取 列1 的前20 个字符更新给 列1
- 在哪里 列1 等于 条件
- 15. 文本函数
- 14.1 显示父类数量, 子类数量, 文章数量. 结果为级联拥有的数量
- select classid,nclassid,count(*) as articles from ctarticle group by classid,nclassid
- //解读
- //选择 ID1, ID11, 计算(所有) 赋 别名 从 表 总结 ID1, ID11
- 结果如:
- classid | nclassid | articles
- 1 + 10 + 100
- etc...
- //类似于数组显示
- 14. Group By / 总结数据
- 心得: Group By 功能为总结相同的数据, 并适当分组显示. 如果结合 having 子句, 可以实现 where 的筛选功能, 也就是减少操作步骤, 只是一步.
- 13.5.1 列出某一天, 上一星期的数据
- SELECT distinct dateandtime
- FROM ctdate_by_query_date
- WHERE dateandtime between ((#2006-5-15#+(Weekday(2006-5-15)-7))-6) and (#2006-5-15#-7)+Weekday(2006-5-15)
- sql="SELECT distinct dateandtime FROM ctarticle WHERE dateandtime between ((#"&date&"#+(Weekday("&date&")"&norp&"7))-6) and (#"&date&"#"&norp&"7)+Weekday("&date&")"
- 13.5 查询一天, 所隶属星期所有天数的数据
- SELECT *
- FROM ctdate_by_query_date
- WHERE dateandtime between ((#2006-5-15#+Weekday(2006-5-15))-6) and #2006-5-15#+Weekday(2006-5-15)
- 13.4 查询一个时间段
- SELECT *
- FROM ctdate_by_query_date
- WHERE dateandtime between #2006-5-1# and #2006-5-30#
- 13.3.2 列出不同年份的年份, 并且不要相同
- select distinct year(dateandtime) from ctarticle
- 结果如:
- Expr1000
- 2000
- 2003
- 2004
- 2005
- 2006
- 13.3.1 列出某一天, 上一年的第一条记录
- SELECT top 1 dateandtime FROM ctarticle where year(dateandtime)=(2006)-1
- sql="SELECT top 1 dateandtime FROM ctarticle where year(dateandtime)=(year(#"&date&"#))"&norp
- 13.3 列出某一年的数据
- sql="select * from ctdate_by_query_date where year(dateandtime)="&year(rqqdt_)&" order by dateandtime desc"
- 13.2.1 查找上一月中的第一条记录
- SELECT top 1 dateandtime FROM ctarticle where year(dateandtime)=year(#2006-5-28#) and month(dateandtime)=month(#2006-5-28#)-1
- 13.2 列出某一月的数据
- sql="select * from ctdate_by_query_date where year(dateandtime)="&year(rqqdt_)&" and month(dateandtime)="&month(rqqdt_)&" order by dateandtime desc"
- 13.1 列出某一日的数据
- sql="select * from ctdate_by_query_date where dateandtime=#"&rqqdt_&"# order by a.articleid desc"
- 13. 时间日期
- 例一: 列出当天的数据
- sql="select * from ctdate_by_query_date where dateandtime=date() order by a.articleid desc"
- 12.3 更新多个值的空值为 ##
- UPDATE ctglossary SET
- gname = nz(gname,"untitle"),
- gcontent = nz(gcontent,"gcontent empty..."),
- submiter = nz(submiter,"anonymous"),
- gsource = nz(gsource,"unknown"),
- gurl = nz(gurl,"/"),
- submitermail = nz(submitermail,"@"),
- subgname = nz(subgname,"unsubtitle")
- 12.2 更新空值为 ##
- update tspic set pname=nz(pname,'no name')
- //解读
- //更新 表 设置 列=替换空值(列,'没有名字')
- 12.1 替换空值为 ##
- select picid, nz(pname,'no name') from tspic
- //解读
- //选择 列1, 替换空值(列2,'没有名字') 从 表
- 12. nz 行函数 / 替换空值
- 11.1.2 ASP 连接视图操作
- rs.open "select * from date_by_query_date where dateandtime=#"&rqqdt_&"#",MM_conn_String
- //何时使用视图, 在单表操作时, 不需要考虑视图; 在多表操作时, 应该使用视图, 表一多, 逻辑就复杂, 复杂就容易出错.
- 11.1 视图操作例 一
- 11.1.1 建立视图, 并保存为: date_by_query_date
- SELECT a.*, b.*, c.*, d.*
- FROM ((ctarticle AS a LEFT JOIN ctclass AS b ON a.classid = b.classid) LEFT JOIN ctnclass AS c ON a.nclassid = c.nclassid) LEFT JOIN cttag AS d ON a.articleid = d.articleid;
- 11. ASP 操作 Access 视图
- 注: 操作视图跟一般操作原理一样, 只不过操作视图的时候, 数据先经过筛选.
- 10.1 判断 articleid 是事大于 2000, 满足条件打印出 '不错'
- select iif(articleid>2000,'不错',' ') as ok from ctarticle where articleid between 1990 and 2050
- 10. iif 函数
- 9.1 自连接1
- select top 10 a.title, a.articleid, b.title as title2, b.articleid as articleid2 from ctarticle a,ctarticle b
- 9. 自连接
- 8.3 union 编写全外连接
- select a_cs.*, b_ncs.*
- from ctclass a_cs left join ctnclass b_ncs on a_cs.classid=b_ncs.classid
- union
- select c_cs.*, d_ncs.*
- from ctclass c_cs right join ctnclass d_ncs on c_cs.classid=d_ncs.classid
- //解读
- 选择 别名1.所有, 别名2.所有
- 从 表1 别名1 左外连接 表2 别名2 在 别名1.列1=别名2.列1
- 联合到
- 选择 别名3.所有, 别名4.所有
- 从 表1 别名3 右外连接 表2 别名4 在 别名3.列1=别名4.列1
- 8.2 select top 10 articleid,classid from ctarticle UNION all select top 10 articleid,classid from cttag;
- //union all 不删除重复行, 不进行自动排序, 执行效率高.
- 注: 做事效率明显会降低
- 8.1 select top 10 articleid,classid from ctarticle UNION select top 10 articleid,classid from cttag;
- 一般联合查询
- //union 查询自动删除重复行和排序
- 8. union | union all / 联合查询
- 7.5.2
- UPDATE (ctarticle AS a LEFT JOIN ctclass AS c ON a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid
- SET tag=tag+' ', b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
- WHERE a.classid=23 AND a.nclassid=0 AND tagid is not null
- 7.5.1
- UPDATE (ctarticle AS a LEFT JOIN (ctnclass AS c LEFT JOIN ctclass AS d ON c.classid = d.classid) ON a.nclassid = c.nclassid AND a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid SET tag=d.class+' '+c.nclass, b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
- WHERE a.classid=23 AND a.nclassid=197;
- 7.5 更新操作
- 74.5 左连接中数据的筛选
- INSERT INTO cttag(articleid,classid,nclassid) SELECT a.articleid,a.classid,a.nclassid from ctarticle a left join cttag b on a.articleid=b.articleid where b.articleid is null
- //本语句功能为, 显示主表的全部内容, 插入数据到副表中没有的数据
- //主要作用为: 让数据减少冗余
- 7.4.4.1 上例中的延续
- SELECT a.*, b.*, c.*, d.*
- FROM cttag as d left join ((ctarticle AS a LEFT JOIN ctclass AS b ON a.classid=b.classid) LEFT JOIN ctnclass AS c ON a.nclassid=c.nclassid) on d.articleid=a.articleid;
- 7.4.4 显示文章表中的全部, 调用类别表中的栏目
- select a.*, b.*, c.* from (ctarticle a left join ctclass b on a.classid=b.classid) left join ctnclass c on a.nclassid=c.nclassid
- //作用, 有时在文章表中包含了在个别类别表中没有的数据, 用这个语法可以读出文章表的全部数据
- //a 为 文章表, b 为主类别, c 为子类别
- 7.4.3 同上例, 选择追加数据时加上空格
- INSERT INTO cttag(articleid,classid,nclassid,tag)
- SELECT a.articleid,a.classid,a.nclassid,d.class+' '+c.nclass
- FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=4 and a.nclassid=154;
- 7.4.2 连接N个表, 并追加数据到其中一个表, N=4
- INSERT INTO cttag(articleid,classid,nclassid,tag)
- SELECT a.articleid,a.classid,a.nclassid,d.class+c.nclass
- FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
- //解读
- 插入到 表2(栏1,栏2,栏3,栏4)
- 选择 别名a.栏1, 别名a.栏2, 别名a.栏3, 别名d.栏4 加上 别名c.栏5
- 从 (表1 别名a 左连接 (表3 别名c 左连接 表4 别名d 在 别名c.栏2 等于 别名d.栏2) 在 别名a.栏2 等于 别名c.栏2 和 别名a.栏3=别名c.栏3) 左连接 表2 别名b 在 别名a.栏1 等于 别名b.栏1 在那里 别名a.栏2=1 和 别名a.栏3=1
- 7.4.1 连接两个表, 并追加数据到其中一个表
- INSERT INTO cttag(articleid,classid,nclassid)
- SELECT a.articleid,a.classid,a.nclassid
- FROM ctarticle AS a LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
- //解读
- 插入到 表2(栏1,栏2,栏3)
- 选择 别名a.栏1, 别名a.栏2, 别名a.栏3
- 从 表1 别名a 左连接 表2 别名b 在 别名a.栏1 等于 别名b.栏1 在那里 别名a.栏4=1 和 别名a.栏5=1
- 7.4. 左连接
- 7.3.1 同步两表的数据
- UPDATE ctarticle a INNER JOIN cttag b ON a.articleid = b.articleid SET b.classid=a.classid, b.nclassid=a.nclassid;
- //解读
- 更新 表1 别名a 联接 表2 别名2 在 别名a.栏1 等于 别名b.栏1 设置 别名b.栏2 更新为 别名a.栏2, 别名b.栏3 更新为 别名a.栏3
- 7.3 右外连接
- select a.*, b.* from bunclass a right join ctclass b on a.classid=b.classid where a.nclassid=20
- 查询别名 a,b 表, 只匹配 b 表中的内容.
- 7.2.3 添加数据到连接表之一
- INSERT INTO cttag ( tag, articleid ) SELECT top 1 b.tag, a.articleid FROM ctarticle AS a left JOIN cttag AS b ON a.articleid = b.articleid WHERE a.articleid order by a.articleid desc;
- 7.2.2 变通中的用法二
- INSERT INTO bureply
- SELECT b.*, a.classid, a.nclassid
- FROM article AS a INNER JOIN reply AS b ON a.articleid = b.articleid
- WHERE classid=50;
- 7.2.1 实际应用中的变通
- INSERT INTO butag ( tag, articleid, classid, nclassid)
- SELECT b.tag, a.articleid, a.classid, a.nclassid
- FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
- WHERE classid=24;
- 7.2 添加数据到其他表
- INSERT INTO butag ( tag, articleid )
- SELECT b.tag, a.articleid
- FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
- WHERE a.articleid<>False;
- //解读
- 添加到 接收表(列1,列2)
- 选择 别名b.列1, 别名a.列2
- 从 表1 表名a 联接 表2 表名b 在 别名a.列c 等于 别名b.列c
- 在哪里 别名a.列c 不等于 没有
- 7.1.1 实际应用中的变通
- SELECT b.tag, a.articleid, a.classid, a.nclassid
- FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
- WHERE a.classid=24;
- 7.1 查询
- SELECT b.tag, a.articleid
- FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
- WHERE a.articleid<>False;
- //解读
- 选择 别名b.列, 别名a.列
- 从 表1 别名a 联接 表2 别名b 在 别名a.列c = 别名b.列c
- 在哪里 别名a.列c 不等于 没有
- 注: as 不是必要
- 7. 外连接——交叉查询
- 提示: 注意表中相同的栏目
- 6. 内连接
- SELECT a.*, b.* FROM article a, nclass b where b.nclassid=44;
- 选择 别名1.所有, 别名2.所有 来自 表1 赋别名, 表2 赋别名 在什么地方 别名2=44
- 5.1 从表中删除列——Access 数据视图操作
- alter table news
- drop column
- url,
- www,
- writer,
- email,
- classid,
- nclassid,
- hits,
- dateandtime;
- //从 News 表 删除 栏目 栏目名
- 5.从表中添加列——Access 数据库视图操作
- alter table news
- add column
- url varchar(255),
- www varchar,
- writer varchar,
- email varchar,
- classid byte,
- nclassid byte,
- hits byte,
- dateandtime datetime;
- //从 News 表 添加 栏目 栏目名 数据类型
- 4.1 delete from article where classid=23 and nclassid=156;
- 4. 删除数据
- <% '删除数据
- set rsDelete=server.CreateObject("adodb.connection")
- rsDelete.open MM_conn_String
- '注意, 输入为数字不能加单引号
- sql= "DELETE FROM article WHERE articleid in(3263, 3262, 3261)" '删除 ID 为 3263, 3262, 3261 的文章
- rsDelete.execute(sql)
- rsDelete.close
- set rsDelete=nothing
- response.Redirect("../index.asp") '返回首页
- %>
- 3.3 同步两表中的数据
- UPDATE ctarticle AS a, ctarticle1 AS b SET a.title=b.title
- WHERE a.articleid=b.articleid;
- 3.2 在本列的值上加上另一列, 也叫合并列, 但是只合并数据到列, 而不合并列本身.
- UPDATE GiArticles SET content=c1+content;
- 3.1 更新数据库表中的多个行
- <% '更新数据库表中的多个行
- set rsUpdate=server.CreateObject("adodb.connection")
- rsUpdate.open MM_conn_String
- sql = "update test set id=0 where id is null"
- rsUpdate.execute(sql)
- rsUpdate.close
- set rsUpdate=nothing
- %>
- 3. 更新数据
- <% '更新数据
- set rsUpdate=server.CreateObject("adodb.connection")
- rsUpdate.open MM_conn_string
- '数字不加单引号, 字符加, 日期加井号
- sql= "update article set classid="&classid_&", nclassid="&nclassid_&", writer='"&writer_&"', email='"&email_&"', www='"&www_&"', url='"&url_&"', title='"&title_&"', content='"&content_&"' where articleid="&request("articleid")
- rsUpdate.execute(sql)
- rsUpdate.close
- set rsUpdate=nothing
- %>
- 2.1.2
- INSERT INTO article(content, title, url, www, writer, email, classid, nclassid, hits, dateandtime) select content, title, url, www, writer, email, classid, nclassid, hits, dateandtime from newbb_backup;
- 2.1.1 从不同的数据库表选择数据并插入
- sql = "INSERT INTO articleInsertTest(title,content) select top 50 title,content from Newbb_posttext"
- 2.1
- <% '从不同的数据库表选择数据并插入
- set rsInsert = Server.CreateObject("ADODB.connection")
- rsInsert.open MM_conn_STRING '数据库连接字符串
- '注意单引号的使用
- sql = "INSERT INTO buarticle select article.* from article where articleid=3445 "
- '从 article 表选择 articleid=3445 的数据, 插入到 buarticle 表
- rsInsert.Execute(sql) '执行数据库插入语句
- rsInsert.close
- set rsInsert=nothing
- %>
- 2. 插入数据
- <% '插入数据
- set rsInsert = Server.CreateObject("ADODB.connection")
- rsInsert.open MM_conn_STRING '数据库连接字符串
- '注意单引号的使用
- sql = "INSERT INTO article (classid,nclassid,title,content ) VALUES ("&classid_&","&nclassid_&",'"&title_&"','"&content_&"' ) " '选择列, 并插入数据
- rsInsert.Execute(sql) '执行数据库插入语句
- rsInsert.close
- set rsInsert=nothing
- %>
- 1.2 参数查询
- SELECT *
- FROM ctarticle AS a
- WHERE a.classid=[@classid o, please input] AND a.nclassid=[@nclassid];
- 1.1 子查询
- "select top 10 * from (select top 1000 * from ctarticle) a"
- 1. 连接数据库
- <% '连接数据库
- set rs=server.CreateObject("adodb.recordset") '创建 rs 数据查询
- rs.open "select * from article",MM_conn_String '数据查询语句
- response.Write rs("title") '简单读出数据
- rs.close '关闭 rs 连接
- set rs=nothing
- %>
- 0. 查找数据库中的表
- <% '查找数据库中所有数据库表名, 与视图为查询类型的视图
- set rs=server.createobject("Adodb.connection")
- rs.open MM_conn_string
- set rsSchema=rs.openSchema(20)'20 指定查找表,视图; 23 指定查找视图
- while not rsSchema.EOF
- if rsSchema("TABLE_TYPE")="TABLE" then '显示表名
- response.write "/--- "
- response.write rsSchema("TABLE_NAME")
- response.write "<br>"
- end if
- if rsSchema("TABLE_TYPE")="VIEW" then '显示视图名
- response.write "-----------------------/ "
- response.write rsSchema("TABLE_NAME")
- response.write "<br>"
- end if
- rsSchema.movenext
- wend
- rsSchema.close
- set rsSchema=nothing
- rs.close
- set rs=nothing 'code by shawl.qiu
- %>
- -1.1 从 A 表创建 B 表, 只选择 A 表的结构
- select * into tshm from ctarticle where 1=0
- -1. 创建 B表, 并从 A表中 选择数据插入 B表
- select articleid,title into ctarticle1 from ctarticle
- ---/---------------------------------------------------------
- Problems List
- -----------------------
- Current Item 5
- -----------------------
- 1. 列出所有年份, 并且不重复显示, 只显示该年一次. 20:30 2006-5-28
- 例: 2001, 2002, 2003, 2004
- 2. 列出所有月份, 并且不重复显示, 只显示年中不同月份一次. 20:30 2006-5-28
- 例: 2001-08, 2001-09, 2001-10, 2001-11
- 3. 同时更新多个表的数据
- //不可能同时更新, 只能分线程更新 20:30 2006-5-28
- 4. ASP 可否使用视图, 或者如何使用视图 20:28 2006-5-28
- 5. ASP 按拼音查找数据 3:03 2006-5-17 20:28 2006-5-28
- ---/---------------------------------------------------------
- Access 操作相关
- 5. 在插入数据时, 如果在一个 RS 连接未关闭的时候, 在其中再选择最末的数据, 那只会选择在未插入数据时最本的一条.
- 4. 查找替换框中 正则的应用
- 4.1 ?[}]*
- 4.2 ?[>]*
- 3. 在知道要取出多少条语录时用 for 循环, 不知道用 while 循环。
- while 循环 有一个小毛病, selete top * 在指定 排序栏目的 ID 出现重复的时候,会出错。
- 2. 页面调用数据库表的内容, 应分为显示主表和副表. 15:18 2006-5-19
- 用外连接显示主表的全部内容, 副表只作为配称.
- 1. 在 Access 操作视图中, 可能的话, 不要使用关系. 20:13 2006-5-18
- !但在数据库语句中喜欢怎么用就怎么用.
- ---/---------------------------------------------------------
- 建表必须
- 2. 前缀
- 命名表应考虑适当的前缀
- 1. 日期时间
- !日期 与 时间 应该为不同的两个列, 方便数据调用
- ---/---------------------------------------------------------
- 建表需要考虑的列
- 3. 权限列
- 2. 排序 ID
- 方便管理
- ! 排序 ID 应设置两个, 一个为版面, 一个为导航.
- 1. tag
- 标签, 现在流行, 也方便
- ---/---------------------------------------------------------
- 数据库不能做的事
- 1. 不能同时添加全新的多个行, 只能分开添加; 添加全新的多个行只能取材于数据库已有的行.
- ---/---------------------------------------------------------
- 规则
- 1. 在默认情况下, 当查找表主键中的值出现在数据表的外键中时, 不允许更改或者删除这些数值.
ASP + Access 初阶笔记 By Shawl.qiu
最新推荐文章于 2021-01-31 22:52:47 发布