使用vb.net应用程序访问的MS Acess 2007数据库 我有两个现有的表
Members
-------
ID name bandID
-----------------------
0 Pierre 1
1 Chart 3
2 John 3
3 Dave 2
Bands
-----
ID bandName
----------------
1 Band a
2 Band b
3 Band c
我想在member.bandId和bands.ID
之间的关系中添加级联到null约束这就是我所拥有的
ALTER TABLE members ADD CONSTRAINT membresBands_FK
FOREIGN KEY (bandID) REFERENCE Bands(ID) ON DELETE CASCADE SET NULL
但我收到此错误消息:
CONSTRAINT子句中的语法错误
从msdn我发现
CREATE TABLE Orders
(OrderId INTEGER PRIMARY KEY,
CustId INTEGER,
OrderNotes NCHAR VARYING (255),
CONSTRAINT FKOrdersCustId FOREIGN KEY (CustId)
REFERENCES Customers ON UPDATE SET NULL ON DELETE SET NULL
是否可以在MS Access中更改表以将关系设置为级联为空?
谢谢!
答案 0 :(得分:4)
你可以在Access中创建这种约束,但只能通过Jet OLE DB提供程序和ADO创建。例如,使用Access中的数据库,您可以通过运行以下VBA代码来创建约束:
CurrentProject.Connection.Execute "ALTER TABLE membres ADD CONSTRAINT membresBands_FK FOREIGN KEY (bandID) REFERENCES bands(ID) ON DELETE SET NULL"
答案 1 :(得分:1)
我不得不等八个小时发布这个...
使用visual basic模块
'Define the bit value for the relation Attributes.
Public Const dbRelationCascadeNull As Long = &H2000
Public Function MakeRel()
'Purpose: Create a Cascade-to-Null relation using DAO.
Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field
Set db = CurrentDb()
'Arguments for CreateRelation(): any unique name, primary table, related table, attributes.
Set rel = db.CreateRelation("membre_bands", "bands", "membres", dbRelationCascadeNull)
Set fld = rel.CreateField("ID") 'The field from the primary table.
fld.ForeignName = "band" 'Matching field from the related table.
rel.Fields.Append fld 'Add the field to the relation's Fields collection.
db.Relations.Append rel 'Add the relation to the database.
'Report and clean up.
Debug.Print rel.Attributes
Set db = Nothing
End Function
然后调用MakeRel函数
在http://allenbrowne.com/ser-64.html 上找到功能
答案 2 :(得分:0)
各种DBMS中级联效果的选项如下:
ON DELETE SET NULL
ON DELETE CASCADE
ON DELETE RESTRICT
ON DELETE NO ACTION
我认为MS-Access有前两个。所以,它应该是:
ON DELETE SET NULL
答案 3 :(得分:0)
AFAIK,Access中没有Cascade to Null
。仅限Cascade Delete
和Cascade Update
。
答案 4 :(得分:0)
docs for Access表示它支持以下引用触发操作:
ON DELETE CASCADE
ON UPDATE CASCADE
ON DELETE SET NULL
ON UPDATE SET NULL
......但是,实际上它只支持前三个,即不支持ON UPDATE SET NULL
。为了进一步说明,引擎根本不支持ON UPDATE SET NULL
引用触发操作,即不仅仅支持DDL语法。