![]() the primary key INTO the first field record (w/o adding an additional 'id' field, as stated earlier. $query = "ALTER TABLE racehorses ADD id INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id)" This an end result sought (TABLE DESCRIPTION) - SQL-script: How to write ALTER statements to set Primary key on an existing table Ask Question Asked 11 years, 1 month ago Modified 4 days ago Viewed 965k times 258 I have an existing table called Person which already has existing 1000 rows of data. This a table currently created - Number of Records found: 4 name VARCHAR(20) YES How to write PHP to ALTER the already existing field (name, in this example) to make it a primary key? W/o, of course, adding any additional 'id' fields to the table. But for now I wanted to share this solution to a more challenging version of the original question in case others ran into this situation. I am guessing that I need to tear down all foreign keys before doing the ALTER table and then rebuild them afterwards. I get this message when I try to do a more complex example with an incoming foreign key from another table. ![]() What I have not been able to get working is the situation where there are incoming FOREIGN KEYs that already point at the USER_ID field. This should be done using the create table statement (you can also add an index by create index). In general it seems strange to add a primary key to an existing table. With the "DROP TABLE" at the top, you can run this over and over to experiment with variations. you cannot according to SQLite's syntax perform this operation. When this is done, the _USER_ID field exists and has all number values for the primary key exactly as you would expect. INSERT INTO SAKAI_USER_ID_MAP VALUES ('postmaster', 'postmaster') ĪDD _USER_ID INT AUTO_INCREMENT NOT NULL FIRST, ![]() INSERT INTO SAKAI_USER_ID_MAP VALUES ('admin', 'admin') I was able to adapt these instructions take a table with an existing non-increment primary key, and add an incrementing primary key to the table and create a new composite primary key with both the old and new keys as a composite primary key using the following code: DROP TABLE IF EXISTS SAKAI_USER_ID_MAP ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |