在访问和/或 DAO 中创建检查约束

发布于 2024-08-09 21:04:49 字数 436 浏览 4 评论 0原文

我正在尝试在访问(jet?)表上创建检查约束。

因此,我使用访问权限打开 .mdb 文件,进入查询 -> 在设计视图中创建查询, 输入 esc,然后菜单->视图->查询,最后输入

create table X ( 一个数字, 检查(a>20) )

但访问认为我有“字段定义中的语法错误”。然而,我不这么认为。因此我的问题是:是否可以创建具有访问权限的检查约束。如果是这样:如何。

此外,我想使用 dao/vba 创建约束,而不是在 GUI 上。这可能吗?

最后,稍微相关一点:如何在 access 中输入 sql 语句。我无法想象我必须使用查询 - >设计视图 - >查询 - >视图路线才能做到这一点。我习惯了Oracle的SQL*Plus,我非常喜欢它,并且我希望也有类似的访问功能。

感谢您的任何意见 雷内

I am trying to create a check constraint on an access (jet?) table.

So, I open the .mdb file with access, go into queries->create query in design view,
type esc, then menu->view->query and finally type

create table X (
a number,
check (a > 20)
)

but access thinks that I have a "syntax error in field definition". However, I don't think so. Therefore my question: is it possible to create a check constraint with access. If so: how.

Additionally, I'd like to create the constraint with dao/vba, not on the GUI. Is that possible?

And lastly, on a slightly related note: how do you enter sql statements into access. I can't imagine that I have to use the queries->design view->query->view route in order to do that. I am used to Oracle's SQL*Plus, which I like very much, and I'd hope there's something similar for access as well.

Thanks for any input
Rene

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧情别恋 2024-08-16 21:04:49

这里有一些注意事项。

您可以为 Oracle 创建传递查询(选择菜单“查询”>“SQL 特定”>“传递”)

