Microsoft Access Forums

Go Back   Microsoft Access Forums > Access Forums > Queries

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 03-04-2010, 01:37 AM
SteveI SteveI is offline Windows XP Access 2007 (version 12.0)
Novice
 
Join Date: Mar 2010
Posts: 6
SteveI is on a distinguished road
Default How do I set a primary Key when making a table with a Query?

Hello all, thanks for looking at my question. I am really quite new to Access (as you will probably tell).

I have created a table with a Make Table query which used a UNION query as the inputs.

The Table created has no primary key set and I wanted to set the primary key as the field 'Product' as this matches with another feld in a different table which I will use later. I would prefer it if this could be done as part of a query as I will carrying out this everyday on many tables and intend to make it into a Macro.

If anyone can tell me how to do this I would be greatly appreciative.

Thanks
Reply With Quote
  #2  
Old 03-04-2010, 01:59 AM
maximus's Avatar
maximus maximus is online now Windows 7 Access 2010 (version 14.0)
Wizard
 
Join Date: Aug 2009
Location: India
Posts: 712
Blog Entries: 1
maximus is on a distinguished road
Default

To assign a field as the primary key in an exsisting table you can run the following sql from a form a Commandbutton on a form using either Docmd.RunSql or Execute method.

assumptions:
Table Name: Persons (Type your table name)
Primary Key Fields: P_id, LastName (In your case it will be products)


ALTER TABLE Persons
ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)


Note:
If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created).


if his solves you problemmark the thread to be solved.
Reply With Quote
  #3  
Old 03-04-2010, 02:15 AM
SteveI SteveI is offline Windows XP Access 2007 (version 12.0)
Novice
 
Join Date: Mar 2010
Posts: 6
SteveI is on a distinguished road
Default

Thanks Maximus, this does work for the table in question. Here is the Sql to create the table, is it possble to put the ALTER TABLE into the CREATE TABLE Query rather than havng two seperate queries. I only ask because I am repeating this on lots of tables.

SELECT UNIONQueryPrices.* INTO NewTable
FROM UNIONQueryPrices;

If so please can you tell me where I can put the ALTER TABLE statement and the syntax I need to use (I said I was new to Access).

Don't know if makes a difference but there are two fields in the table Product and Price.

Or alternatively is it possible to make one query that updates several tables with the Primary key all on the field Product?

I really appreciate your help.
Reply With Quote
  #4  
Old 03-04-2010, 03:05 AM
maximus's Avatar
maximus maximus is online now Windows 7 Access 2010 (version 14.0)
Wizard
 
Join Date: Aug 2009
Location: India
Posts: 712
Blog Entries: 1
maximus is on a distinguished road
Default

Can you give me your table structure. i may have a solution that may be useful. cannot suggest until i get your table structure. alter and create table cannot be used together. But the Idea that i have is to create a table with all the required fields set the primary key and then Insert the values from the union query. plz send me the table structure.
Reply With Quote
  #5  
Old 03-04-2010, 03:26 AM
SteveI SteveI is offline Windows XP Access 2007 (version 12.0)
Novice
 
Join Date: Mar 2010
Posts: 6
SteveI is on a distinguished road
Default

Not sure what you measn by Table Structure so I have attached the whole database.

The tables I wish to merge together are UNIONFirsttoTable, UNIONSecondtoTable, UNIONThirdtoTable and UNIONFourthtoTable all into the same format as the Grades table.

Thanks
Attached Files
File Type: zip Experiment1.zip (45.3 KB, 1 views)
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making tables: use query or report or what? cowboy Access 7 01-07-2010 09:20 AM
making into update query tom4038 Queries 1 09-23-2009 09:19 AM
Form to add/edit rows in a table with a multi-field primary key JohnnyOz Forms 1 06-01-2009 11:09 AM
Making a new field in a query mslieder Queries 3 05-14-2008 09:44 AM
Making a backup copy of table or database wasim_sono Access 0 03-09-2006 02:44 AM


All times are GMT -8. The time now is 08:16 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.