Results 1 to 5 of 5
  1. #1
    nomadsoul is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2015
    Posts
    2

    Proper Foreign Key Syntax?

    Hi I can't find the proper sql syntax for defining the FK. I'm creating two tables; customers and orders. In the orders tables the cust_ID is the FK. Well, I thought the syntax below was correct but it wasn't accepted when I ran the Create Orders Table statement. If this is not the proper FK reference can someone tell me what is? I've looked every where. I want to use strict sql and avoid any query builders and such. I can't find any good access sql syntax resources either.
    Thank You.
    -v2007

    The customer table:
    Create Table Customers
    (


    Cust_ID int NOT NULL Primary Key,
    LastName varchar(20) NOT NULL,
    FirstName varchar(20),
    Street varchar(40),
    State char(2),
    Phone varchar(15),
    PostCode varchar(20)
    );

    The Orders Table:
    Create Table Orders
    (
    Ord_ID int NOT NULL Primary Key,
    OrderNO int NOT NULL,
    ShippedVia varchar(30),
    ShipAddress varchar(55),
    ShipCity varchar(30),
    ShipRegion varchar(1),
    DateOfDelivery DATETIME,
    Cust_ID int Foreign Key References Customers(Cust_ID)
    );

    The error high-lights the keyword 'Foreign' as when I run it. But there's no problem creating the customer table.

  2. #2
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    As you self describe yourself as novice - I am wondering why you seek to create tables via a code method?

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Here you are - try this:

    currentdb.execute "Create Table Orders (
    Ord_ID int NOT NULL Primary Key,
    OrderNO int NOT NULL,
    CustID Int,
    ShippedVia varchar(30),
    ShipAddress varchar(55),
    ShipCity varchar(30),
    ShipRegion varchar(1),
    DateOfDelivery DATETIME,
    CONSTRAINT FKOrdersCustID Foreign Key(CustID) References Customers );

    It worked when I tried it in A2010.

    I found all the information I needed in the MS Access VBA help, searching on "foreign Key"

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I agree with NTC. This is your first post-- why are you creating tables via code?

    Have you done a database design/erd?
    When you show code it is best to show all procedure code.

    You may find w3schools sql a helpful sight.

    Good luck with your project, and welcome to the forum.

  5. #5
    nomadsoul is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2015
    Posts
    2
    Hi NTC
    I'm not really a db or sql novice -- only an Access novice. I'm actually teaching a general db class at and I want them to understand the foundations through SQL before we expand our class project. I'm having my students use Access because most already have it on their drives. Last time I used it was when I was a student (2003) since then I've favored MySQL and SQL Server. I like Access but find it harder to use than before.
    John:
    I tried it and it works, thanks!
    I also successfully tried this and it worked:

    Create Table Orders
    (
    Ord_ID int NOT NULL Primary Key,
    Cust_ID int NOT NULL,
    OrderNO int NOT NULL,
    ShippedVia varchar(30),
    ShipAddress varchar(50),
    ShippedCity varchar(30),
    ShipRegion Varchar(1),
    DateOfDelivery DATETIME,
    Foreign Key (Cust_ID) References Customers(Cust_ID)
    );

    Orange:
    Thanks, actually I tried w3c first and got the original syntax:
    Cust_ID int Foreign Key References Customers(Cust_ID)
    Modeled from:P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
    from this page:
    http://www.w3schools.com/sql/sql_foreignkey.asp

    I also trust this resource so I'm really curious why it didn't work.

    Thanks for the great replies, I hope to learn much more from this forum.


Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 10
    Last Post: 04-01-2014, 05:01 PM
  2. Proper ERD
    By Zealotwraith in forum Access
    Replies: 1
    Last Post: 09-03-2013, 10:29 PM
  3. Replies: 10
    Last Post: 05-08-2012, 09:17 AM
  4. Replies: 2
    Last Post: 04-19-2012, 11:29 AM
  5. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums