alter-table

Alter a MySQL column to be AUTO_INCREMENT

这一生的挚爱 提交于 2019-11-26 04:06:42
问题 I’m trying to modify a table to make its primary key column AUTO_INCREMENT after the fact. I have tried the following SQL, but got a syntax error notification. ALTER TABLE document ALTER COLUMN document_id AUTO_INCREMENT Am I doing something wrong or is this not possible? +--------------------+ | VERSION() | +--------------------+ | 5.0.75-0ubuntu10.2 | +--------------------+ 回答1: ALTER TABLE document MODIFY COLUMN document_id INT auto_increment 回答2: Roman is right, but note that the auto

SQL Server add auto increment primary key to existing table

送分小仙女□ 提交于 2019-11-26 03:03:36
问题 As the title, I have an existing table which is already populated with 150000 records. I have added an Id column (which is currently null). I\'m assuming I can run a query to fill this column with incremental numbers, and then set as primary key and turn on auto increment. Is this the correct way to proceed? And if so, how do I fill the initial numbers? 回答1: No - you have to do it the other way around: add it right from the get go as INT IDENTITY - it will be filled with identity values when

Altering a column: null to not null

荒凉一梦 提交于 2019-11-26 02:59:41
问题 I have a table that has several nullable integer columns. This is undesirable for several reasons, so I am looking to update all nulls to 0 and then set these columns to NOT NULL . Aside from changing nulls to 0 , data must be preserved. I am looking for the specific SQL syntax to alter a column (call it ColumnA ) to \" not null \". Assume the data has been updated to not contain nulls. Using SQL server 2000 . 回答1: First, make all current NULL values disappear: UPDATE [Table] SET [Column]=0

How do I rename a column in a SQLite database table?

喜你入骨 提交于 2019-11-26 00:35:14
问题 I would need to rename a few columns in some tables in a SQLite database. I know that a similar question has been asked on stackoverflow previously, but it was for SQL in general, and the case of SQLite was not mentioned. From the SQLite documentation for ALTER TABLE, I gather that it\'s not possible to do such a thing \"easily\" (i.e. a single ALTER TABLE statement). I was wondering someone knew of a generic SQL way of doing such a thing with SQLite. 回答1: This was just fixed with 2018-09-15

Adding an identity to an existing column

巧了我就是萌 提交于 2019-11-25 21:49:00
问题 I need to change the primary key of a table to an identity column, and there\'s already a number of rows in table. I\'ve got a script to clean up the IDs to ensure they\'re sequential starting at 1, works fine on my test database. What\'s the SQL command to alter the column to have an identity property? 回答1: You can't alter the existing columns for identity. You have 2 options, Create a new table with identity & drop the existing table Create a new column with identity & drop the existing