MS SQL 2008

Hi,

Look at the followinng table:-

CREATE TABLE [dbo].[PageItems](
      [pageId] [int] NOT NULL,
      [itemId] [int] NOT NULL
) ON [PRIMARY]
GO



ALTER TABLE [dbo].[PageItems]  WITH CHECK ADD  CONSTRAINT [FK_PageItems_Items] FOREIGN KEY([itemId])
REFERENCES [dbo].[Items] ([id])
GO
ALTER TABLE [dbo].[PageItems] CHECK CONSTRAINT [FK_PageItems_Items]
GO


ALTER TABLE [dbo].[PageItems]  WITH CHECK ADD  CONSTRAINT [FK_PageItems_Pages] FOREIGN KEY([pageId])
REFERENCES [dbo].[Pages] ([id])
GO
ALTER TABLE [dbo].[PageItems] CHECK CONSTRAINT [FK_PageItems_Pages]
GO


Basicly it creates a table with some columns.  It then assigns pageId and itemId as foreign keys.
These keys are actually primary keys in the tables [Pages] and [Items].

My question is : where is the primary key to the table [pageItems] assigned? It doesn't seem to be given a primary key?

Many thanks for any help.

Kind regards,

Paul
plambkin1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AnujSQL Server DBACommented:
There is no Primary key defined on table PageItems, to assign PK


ALTER TABLE [dbo].[PageITems] ADD CONSTRAINT [PK_PageItems] PRIMARY KEY CLUSTERED  ([Key Colums])

eg: set PageID as PK

ALTER TABLE [dbo].[PageITems] ADD CONSTRAINT [PK_PageItems] PRIMARY KEY CLUSTERED  ([pageId])
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AnujSQL Server DBACommented:
To List out the pk of table

use EXEC sp_pkeys  @table_name = 'PageItems'
     
0
mcs0506Commented:
Hi,
This is basically association table between tables [Pages] and [Items]. If you want want refer [PageItems] records to other tables then you can add primery key e.g. PageItemsId otherwise there is no need to add PK in this table.


Dani
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

plambkin1Author Commented:
Thanks Dani - this is not my code but somebody elses that I am trying to understand.

But if this is an association table between [Pages] and [Items] - how van there be more than one record
if there is no primary key.

Paul
0
mcs0506Commented:
Yah there is a chance for duplication of association if primary key not exist.
For example page2 is associated to item1 may repeat number of times.
But you can stop this duplication by adding composite primary key(by using pageid and itemid combined as primary key).


dani
0
plambkin1Author Commented:
Hi - so in essence the way the code is at the moment there will be only ever one record in the table PageItems.

Paul
0
plambkin1Author Commented:
Hi - by the way have you any idea why I am getting the error "Syntax error BOOLEAN" in the code below when I run it true Apache Derby

CREATE TABLE Answers (
      id INTEGER GENERATED ALWAYS AS IDENTITY NOT NULL,
      text  VARCHAR(1000) NOT NULL,
      isCorrect BOOLEAN,
      explanation VARCHAR(1000) NULL,
      act BOOLEAN,
      version INTEGER NOT NULL
);

ALTER TABLE Answers ADD CONSTRAINT Answers_PRIMARY_KEY PRIMARY KEY (id);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.