auto-increment

In MySQL, how can I use the autoincrement value in another column at the time of the INSERT?

社会主义新天地 提交于 2020-01-15 14:27:50
问题 I am looking to have the automagically set autoincrement included in another column during the insert. For example in a table where ID is the autoincrement and Name is the other column, I'd like to do something like `INSERT INTO Names (Name) VALUES (CONCAT("I am number ",ID));` Currently, I do the INSERT without Name, then I have to immediately after do an UPDATE using $mysqli->insert_id. I don't want to query the table in advance because, as small a time as it may be, another record could

In MySQL, how can I use the autoincrement value in another column at the time of the INSERT?

ⅰ亾dé卋堺 提交于 2020-01-15 14:27:09
问题 I am looking to have the automagically set autoincrement included in another column during the insert. For example in a table where ID is the autoincrement and Name is the other column, I'd like to do something like `INSERT INTO Names (Name) VALUES (CONCAT("I am number ",ID));` Currently, I do the INSERT without Name, then I have to immediately after do an UPDATE using $mysqli->insert_id. I don't want to query the table in advance because, as small a time as it may be, another record could

MySQL AUTO_INCREMENT by group using InnoDB or alternatives

こ雲淡風輕ζ 提交于 2020-01-15 09:01:09
问题 I am designing a web application using a MySQL database, and I am stuck on one of the fine points of my database design. Here's an example of the type of data I would like to store using the InnoDB engine: user_id account_id 1 1 1 2 1 3 2 1 2 2 3 1 As is clear from the above, I would like the account_id to auto increment for each individual user. Here are some problems I have encountered: I do know that to do this automatically I could use the MyISAM engine, but after I have done some reading

Make Postgres choose the next minimal available id

这一生的挚爱 提交于 2020-01-13 10:25:31
问题 I would like to make Postgres choose the first next available id so that no error occurs in the following case: CREATE TABLE test( id serial PRIMARY KEY, name varchar ); Then: INSERT INTO test VALUES (2,'dd'); INSERT INTO test (name) VALUES ('aa'); INSERT INTO test (name) VALUES ('bb'); This will give a constraint error since id is primary. How can I tell Postgres to insert the record with the next free id? 回答1: Generally it's best to never overrule the default in a serial column. If you

SQL Server: insert next available int

…衆ロ難τιáo~ 提交于 2020-01-12 22:28:09
问题 I'm dealing with a table in SQL Server that has a serial_no column, which is defined as a non null int. It doesn't appear to be an auto incrementing field, as if I leave that column out of my insert statement I get an error saying that the serial_no field cannot be null. So how do I insert the next available number? I tried this: INSERT INTO mytable (serial_no) VALUES ( (SELECT MAX(serial_no)+1 FROM mytable)) but I get an error saying that subqueries cannot be used in this context. EDIT: This

How to insert new row to database with AUTO_INCREMENT column without specifying column names?

我的未来我决定 提交于 2020-01-12 04:01:29
问题 I have a table with the following columns: id - INT UNSIGNED AUTO_INCREMENT name - VARCHAR(20) group - VARCHAR(20) I know that I can add a row like this: INSERT INTO table_name (name, group) VALUES ('my name', 'my group') I wonder if there is a way to add a row without specifying the column names , like when there is no AUTO_INCREMENT column ? 回答1: For some databases, you can just explicitly insert a NULL into the auto_increment column: INSERT INTO table_name VALUES (NULL, 'my name', 'my

What is the limit of auto_increment (integer) in mysql

匆匆过客 提交于 2020-01-11 18:53:02
问题 I have a mysql database in which i am using auto_increment(integer), can you tell me till what integer it can incremented. How we can increase the limit of auto_increment? 回答1: The limit of an auto_increment column is the size of the column: Use a large enough integer data type for the AUTO_INCREMENT column to hold the maximum sequence value you will need. When the column reaches the upper limit of the data type, the next attempt to generate a sequence number fails. For example, if you use

What is the limit of auto_increment (integer) in mysql

∥☆過路亽.° 提交于 2020-01-11 18:51:33
问题 I have a mysql database in which i am using auto_increment(integer), can you tell me till what integer it can incremented. How we can increase the limit of auto_increment? 回答1: The limit of an auto_increment column is the size of the column: Use a large enough integer data type for the AUTO_INCREMENT column to hold the maximum sequence value you will need. When the column reaches the upper limit of the data type, the next attempt to generate a sequence number fails. For example, if you use

How to import text file to table with primary key as auto-increment

给你一囗甜甜゛ 提交于 2020-01-09 19:48:38
问题 I have some bulk data in a text file that I need to import into a MySQL table. The table consists of two fields .. ID (integer with auto-increment) Name (varchar) The text file is a large collection of names with one name per line ... (example) John Doe Alex Smith Bob Denver I know how to import a text file via phpMyAdmin however, as far as I understand, I need to import data that has the same number of fields as the target table. Is there a way to import the data from my text file into one

Prevent auto increment on MySQL duplicate insert

偶尔善良 提交于 2020-01-08 12:23:02
问题 Using MySQL 5.1.49, I'm trying to implement a tagging system the problem I have is with a table with two columns: id(autoincrement) , tag(unique varchar) (InnoDB) When using query, INSERT IGNORE INTO tablename SET tag="whatever" , the auto increment id value increases even if the insert was ignored. Normally this wouldn't be a problem, but I expect a lot of possible attempts to insert duplicates for this particular table which means that my next value for id field of a new row will be jumping