auto-increment

SQL: Creating a relation table with 2 different auto_increment

。_饼干妹妹 提交于 2019-11-26 14:40:56
I have 2 tables, both with their own auto incremented IDs, which are of course primary keys. When I want to create a 3rd table to establish the relation between these 2 tables, I always have an error. First one is about that you can have only 1 automatically-incremented column, the second one occurs when I delete the auto_increment statement from those 2, therefore sql doesn't allow me to make them foreign keys, because of the type matching failure. Is there a way that I can create a relational table without losing auto increment features? Another possible (but not preffered) solution may be

SQL server identity column values start at 0 instead of 1

喜夏-厌秋 提交于 2019-11-26 14:33:28
问题 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) 回答1: From DBCC

Prevent auto increment on MySQL duplicate insert

断了今生、忘了曾经 提交于 2019-11-26 14:24:16
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 way too much. For example I'll end up with a table with say 3 rows but bad id 's 1 | test 8 | testtext

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

China☆狼群 提交于 2019-11-26 14:09:38
问题 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

Too many auto increments with ON DUPLICATE KEY UPDATE

不羁岁月 提交于 2019-11-26 13:55:55
问题 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:/

PHP MYSQL - Insert into without using column names but with autoincrement field

最后都变了- 提交于 2019-11-26 13:48:33
问题 I need to insert a long row with 32 fields into a MySQL table. I'd like to do something like this: $sql="insert into tblname values (... 32 fields ...)"; Obviously it works fine if the fields are in the same order as the MySQL table fields. But, my table has an auto-increment id as it's first field. What I want is to fill in all table names but the first (id) one. Suggestions? 回答1: Just use NULL as your first value, the autoincrement field will still work as expected: INSERT INTO tblname

How to generate auto increment field in select query

笑着哭i 提交于 2019-11-26 12:55:41
问题 For example I have a table with 2 columns, first_name and last_name with these values Ali Khani Elizabette Amini Britney Spears ,... I want to write a select query that generate a table like this: 1 Ali Khani 2 Elizabette Amini 3 Britney Spears ,... Thanks for your help. 回答1: If it is MySql you can try SELECT @n := @n + 1 n, first_name, last_name FROM table1, (SELECT @n := 0) m ORDER BY first_name, last_name SQLFiddle And for SQLServer SELECT row_number() OVER (ORDER BY first_name, last_name)

What are the overheads of using AUTOINCREMENT for SQLite on Android?

陌路散爱 提交于 2019-11-26 12:34:00
问题 In the SQLite Documentation, it includes the following :- The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed. and The behavior implemented by the AUTOINCREMENT keyword is subtly different from the default behavior. With AUTOINCREMENT, rows with automatically selected ROWIDs are guaranteed to have ROWIDs that have never been used before by the same table in the same database. And the

make an ID in a mysql table auto_increment (after the fact)

半腔热情 提交于 2019-11-26 11:56:37
问题 I acquired a database from another developer. He didn\'t use auto_incrementers on any tables. They all have primary key ID\'s, but he did all the incrementing manually, in code. Can I turn those into Auto_incrementers now? Wow, very nice, thanks a ton. It worked without a hitch on one of my tables. But a second table, i\'m getting this error...Error on rename of \'.\\DBNAME#sql-6c8_62259c\' to \'.\\DBNAME\\dealer_master_events\' 回答1: For example, here's a table that has a primary key but is

auto_increment by group

你说的曾经没有我的故事 提交于 2019-11-26 11:34:39
问题 Is there a way with MySQL (5.0 specifically) to have an auto_increment field who\'s value is based on a grouping column? Example: id name group_field 1 test 1 2 test2 1 1 test3 2 2 test4 2 1 test5 3 2 test6 3 I\'d like to not have to go through any \'crazy\' methods to achive this, but will if necessary. 回答1: For MyISAM and BDB tables you can have an auto_increment field as a secondary part of key, e.g. CREATE TABLE foo ( id INT AUTO_INCREMENT NOT NULL, group_field INT NOT NULL, name VARCHAR