从 Access 2003 开始​​,您可以选择 SQL Server 兼容语法 (ANSI 92) (http://office.microsoft.com/en-us/access/HA010345621033.aspx)

使用 VBA / DAO 的验证规则

''Reference: Microsoft DAO x.x Object Library

Dim tdf As TableDef
Dim db As Database

Set db = CurrentDb

Set tdf = db.TableDefs("Table1")

tdf.Fields("aDouble").ValidationRule = "<10"
tdf.Fields("aDouble").ValidationText = "Must be less than 10"

使用 ADO / VBA 的约束。请参阅 [中级 Microsoft Jet SQL for Access 2000](http ://msdn.microsoft.com/en-us/library/aa140015(office.10).aspx)

''Reference: Microsoft ADO Ext. x.x for DDL and Security

Dim cn As ADODB.Connection 'For action queries
Dim rs As ADODB.Recordset  'For select queries
Dim s As String
Dim RecordsAffected As Long

Set cn = CurrentProject.Connection

''You can store sql in a table
s = DLookup("SQLText", "sysSQL", "ObjectName='q1'")
''Result: CREATE TABLE tblCreditLimit (LIMIT DOUBLE)
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

''You can run queries from VBA
s = "INSERT INTO tblCreditLimit VALUES (100)"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "CREATE TABLE tblCustomers (CustomerID COUNTER, CustomerName Text(50))"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "INSERT INTO tblCustomers VALUES (1, 'ABC Co')"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "ALTER TABLE tblCustomers " _
   & "ADD COLUMN CustomerLimit DOUBLE"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

''You can add contraints using ADO like so:
s = "ALTER TABLE tblCustomers " _
   & "ADD CONSTRAINT LimitRule " _
   & "CHECK (CustomerLimit <= (SELECT LIMIT " _
   & "FROM tblCreditLimit))"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "UPDATE tblCustomers " _
   & "SET CustomerLimit = 200 " _
   & "WHERE CustomerID = 1"
''Error occurs here
cn.Execute s, RecordsAffected

s = "UPDATE tblCustomers " _
   & "SET CustomerLimit = 90 " _
   & "WHERE CustomerID = 1"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

''Clean up
''You cannot do this through the database window,
''because of the constraint.
s = "ALTER TABLE tblCustomers DROP CONSTRAINT LimitRule "
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "DROP TABLE tblCustomers "
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "DROP TABLE tblCreditLimit "
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

Here are some notes.

You can create a Pass-Through query for Oracle (Select menu "Query" > "SQL Specific" > "Pass-Through")

Since Access 2003, you can select SQL Server Compatible Syntax (ANSI 92) (http://office.microsoft.com/en-us/access/HA010345621033.aspx)

A validation rule with VBA / DAO

''Reference: Microsoft DAO x.x Object Library

Dim tdf As TableDef
Dim db As Database

Set db = CurrentDb

Set tdf = db.TableDefs("Table1")

tdf.Fields("aDouble").ValidationRule = "<10"
tdf.Fields("aDouble").ValidationText = "Must be less than 10"

Constraints with ADO / VBA. See [Intermediate Microsoft Jet SQL for Access 2000](http://msdn.microsoft.com/en-us/library/aa140015(office.10).aspx)

''Reference: Microsoft ADO Ext. x.x for DDL and Security

Dim cn As ADODB.Connection 'For action queries
Dim rs As ADODB.Recordset  'For select queries
Dim s As String
Dim RecordsAffected As Long

Set cn = CurrentProject.Connection

''You can store sql in a table
s = DLookup("SQLText", "sysSQL", "ObjectName='q1'")
''Result: CREATE TABLE tblCreditLimit (LIMIT DOUBLE)
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

''You can run queries from VBA
s = "INSERT INTO tblCreditLimit VALUES (100)"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "CREATE TABLE tblCustomers (CustomerID COUNTER, CustomerName Text(50))"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "INSERT INTO tblCustomers VALUES (1, 'ABC Co')"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "ALTER TABLE tblCustomers " _
   & "ADD COLUMN CustomerLimit DOUBLE"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

''You can add contraints using ADO like so:
s = "ALTER TABLE tblCustomers " _
   & "ADD CONSTRAINT LimitRule " _
   & "CHECK (CustomerLimit <= (SELECT LIMIT " _
   & "FROM tblCreditLimit))"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "UPDATE tblCustomers " _
   & "SET CustomerLimit = 200 " _
   & "WHERE CustomerID = 1"
''Error occurs here
cn.Execute s, RecordsAffected

s = "UPDATE tblCustomers " _
   & "SET CustomerLimit = 90 " _
   & "WHERE CustomerID = 1"
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

''Clean up
''You cannot do this through the database window,
''because of the constraint.
s = "ALTER TABLE tblCustomers DROP CONSTRAINT LimitRule "
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "DROP TABLE tblCustomers "
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected

s = "DROP TABLE tblCreditLimit "
cn.Execute s, RecordsAffected
Debug.Print RecordsAffected
ま昔日黯然 2024-08-16 21:04:49

列上有验证规则。
您可以使用 VB for Access。这里没有 SQL*Plus...
您始终可以使用 SQL Express 作为数据源 - 具有真正 sql server 的所有优点,并且仅将访问用作前端。

There is Validation rule on a column.
You can use VB for Access. no SQL*Plus here...
You can always use SQL Express as a data source - with all the benefits of real sql server and use access only as a front.

红尘作伴 2024-08-16 21:04:49

要在 Access 中执行此操作,您需要首先打开界面进入 ANSI-92 查询模式。我已经测试了您的 SQL DDL 代码:它工作正常并创建了一个 FLOAT (双精度)类型的列。

使用 DAO 无法做到这一点,但可以使用 ADO。长话短说:在 Jet 4.0 时代,当访问团队偏爱 ADO 时,CHECK 约束被引入到引擎中。从 Access2007 开始,Access 团队重新开始青睐 DAO,但尚未堵塞 DAO 中的 Jet 4.0“漏洞”。因此,对于大多数 Jet 4.0 独有的功能(可压缩数据类型、固定长度文本数据类型、快速外键等),您需要使用 ADO。

To do this in Access, you need to first open the interface into ANSI-92 Query Mode. I've tested your SQL DDL code: it works fine and creates a column of type FLOAT (Double).

Is is not possible to do this using DAO but you can use ADO. Long story short: CHECK constraints were introduced into the engine in the Jet 4.0 era when the Access Team were favouring ADO. With effect from Access2007, the Access Team are back to favouring DAO but have yet to plug the Jet 4.0 'holes' in DAO. So for the majority of Jet 4.0 -only functionality (compressible data types, fixed-length text data types, fast foreign keys, etc) you need to use ADO.

剩余の解释 2024-08-16 21:04:49

您不能在查询生成器中使用标准 ANSI,除非您将数据库设置为 sql ansi 兼容。如果您确实更改了此设置,那么您可以像您一样在查询生成器中使用 sql。但是,我不建议更改现有数据库的此设置。

如果你这样做,你可以输入:

CREATE TABLE z1 
       (id int IDENTITY , FirstName CHAR, LastName CHAR, SSN INTEGER ,
check (id < 20),  
constraint Mypk primary key (id) )

In you don't need to save the sql in the query builder, and just 想输入 sql,然后简单地敲击 ctrl-g 来获取访问命令行提示符,然后你就可以然后输入:

currentproject.Connection.Execute "CREATE TABLE
    z1(id int IDENTITY , FirstName CHAR, LastName CHAR, SSN INTEGER ,
    check (id < 20),
    constraint Mypk primary key (id) )"

上面的内容将在一行中输入。因此,如果需要,您可以使用命令行提示符。

You can’t use standard ANSI in the query builder UNLESS you set the database as sql ansi compatible. If you do change this setting, then you CAN can use the sql in the query builder as you have. I would not recommend changing this setting for existing databases however.

If you do, you could type in:

CREATE TABLE z1 
       (id int IDENTITY , FirstName CHAR, LastName CHAR, SSN INTEGER ,
check (id < 20),  
constraint Mypk primary key (id) )

In you don’t need to save the sql in the query builder, and just want to type in the sql, then simply whack ctrl-g to get the access command line prompt, and you can then type in:

currentproject.Connection.Execute "CREATE TABLE
    z1(id int IDENTITY , FirstName CHAR, LastName CHAR, SSN INTEGER ,
    check (id < 20),
    constraint Mypk primary key (id) )"

The above would be typed on one line. So, you can use the command line prompt if you want..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文