auto-increment

How does MySQL Auto Increment work?

久未见 提交于 2019-11-27 09:16:05
I was just creating a new table using MySQL Query Browser, and noticed there's a tick under Auto Increment Column. How does that work? When adding to the database programatically, do I just add a number, and then the database automatically increments that number? Everytime a NEW user registers on my site, I want their Customer ID (integer only) to auto increment, so I don't have to try and randomly generate a unique number. Can this be done simply? Thank you! When adding to the database programatically, do I just add a number, and then the database automatically increments that number? Yes,

SQL server identity column values start at 0 instead of 1

不打扰是莪最后的温柔 提交于 2019-11-27 09:11:10
I've got a strange situation with some tables in my database starting its IDs from 0, even though TABLE CREATE has IDENTITY(1,1). This is so for some tables, but not for others. It has worked until today. I've tried resetting identity column: DBCC CHECKIDENT (SyncSession, reseed, 0); But new records start with 0. I have tried doing this for all tables, but some still start from 0 and some from 1. Any pointers? (i'm using SQL Server Express 2005 with Advanced Services) From DBCC CHECKIDENT DBCC CHECKIDENT ( table_name, RESEED, new_reseed_value ) If no rows have been inserted to the table since

ERROR: permission denied for sequence cities_id_seq using Postgres

若如初见. 提交于 2019-11-27 09:06:47
问题 I'm new at postgres (and at database info systems all in all). I ran following sql script on my database: create table cities ( id serial primary key, name text not null ); create table reports ( id serial primary key, cityid integer not null references cities(id), reportdate date not null, reporttext text not null ); create user www with password 'www'; grant select on cities to www; grant insert on cities to www; grant delete on cities to www; grant select on reports to www; grant insert on

Access Auto-Increment Value During INSERT INTO Statement

半城伤御伤魂 提交于 2019-11-27 08:38:36
问题 I am currently using MySQL. I have a table that has an auto_increment 'id' field, and an 'imgname' field containing a string that is the file name of an image. I need to generate the 'imgname' value using the auto_increment value that is create by an INSERT INTO statement. The problem is, I don't know this value until I can use mysql_insert_id, AFTER the insert query has run. I would like to know if it's possible to access this value DURING the insert query somehow and then use it to generate

SQLite auto-increment non-primary key field

自作多情 提交于 2019-11-27 08:37:32
Is it possible to have a non-primary key to be auto-incremented with every insertion? For example, I want to have a log, where every log entry has a primary key (for internal use), and a revision number ( a INT value that I want to be auto-incremented). As a workaround, this could be done with a sequence, yet I believe that sequences are not supported in SQLite. Icarus You can do select max(id)+1 when you do the insertion. For example: INSERT INTO Log (id, rev_no, description) VALUES ((SELECT MAX(id) + 1 FROM log), 'rev_Id', 'some description') Note that this will fail on an empty table since

SQL Server Unique Composite Key of Two Field With Second Field Auto-Increment

我是研究僧i 提交于 2019-11-27 08:31:09
I have the following problem, I want to have Composite Primary Key like: PRIMARY KEY (`base`, `id`); for which when I insert a base the id to be auto-incremented based on the previous id for the same base Example: base id A 1 A 2 B 1 C 1 Is there a way when I say: INSERT INTO table(base) VALUES ('A') to insert a new record with id 3 because that is the next id for base 'A'? The resulting table should be: base id A 1 A 2 B 1 C 1 A 3 Is it possible to do it on the DB exactly since if done programmatically it could cause racing conditions. EDIT The base currently represents a company, the id

how to return last inserted (auto incremented) row id in HSQL?

≯℡__Kan透↙ 提交于 2019-11-27 07:55:41
问题 i am working with HSQL database for testing purpose. i want standalone db file. but now i am in trouble to get last inserted row id (auto-incremental - identity) in HSQL. how can i get id?? 回答1: It's pretty hard to write a query to perform this when you haven't given your table schema, but something like the following: SELECT TOP 1 Id FROM [TABLENAME] ORDER BY Id DESC 回答2: http://www.hsqldb.org/doc/guide/ch09.html the last inserted value into an identity column for a connection is available

Too many auto increments with ON DUPLICATE KEY UPDATE

我的未来我决定 提交于 2019-11-27 07:52:19
I have a basic table with columns: id (primary with AI) name (unique) etc If the unique column doesn't exist, INSERT the row, otherwise UPDATE the row.... INSERT INTO pages (name, etc) VALUES 'bob', 'randomness' ON DUPLICATE KEY UPDATE name = VALUES(name), etc = VALUES(etc) The problem is that if it performs an UPDATE, the auto_increment value on the id column goes up. So if a whole bunch of UPDATES are performed, the id auto_increment goes through the roof. Apparently it was a bug: http://bugs.mysql.com/bug.php?id=28781 ...but I'm using InnoDB on mySQL 5.5.8 on shared hosting. Other people

Is there any harm in resetting the auto-increment?

本小妞迷上赌 提交于 2019-11-27 07:47:34
问题 I have a 100 million rows, and it's getting too big. I see a lot of gaps. (since I delete, add, delete, add.) I want to fill these gaps with auto-increment. If I do reset it..is there any harM? If I do this, will it fill the gaps?: mysql> ALTER TABLE tbl AUTO_INCREMENT = 1; 回答1: Potentially very dangerous, because you can get a number again that is already in use. What you propose is resetting the sequence to 1 again. It will just produce 1,2,3,4,5,6,7,.. and so on, regardless of these

Is Access's AutoNumber (Increment) guaranteed to increment?

北战南征 提交于 2019-11-27 07:24:26
问题 For a particular table, I have my ID field set to AutoNumber (Increment). If I add 5 rows to this table in quick succession, is each guaranteed to have a larger ID than the last? For instance, does autonumbering ever restart from 1 if some earlier values have been deleted? 回答1: The only time I have ever had trouble with Access autonumbers is when, in error, I set the value of an autonumber key field to a number lower than the current maximum using an append query. Gaps had been created in the