auto-increment

MySQL's AUTO_INCREMENT behavior in a multiple row insert

佐手、 提交于 2019-12-07 01:06:12
问题 I think the answer to my question is obvious but since I could not find any documentation to support it, I thought it's worth asking. At least for the record. As we all know AUTO_INCREMENT fields are incremented each time an INSERT statement is executed. And its value can be retrieved by LAST_INSERT_ID() function. It is also mentioned in MySQL's Manual that with multiple-row inserts, LAST_INSERT_ID() will return the first ID of the inserted rows. Which I think is a good idea (really useful).

How to enable auto-increment with Sql compact & C#

情到浓时终转凉″ 提交于 2019-12-06 15:06:04
问题 I'm using C# 2010 Express. I have created a SQL Server CE database file ( .sdf ) then created Information table and ID, Name, City columns with Database Explorer. I want to save names and city user will write to the form. The auto-increment option is disabled in Database explorer and Properties windows. When I'm saving something to that database i must change the ID manually, how can I make ID column auto-increment enabled? 回答1: There is a simple way for SQL CE: ALTER TABLE TableName ALTER

Create a computed column based on another column in MySQL

≡放荡痞女 提交于 2019-12-06 12:59:45
I have a 2 columns in my table: a varchar(8) and an int . I want to auto-increment the int column and when I do, I want to copy the value into the varchar(8) column , but pad it with 0's until it is 8 characters long, so for example, if the int column was incremented to 3 , the varchar(8) column would contain '00000003' . My two questions are, what happens when the varchar(8) column gets to '99999999' because I don't want to have duplicates? How would I do this in MySQL? If my values can be between 00000000 to 99999999 , how many values can i have before I run out? This is my alternative

SQL update records with incrementing value starting from 1 each time

一世执手 提交于 2019-12-06 12:06:57
I am adding batches of records to a table using a single insert statement. I want each new batch to be allocated incrementing numbers, but starting from 1 each time. So, if I have Batch Name IncementingValue 1 Joe 1 1 Pete 2 1 Andy 3 2 Sue 1 2 Mike 2 2 Steve 3 and I then add two records (using a single insert statement) : 3 Dave 3 Paul How can I run an update statement against this table so that Dave will be set to 1 and Paul to 2. I don't want to use a cursor. The ranking function ROW_NUMBER should do what you need. You didn't mention any specific rules about how the sequence number should be

Incrementing revision numbers in table's composite key

我们两清 提交于 2019-12-06 11:37:11
I'm running SQL Server 2014 locally for a database that will be deployed to an Azure SQL V12 database. I have a table that stores values of extensible properties for a business-entity object, in this case the three tables look like this: CREATE TABLE Widgets ( WidgetId bigint IDENTITY(1,1), ... ) CREATE TABLE WidgetProperties ( PropertyId int IDENTITY(1,1), Name nvarchar(50) Type int -- 0 = int, 1 = string, 2 = date, etc ) CREATE TABLE WidgetPropertyValues ( WidgetId bigint, PropertyId int, Revision int, DateTime datetimeoffset(7), Value varbinary(255) CONSTRAINT [PK_WidgetPropertyValues]

Column with alternate serials

瘦欲@ 提交于 2019-12-06 11:12:39
I would like to create a table of user_widgets which is primary keyed by a user_id and user_widget_id, where user_widget_id works like a serial, except for that it starts at 1 per each user. Is there a common or practical solution for this? I am using PostgreSQL, but an agnostic solution would be appreciated as well. Example table: user_widgets | user_id | user_widget_id | user_widget_name | +-----------+------------------+----------------------+ | 1 | 1 | Andy's first widget | +-----------+------------------+----------------------+ | 1 | 2 | Andy's second widget | +-----------+---------------

How to automatically create the primary key that has the following series A001000001 … A001xxxxxx in SQL?

随声附和 提交于 2019-12-06 10:53:14
问题 I want to create a primary key(auto-increment) which is start with A001000001 . Here A001 would be constant and 000001 would be iterate. I want rows like this: How can I do it using SQL query? 回答1: For SQL Server (you didn't clearly specify which RDBMS you're using), my suggestion would be: add a INT IDENTITY column to your table - and quite frankly, I would make that column the primary key add a computed column to your table that does this "prefixing" Something like: CREATE TABLE dbo

Auto increment with XQuery Update?

久未见 提交于 2019-12-06 10:03:56
Does XQuery Update support auto increment attributes, just like auto increment fields in SQL ? I'm using BaseX as my database. Arjan Given an answer from Christian Grün on the BaseX mailing list , this is doable when the node one is adding is defined in the XQuery Update statement, and hence can be enhanced using an {enclosed expression} before inserting it: You might specify the attribute counter within your XML file/database and increment it every time when you insert an element. A simple example: input.xml: <root count="0"/> insert.xq: let $root := doc('input.xml')/root let $count := $root/

CREATE TABLE new_table_name LIKE old_table_name with old_table_name's AUTO_INCREMENT values

最后都变了- 提交于 2019-12-06 06:57:50
Can I create a new table with an old table's autoincriment status in mysql client? I think, that ALTER TABLE new_table_name AUTO_INCREMENT=@my_autoincr_iment helps me, but this construction must use with a constant value. I don't want to use a difficult script. mysql> create table new_table like old_table; mysql> select @my_auto_increment:=auto_increment from information_schema.tables where table_name='old_table'; mysql> set @query = CONCAT("alter table new_table auto_increment = ", @my_auto_increment); mysql> prepare stmt from @query; mysql> execute stmt; mysql> deallocate prepare stmt; Thx

How do I update a table to add a primary key and update all of the existing rows with incremented IDs?

与世无争的帅哥 提交于 2019-12-06 05:17:42
问题 I have a table with 20,000 rows of data that I imported but I forgot to put a primary key on it so that each row has a unique key. I want the first row to start at ID 1 and increment all the way up to the last row and finish at ID 20000. How do I update all of the rows with a single query? I'm using MySQL. Have tried using PhpMyAdmin but it wouldn't do it. 回答1: After adding a new ID column (don't set as a primary key just yet, and don't turn on auto increment) run: SET @index = 1; UPDATE