you can always query it back using the values submitted for certain selected columns
every table which uses an auto_increment column as the primary key will also have one or more columns that make up what’s known as a “candidate key” which could hvae been used as the primary key except the auto_increment was used instead
so let’s say a table looks like this –
CREATE
TABLE mytable
( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT
, foo VARCHAR(9) NOT NULL
, bar VARCHAR(37) NOT NULL
, date_added DATE NOT NULL
, added_by_user INTEGER NOT NULL REFERENCES users(id)
, date_updated DATE NULL
, updated_by_user INTEGER NULL REFERENCES users(id)
)
a typical insert would look like this –
INSERT
INTO mytable
( foo
, bar
, date_added
, added_by_user )
VALUES
( 'happy'
, 'as a pig in shit'
, CURRENT_DATE
, 937 )
once this row has been inserted, it gets a new auto_increment value for the id
column, right?
well, you can retrieve that id value like this –
SELECT id
FROM mytable
WHERE foo = 'happy'
AND bar = 'as a pig in shit'
AND added_by_user = 937
the reason you can do this is because presumably the combination of foo
, bar
, and added_by_user
are unique
note: candidate keys should always be declared as such with a UNIQUE constraint
ALTER
TABLE mytable
ADD UNIQUE ( foo, bar, added_by_user )