473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL to create tables with foreign keys

Hello everyone,
I worked through my first database and have gone back to the assignment one about the court.
I seem to be having a great deal of trouble creating tables.
for example

create table(crown court),
crowncourtno (autonumber),
telno text (15),
numberofseats text(5),
constraints crowncourtno primary key_pk ));

I am using access 2003, it doesnt seem to like the brackets or the commas, and when i get rid of them it doenst like the names. I've been following advice on here, out of books and still its not working.

so please help.
sunday
Jun 27 '07 #1
9 6593
r035198x
13,262 8TB
Hello everyone,
I worked through my first database and have gone back to the assignment one about the court.
I seem to be having a great deal of trouble creating tables.
for example

create table(crown court),
crowncourtno (autonumber),
telno text (15),
numberofseats text(5),
constraints crowncourtno primary key_pk ));

I am using access 2003, it doesnt seem to like the brackets or the commas, and when i get rid of them it doenst like the names. I've been following advice on here, out of books and still its not working.

so please help.
sunday
Do not post questions in the articles sections.
Moved to the forum section.
Jun 28 '07 #2
so sorry I posted in the wrong place ( I was wondering why I hadnt had a reply)
I was wondering when creating tables in sql is the order you do them important and prehaps i should wait to create them all before runing them.
hope somone can put me on the right road.

sunday
Jun 28 '07 #3
JKing
1,206 Expert 1GB
Hi, I'll show you an example of creating a table in through SQL. The table is simple but you should be able to gather the format of things from it.

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE tblEmployee
  2. (
  3. empID COUNTER,
  4. empFName TEXT(25) NOT NULL,
  5. empLName TEXT(35),
  6. deptID number,
  7. CONSTRAINT pk_employee PRIMARY KEY(empID),
  8. CONSTRAINT fk_employee FOREIGN KEY(deptID) REFERENCES tblDept(deptID)
  9. )
  10.  
The order in which you create tables is important. You must create parent tables before creating child tables. So in this example I would not be able to create the employee table with a foreign key constraint on deptID if tblDept had not yet been created.
Jun 28 '07 #4
thanks so much for that,
I've been racking my brain on how to create them correctly, this database that i am working on has so many different tables.
I will keep trying now.
thankyou
sunday
Jun 29 '07 #5
HI again,
I've been getting along really well creating these tables but have hit another problem.
here is what I am trying to do.

create table case
(
caseno text (4),
judgeno text (4),
barristerno text (4),
constraint pk_caseno primary key
constraint fk_caseno foreign key (booking) references booking (caseno)
constraint fk_judgeno foreign key(judge) references judge (judgeno)
constraint fk_barristerno foreign key (prosecuting barrister) references barristerno (prosecuting barrister));

I have managed to create all the other tables with the foreign keys in, but cant seem to get this one to work.
please tell me what I am doing wrong again, its so frustrating.
sunday
Jun 29 '07 #6
JKing
1,206 Expert 1GB
I'm assuming you're trying to create the intermediate table in a many to many relationship. The three fields make up the primary key but they are also there own seperate foreign keys. Make sure to backup what you have before trying this out.

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE case
  2. (
  3. caseNo text(4),
  4. judgeNo text(4),
  5. barristerNo text(4),
  6. PRIMARY KEY (caseNo, judgeNo, barristerNo),
  7. FOREIGN KEY (caseNo) REFERENCES tblBooking(caseNo),
  8. FOREIGN KEY (judgeNo) REFERENCES tblJudge(judgeNo),
  9. FOREIGN KEY (barristerNo) REFERENCES tblBarrister(barristerNo)
  10. )
  11.  
Jun 30 '07 #7
thankyou again,
I have managed to create all my tables now and enforced the intergrity in the relationships table.
I was wondering if there is a way of inserting the data in groups?
At the moment I am inputing it one row at a time.
I am using the insert statement ie
insert into ( field 1, field2, field3)
values '1', '2' '3';

thanks again
sunday
Jun 30 '07 #8
ok I take it that I must imput the data one row at a time.
I would have thought that there would have been an easier way.
sunday
Jul 4 '07 #9
MMcCarthy
14,534 Expert Mod 8TB
ok I take it that I must imput the data one row at a time.
I would have thought that there would have been an easier way.
sunday
Have you got the data somewhere else? Excel, text file, etc.
Jul 5 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: ToMeK | last post by:
hi! i'm trying to builid web catalog and i'm having problems with organizing my tables. here's what i want: i have main categories, sub_categories and articles like this: CATEGORY_1 SUB_CAT_1...
8
by: Brian S. Smith | last post by:
Hi gang, Please help. I've been through the Access help, searched the Web, and I can't seem to get a straight answer. As the Subject line suggests, I want to run a fairly simple VB/Access Sub...
7
by: sea | last post by:
Is it a good idea to programatically create a primary key? For example in a table called names, I have the following fields, (1) firstname (2)lastname (3) ID - will it be ok to create a primary...
24
by: flkeyman | last post by:
Work in legal office. Trying to create solid designed database structure. This is list of tables w/fields and primary keys. Any comments/advice greatly appreciated. tbl-Defendants CaseNumber...
17
by: Wilfried | last post by:
Hi, I have 5 tables: main data1
2
by: Mindy | last post by:
Hey, I want to create links between my two tables. The primary keys of these two tables are character variables, with length =11. I followed exactly the instruction of Dummuy Book for ACCESS...
3
by: teedilo | last post by:
Our MS SQL (SQL Server 2000) DBA has database privileges locked down pretty tightly. We end users/developers do not have administrator privileges for most databases. That arrangement has worked...
2
by: bobby_b_ | last post by:
I have a table where fields 1 and 2 make up the primary key. Because of this, I have a unique composite index on fields 1 and 2 (as required by DB2). Now my question is: Fields 1 and 2 are also...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
4
by: shreedhan | last post by:
I have two tables table1 and table2 Table1 has a foreign key which references key of table2 Table2 also has a foreign key which references key of table1 Now when I try to insert into any of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.