Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

create a joint primary key for a access table?

Posted on 2004-09-01
3
Medium Priority
?
1,118 Views
Last Modified: 2012-05-05
following is the .ddl file, how to create a joint primary key?

create table bookcmptable (

  isbn varchar(255),
  copyNumber integer,
  title varchar(255),
  author varchar(255),
  studentkey integer,
  primary key (isbn, copyNumber)
);
0
Comment
Question by:she1
3 Comments
 
LVL 4

Expert Comment

by:chipple
ID: 11960311
By "joint primary key", you mean a primary key on several fields?
Your CREATE TABLE query already contains one. But if you want to add the primary key separately, try the following.

For example, if you did:

create table bookcmptable (
  isbn varchar(255),
  copyNumber integer,
  title varchar(255),
  author varchar(255),
  studentkey integer
);

You can do:

ALTER TABLE bookcmptable
ADD CONSTRAINT PK_bookcmptable PRIMARY KEY (isbn, copyNumber);

Good luck!
0
 

Expert Comment

by:Naveen13
ID: 11994180

Hi if you create a table with our a composite primary key..
You can use alter statement  to a modify the table

ALTER TABLE bookcmptable ADD PRIMARY KEY (isbn, copyNumber);

This helps you
naveen
0
 
LVL 1

Accepted Solution

by:
Medfly earned 120 total points
ID: 11999347
The way you have written your Create Table command.. you have already defined a "composite" or "joint" primary key.
---------------------------------------------------
create table bookcmptable (

  isbn varchar(255),
  copyNumber integer,
  title varchar(255),
  author varchar(255),
  studentkey integer,
  primary key (isbn, copyNumber)    <== PRIMARY KEY
);
---------------------------------------------------

The only thing I would do differently is name your Primary Key Constraint which would change your syntax to look something like

create table bookcmptable (

  isbn varchar(255),
  copyNumber integer,
       CONSTRAINT pk_bookcmptable PRIMARY KEY (isbn, copyNumber),
  title varchar(255),
  author varchar(255),
  studentkey integer,
  );

Does this help?  Or are you talking about making a Foreign Key reference to another table?
Cheers!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question