Can't get Constraints to work (1 Viewer)

Andyonline

New member
Local time
Today, 13:32
Joined
Apr 5, 2010
Messages
4
hi everyone, im trying to use constraints in sql queries to control a few things as a create a table, like on one table i want to control the allowed menutypes (lunch,dinner etc) and on another im trying to restrict an amount of money to be between two values.Trouble is i can't get either to work,and i don't know why.Any help greatly appreciated!Im using access 2007.
Code:
Create Table Menu(
MenuID GUID,
RestaurantID GUID not null,
StartDate Datetime,
EndDate Datetime,
MenuType char (9),
Primary Key (MenuID),
constraint FK_RestaurantID foreign key (RestaurantID) REFERENCES Restaurant (RestaurantID),
constraint chk_MenuType CHECK (MenuType in ("Breakfast","Lunch", "Dinner"))
);
and the second example of my failure,
Code:
Create Table MenuItem(
MenuItemID GUID,
MenuID GUID not null,
Name char (25),
Description memo,
ImageLocation char (50),
Price currency,
PreparationTime Integer,
Primary Key (MenuItemID),
constraint FK_MenuID foreign key (MenuID) REFERENCES Menu (MenuID),
constraint chk_price CHECK (Price in (>= 2.99 and <= 24.99))
);

All i keep getting are syntax errors but i really can't see the problem, and im new to using sql/access.
Thanks for looking !
 

Andyonline

New member
Local time
Today, 13:32
Joined
Apr 5, 2010
Messages
4
just to add,i know i can use access2007 to add constraints via gui option, but i need this working in sql.
 

Andyonline

New member
Local time
Today, 13:32
Joined
Apr 5, 2010
Messages
4
Anyone?Im still banging my head up the wall with this one,be happy for any advice.
 

Andyonline

New member
Local time
Today, 13:32
Joined
Apr 5, 2010
Messages
4
well dissappointing as it was to get not one single reply, i thought i'd share what i found in case anyone else is stuck- and that is access does'nt seem to support much in the way of constraints, and you'd need to be using SQL server for them to work.
 

Banana

split with a cherry atop.
Local time
Today, 05:32
Joined
Sep 1, 2005
Messages
6,318
I just saw the thread now.

I should point out that I don't think those are the same thing as ValidationText/ValidationRule which you can do via GUI.

The CHECK CONSTRAINTs expect a SQL Statement. For example, this is valid:

Code:
CONSTRAINT myConstraint CHECK (1=(SELECT COUNT(*) FROM aTable;))

The expression you used perhaps may be valid in ValidationRule but not for CHECK CONSTRAINT which is entirely distinct.

AFAIK, there is no DDL syntax for the ValidationRule/Text - those has to be done via DAO.

One option to make it work on any other database is to run a DDL script, then have some kind of additional text file or whatever that a function can read in to then create the ValidationRule via DAO. If that will work, do feel free to post back with questions.
 

Users who are viewing this thread

Top Bottom