我正在处理Access database,并且想将a另存CheckConstraint为查询。这甚至有可能Access吗?无论如何,这是我的查询:
Access database
CheckConstraint
Access
ALTER TABLE LEVERANCIER ADD CONSTRAINT chk_postcode CHECK( NOT EXISTS( SELECT levnr, postcode FROM LEVERANCIER WHERE Left(postcode, 4) = 5050 OR Woonplaats = "Amsterdam"));
它确实可以在AnySQL Maestro中工作,但是不能在Access中工作。
Access数据库引擎确实支持CHECK约束,但是不能从Access中的查询设计器中执行创建约束的DDL。必须使用VBA代码和ADO连接创建它们,如下所示:
Option Compare Database Option Explicit Public Sub AddCheckConstraint() Dim strSql As String strSql = _ "ALTER TABLE LEVERANCIER" & vbNewLine & _ vbTab & "DROP CONSTRAINT chk_postcode;" On Error Resume Next CurrentProject.Connection.Execute strSql On Error GoTo 0 strSql = _ "ALTER TABLE LEVERANCIER" & vbNewLine & _ vbTab & "ADD CONSTRAINT chk_postcode" & vbNewLine & _ vbTab & "CHECK (" & vbNewLine & _ vbTab & vbTab & "NOT EXISTS (" & vbNewLine & _ vbTab & vbTab & vbTab & "SELECT levnr, postcode " & vbNewLine & _ vbTab & vbTab & vbTab & "FROM LEVERANCIER " & vbNewLine & _ vbTab & vbTab & vbTab & "WHERE Left(postcode, 4) = '5050' OR Woonplaats = 'Amsterdam' " & vbNewLine & _ vbTab & vbTab & ")" & vbNewLine & _ vbTab & ");" CurrentProject.Connection.Execute strSql End Sub