Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I copy an existing table and give it a primary key?

Status
Not open for further replies.

BrimReap

Programmer
Mar 8, 2002
9
US
I have a table with out a primary key. I want to copy this table's data/structure into a new table with a primary key (with auto increment attribute). What code can I add on to this to acheive this? Will it assign id's to the current data?

select *
into NEW_TABLE
from OLD_TABLE
 
there are a couple of ways to do it

note that

SELECT INTO newtable FROM oldtable

will not work in mysql (that syntax is microsoft)


the way i would do it is simply alter the existing table and add the primary key to it


alter oldtable
add id integer not null primary key auto_increment

voila

rudy
SQL Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top