English 中文(简体)
How can I insert data without specifying a value for a non auto-increment primary key?
原标题:
  • 时间:2009-11-12 22:15:51
  •  标签:
  • mysql
  • insert

I have this table:

CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)


1. insert into persons values (1,  John ,  Smith ,  LA ,  LA );
2. insert into persons (p_id, lastname, firstname, address, city) values  (2,  John ,  Smith ,  LV ,  LV );

How can I insert data into a table, without specifying a value for a primary key (the primary key doesn t have the "auto_increment" attribute).

I tried with:

  1. insert into persons values ( John , Smith , LA , LA );
  2. insert into persons values (null, John , Smith , LA , LA );
  3. insert into persons values (auto, John , Smith , LA , LA );

but no luck

最佳回答

I have seen this implemented as

  • DEFAULT value on the PK column (say -9999)
  • A table that holds the Current_PK value
  • An INSERT trigger that changes that PK from -9999 to Current_PK and increments Current_PK

Another way that I have seen is to get the MAX from the table and increment it and put it into the new row.

I must say, that both these methods caused bad data, caused locking and blocking and degraded performance.

In your case, I do not see a way other than actually specifying a value for PK.

问题回答

Primary keys cannot be null, and if you don t set them to auto increment, then how does MySQL know what you want to put in the table? You need to either specify the ID (perhaps by selecting the MAX(P_Id) and adding 1 to it) or set it to auto increment.

Although simply adding an auto increment field is definitely the best approach, you could try something like this:

INSERT INTO Persons (P_Id, FirstName, LastName, Address, City)
VALUES ((SELECT MAX(P_Id) + 1 FROM Persons),  John ,  Smith ,  LA ,  LA );

This uses a subselect, so if you re using an old version of MySQL it may not work.

 Select p_id+1 From Person OrderBy p_id desc limit 1;

When loading a row into a table with auto-increment you can specify a value provided that value is NOT less than the highest value in that key. This works the same as a UNIQUE key and will error as if it is a duplicate. Otherwise it will treat the new loaded value as the new highest value for that key.

I am most curious to know why anyone would want to use a primary key without using auto-increment. The auto-increment makes for a most elegant way of avoiding duplicate data in any table. If the rest of the data is identical with another row in that table it can be identified and fixed/deleted. Used in combination with another row which is a unique key (to catch any duplicates) the table becomes very robust.

ianm





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签