alter-table

mysql : loop over tables and alter table add index

☆樱花仙子☆ 提交于 2019-12-09 06:19:17
问题 I have ~1000 tables that start with the same prefix : table_prefix_{SOME_ID} (i can take the ids from another table) what is the fast way to loop over all the tables in mysql and do : ALTER TABLE `table_prefix_{some_id}` ADD INDEX `fields` (`field`) 回答1: Forget looping. Just do this: select concat( 'alter table ', a.table_name, ' add index `fields` (`field`);' ) from information_schema.tables a where a.table_name like 'table_prefix_%'; Then take the result set and run it as a SQL script. BTW,

Change huge table PK column data type

送分小仙女□ 提交于 2019-12-07 22:25:21
问题 Now that we've ran out of int capacity on a PK column (which is an IDENTITY ) I'd like to do this to bigint , but simple ALTER TABLE seems to be unable to handle that big of a table. So my question is: how do I change the type of a PK column with keeping actual values in place and do I need to alter referencing tables as well? 回答1: In addition to KLE's suggestion, the following queries might help: To disable all constraints on the tables that reference oldTable try to execute the output of

Quickly dropping and re-creating multiple indexes, views, statistics when altering a column

主宰稳场 提交于 2019-12-07 09:44:22
问题 I have a column "StoreNumber" in my "Project" table which I want to change to be "NOT NULL". I recently sanitized all of the old data so that there are no null entries. However, when I execute the following statement it fails due to multiple dependencies to various views, indexes, and statistics ALTER TABLE [Project] ALTER COLUMN StoreNumber VARCHAR(9) NOT NULL GO What is the fastest way to drop all of these views/indexes/statistics then run the alter statement, and then recreate all of the

what is the difference between 'alter table rename' and 'rename table'?

半腔热情 提交于 2019-12-07 07:13:28
问题 I am using MySQL. Here's an example, I want to rename a table A to B, so what's the difference between following statement: alter table A rename to B; and this one: rename table A to B; Can anyone give a detail compare between them? Is it vary based on different engine? 回答1: As documented under ALTER TABLE Syntax: For ALTER TABLE tbl_name RENAME TO new_tbl_name without any other options, MySQL simply renames any files that correspond to the table tbl_name without making a copy. (You can also

How to use ALTER TABLE to add a new column and make it unique?

蓝咒 提交于 2019-12-06 23:56:21
问题 How do I use ALTER TABLE to add a new column and make it unique? 回答1: Depends on the DBMS, but I think the following is quite portable: ALTER TABLE table_name ADD column_name datatype ALTER TABLE table_name ADD UNIQUE (column_name) If you want to give a name to the UNIQUE constraint, you could replace the last command with this: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name) 回答2: if table is empty ALTER TABLE ADD (FieldName Type) ALTER TABLE ADD CONSTRAINT UNIQUE

Change huge table PK column data type

穿精又带淫゛_ 提交于 2019-12-06 06:26:52
Now that we've ran out of int capacity on a PK column (which is an IDENTITY ) I'd like to do this to bigint , but simple ALTER TABLE seems to be unable to handle that big of a table. So my question is: how do I change the type of a PK column with keeping actual values in place and do I need to alter referencing tables as well? In addition to KLE's suggestion, the following queries might help: To disable all constraints on the tables that reference oldTable try to execute the output of the following query: SELECT 'ALTER TABLE ' + OBJECT_NAME(fk.parent_object_id) + ' NOCHECK CONSTRAINT ' + fk

Cannot cast type numeric to boolean

筅森魡賤 提交于 2019-12-06 02:58:41
问题 ALTER TABLE products ALTER COLUMN power_price DROP DEFAULT; ALTER TABLE products ALTER COLUMN power_price TYPE bool USING (power_price::boolean); ALTER TABLE products ALTER COLUMN power_price SET NOT NULL; ALTER TABLE products ALTER COLUMN power_price SET DEFAULT false; Postgres gives me this error: Query failed: ERROR: cannot cast type numeric to boolean 回答1: Use: ALTER TABLE products ALTER power_price TYPE bool USING (power_price::int::bool); There is no direct cast defined between numeric

Quickly dropping and re-creating multiple indexes, views, statistics when altering a column

萝らか妹 提交于 2019-12-05 18:20:43
I have a column "StoreNumber" in my "Project" table which I want to change to be "NOT NULL". I recently sanitized all of the old data so that there are no null entries. However, when I execute the following statement it fails due to multiple dependencies to various views, indexes, and statistics ALTER TABLE [Project] ALTER COLUMN StoreNumber VARCHAR(9) NOT NULL GO What is the fastest way to drop all of these views/indexes/statistics then run the alter statement, and then recreate all of the views/indexes/statistics again? I know that I could copy out all of the drop and create statements one

what is the difference between 'alter table rename' and 'rename table'?

笑着哭i 提交于 2019-12-05 13:23:07
I am using MySQL. Here's an example, I want to rename a table A to B, so what's the difference between following statement: alter table A rename to B; and this one: rename table A to B; Can anyone give a detail compare between them? Is it vary based on different engine? As documented under ALTER TABLE Syntax : For ALTER TABLE tbl_name RENAME TO new_tbl_name without any other options, MySQL simply renames any files that correspond to the table tbl_name without making a copy. (You can also use the RENAME TABLE statement to rename tables. See Section 13.1.32, “ RENAME TABLE Syntax” .) Any

Unable to drop constraint in SQL server 2005, “Could not drop constraint. See previous errors”

自作多情 提交于 2019-12-05 07:38:07
I'm trying to drop a constraint on a DB table, something like: ALTER TABLE MyTable drop CONSTRAINT FK_MyTable_AnotherTable But the execution just runs and runs. If I stop it I see: Msg 3727, Level 16, State 0, Line 2 Could not drop constraint. See previous errors. Web search throws up various pages but note that the constraint is properly named and I am trying to remove it using the correct name I was having the same issue on SQL Server 2008 R2 , I solved my problem with below line hopefully it will work for someone else as well :) Alter Table [Table Name] DROP Column [Column Name] Found a way