auto-increment

How to create id with AUTO_INCREMENT on Oracle?

*爱你&永不变心* 提交于 2019-11-26 01:18:05
问题 It appears that there is no concept of AUTO_INCREMENT in Oracle, up until and including version 11g. How can I create a column that behaves like auto increment in Oracle 11g? 回答1: There is no such thing as "auto_increment" or "identity" columns in Oracle as of Oracle 11g . However, you can model it easily with a sequence and a trigger: Table definition: CREATE TABLE departments ( ID NUMBER(10) NOT NULL, DESCRIPTION VARCHAR2(50) NOT NULL); ALTER TABLE departments ADD ( CONSTRAINT dept_pk

Get the new record primary key ID from mysql insert query?

孤街浪徒 提交于 2019-11-26 00:58:19
问题 Ok, so lets say I am doing a mysql INSERT into one of my tables and the table has the column item_id which is set to autoincrement and primary key . How do I get the query to output the value of the newly generated primary key item_id in the same query? Currently I am running a second query to retrieve the id but this hardly seems like good practice considering this might produce the wrong result... If this is not possible then what is the best practice to ensure I retrieve the correct id?

Reset AutoIncrement in SQL Server after Delete

和自甴很熟 提交于 2019-11-26 00:49:53
问题 I\'ve deleted some records from a table in a SQL Server database. Now the ID\'s go from 101 to 1200. I want to delete the records again, but I want the ID\'s to go back to 102. Is there a way to do this in SQL Server? 回答1: Issue the following command to reseed mytable to start at 1: DBCC CHECKIDENT (mytable, RESEED, 0) Read about it in the Books on Line (BOL, SQL help). Also be careful that you don't have records higher than the seed you are setting. 回答2: DBCC CHECKIDENT('databasename.dbo

Get current AUTO_INCREMENT value for any table

梦想的初衷 提交于 2019-11-26 00:39:51
问题 How do I get the current AUTO_INCREMENT value for a table in MySQL? 回答1: You can get all of the table data by using this query: SHOW TABLE STATUS FROM `DatabaseName` WHERE `name` LIKE 'TableName' ; You can get exactly this information by using this query: SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'DatabaseName' AND TABLE_NAME = 'TableName'; 回答2: I believe you're looking for MySQL's LAST_INSERT_ID() function. If in the command line, simply run the following:

Get the new record primary key ID from mysql insert query?

白昼怎懂夜的黑 提交于 2019-11-26 00:22:22
Ok, so lets say I am doing a mysql INSERT into one of my tables and the table has the column item_id which is set to autoincrement and primary key . How do I get the query to output the value of the newly generated primary key item_id in the same query? Currently I am running a second query to retrieve the id but this hardly seems like good practice considering this might produce the wrong result... If this is not possible then what is the best practice to ensure I retrieve the correct id? You need to use the LAST_INSERT_ID() function: http://dev.mysql.com/doc/refman/5.0/en/information

SQL Server, How to set auto increment after creating a table without data loss?

▼魔方 西西 提交于 2019-11-26 00:12:52
问题 I have a table table1 in SQL server 2008 and it has records in it. I want the primary key table1_Sno column to be an auto-incrementing column. Can this be done without any data transfer or cloning of table? I know that I can use ALTER TABLE to add an auto-increment column, but can I simply add the AUTO_INCREMENT option to an existing column that is the primary key? 回答1: Changing the IDENTITY property is really a metadata only change. But to update the metadata directly requires starting the

Auto increment table column

痞子三分冷 提交于 2019-11-25 23:59:51
问题 Using Postgres, I\'m trying to use AUTO_INCREMENT to number my primary key automatically in SQL. However, it gives me an error. CREATE TABLE Staff ( ID INTEGER NOT NULL AUTO_INCREMENT, Name VARCHAR(40) NOT NULL, PRIMARY KEY (ID) ); The error: ********** Error ********** ERROR: syntax error at or near \"AUTO_INCREMENT\" SQL state: 42601 Character: 63 Any idea why? 回答1: Postgres 10 or later serial columns (see below) remain unchanged. But consider an IDENTITY column. Postgres 10 implements this

How to set initial value and auto increment in MySQL?

▼魔方 西西 提交于 2019-11-25 23:36:30
问题 How do I set the initial value for an \"id\" column in a MySQL table that start from 1001? I want to do an insert \"INSERT INTO users (name, email) VALUES (\'{$name}\', \'{$email}\')\"; Without specifying the initial value for the id column. 回答1: Use this: ALTER TABLE users AUTO_INCREMENT=1001; or if you haven't already added an id column, also add it ALTER TABLE users ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT, ADD INDEX (id); 回答2: MySQL - Setup an auto-incrementing primary key that starts

Auto Increment after delete in MySQL

岁酱吖の 提交于 2019-11-25 23:31:04
问题 I have a MySQL table with a primary key field that has AUTO_INCREMENT on. After reading other posts on here I\'ve noticed people with the same problem and with varied answers. Some recommend not using this feature, others state it can\'t be \'fixed\'. I have: table: course fields: courseID, courseName Example: number of records in the table: 18. If I delete records 16, 17 and 18 - I would expect the next record entered to have the courseID of 16, however it will be 19 because the last entered

How to make MySQL table primary key auto increment with some prefix

最后都变了- 提交于 2019-11-25 22:41:37
问题 I have table like this table id Varchar(45) NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(30) NOT NULL, I want to increment my id field like \'LHPL001\',\'LHPL002\',\'LHPL003\' ... etc. What should I have to do for that? Please let me know any possible way. 回答1: If you really need this you can achieve your goal with help of separate table for sequencing (if you don't mind) and a trigger. Tables CREATE TABLE table1_seq ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ); CREATE TABLE table1 ( id