alter-table

Rename column only if exists

萝らか妹 提交于 2019-12-19 17:31:03
问题 PostgreSQL does not allow ALTER TABLE t RENAME COLUMN IF EXISTS c1 TO c2 ...or anything like that. However, it's very convenient to be able to write scripts which modify DB structure which can be run again without first checking if it has already been run. How do I write a PostgreSQL function to do exactly this? 回答1: Better to have two functions, one calling the other: CREATE OR REPLACE FUNCTION column_exists(ptable TEXT, pcolumn TEXT) RETURNS BOOLEAN AS $BODY$ DECLARE result bool; BEGIN --

Rename column only if exists

我与影子孤独终老i 提交于 2019-12-19 17:30:10
问题 PostgreSQL does not allow ALTER TABLE t RENAME COLUMN IF EXISTS c1 TO c2 ...or anything like that. However, it's very convenient to be able to write scripts which modify DB structure which can be run again without first checking if it has already been run. How do I write a PostgreSQL function to do exactly this? 回答1: Better to have two functions, one calling the other: CREATE OR REPLACE FUNCTION column_exists(ptable TEXT, pcolumn TEXT) RETURNS BOOLEAN AS $BODY$ DECLARE result bool; BEGIN --

MySQL dropping all indexes from table

落花浮王杯 提交于 2019-12-19 12:51:15
问题 I have a MySQL database that runs for some time now with many changes on it. Lately I looked over it and I noticed that in some cases I have doubled the index on the same field. Some Indexes are missing, and in general there is a huge mess in all the indexes. I wants to drop all indexes from a table. Later on I have a prepared script that will run ALTER TABLE and add the relevant indexes. Is there a way to drop all indexes from a table? 回答1: If you have phpmyadmin or any similar tool you can

Warning: A long semaphore wait

≯℡__Kan透↙ 提交于 2019-12-18 12:17:36
问题 For the past 4 days I have had massive problems with my nightly updates , except for 1 night were it all went fine in between these 4 days. During these updates i update a couple of fulltext indexes. I do it in this manner. Drop the fulltext index Update the fulltext table Add the fulltext index This has been working perfect for over 2 years . Usual update time was around 3-4 hours which was normal for the amount of data that is updated each night. But since Friday really the update times has

insert a NOT NULL column to an existing table

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 10:29:12
问题 I have tried: ALTER TABLE MY_TABLE ADD STAGE INT NOT NULL; But it gives this error message: ALTER TABLE only allows columns to be added that can contain nulls or have a DEFAULT definition specified 回答1: As an option you can initially create Null-able column, then update your table column with valid not null values and finally ALTER column to set NOT NULL constraint: ALTER TABLE MY_TABLE ADD STAGE INT NULL GO UPDATE MY_TABLE SET <a valid not null values for your column> GO ALTER TABLE MY_TABLE

Can I create a named default constraint in an add column statement in SQL Server?

不羁的心 提交于 2019-12-18 10:06:10
问题 In SQL Server, I have a new column on a table: ALTER TABLE t_tableName ADD newColumn NOT NULL This fails because I specify NOT NULL without specifying a default constraint. The table should not have a default constraint. To get around this, I could create the table with the default constraint and then remove it. However, there doesn't appear to be any way to specify that the default constraint should be named as part of this statement, so my only way to get rid of it is to have a stored

How to rename a table in SQL Server?

寵の児 提交于 2019-12-18 09:55:07
问题 The SQL query that I have used is : ALTER TABLE oldtable RENAME TO newtable; But, it gives me an error. Server: Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'TO'. 回答1: To rename a table in SQL Server, use the sp_rename command: exec sp_rename 'schema.old_table_name', 'new_table_name' 回答2: To rename a column: sp_rename 'table_name.old_column_name', 'new_column_name' , 'COLUMN'; To rename a table: sp_rename 'old_table_name','new_table_name'; 回答3: When using sp_rename

Alter a live table to make a key non-unique

若如初见. 提交于 2019-12-18 07:33:36
问题 I saw some other questions related to this, but they were not MySQL. The database is a live database, so I don't want to delete and recreate the table. I simply want to make a column no longer unique, which is less permissive in nature so it shouldn't cause any problems. 回答1: If your column was defined unique using UNIQUE clause, then use: ALTER TABLE mytable DROP INDEX constraint_name , or, if your constraint was implicitly named, ALTER TABLE mytable DROP INDEX column_name If it was defined

Why can't SQL Server alter a view in a stored procedure?

﹥>﹥吖頭↗ 提交于 2019-12-18 05:04:33
问题 I'm using MS SQL Server, and I'd like to alter a view from within a stored procedure, by executing something like "alter view VIEWNAME as ([some sql])". A few pages thrown up by google assert that this is not supported directly (and neither are related alter-table statements), but there are also examples of how to work around it using constructions like this: declare @sql varchar(max) select @sql = 'alter view VIEWNAME as ([some sql])' exec(@sql) Writing code as literal strings smells a bit,

Adding column between two other columns in SQL server

谁说胖子不能爱 提交于 2019-12-17 20:21:50
问题 Can you add a column to a table inserting it in between two existing columns in SQL Server without dropping and re-creating the table? 回答1: The simple answer is no. Is there a reason why column order is important to you? 回答2: Mediumly long answer, yes (ish) but it's ugly and you probably wouldn't want to do it. please note: this code creates a physical table CREATE TABLE MyTest (a int, b int, d int, e int) INSERT INTO MyTest (a,b,d,e) VALUES(1,2,4,5) SELECT * FROM MyTest ALTER TABLE MyTest