alter-table

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

こ雲淡風輕ζ 提交于 2019-12-05 03:43:11
How do I use ALTER TABLE to add a new column and make it unique? 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) if table is empty ALTER TABLE ADD (FieldName Type) ALTER TABLE ADD CONSTRAINT UNIQUE(FieldName) If you have data in table you need to this in three steps: Add column Fill values Add unique constraint

Create column and insert into it within the same transaction?

浪尽此生 提交于 2019-12-05 01:19:03
Is it possible to create a column and insert values to it during the same transaction? This is part of an upgrade script. I found the following method online , but it does not work; I get an error: Invalid column name 'IndexNumber'. . I'm assuming this is because the transaction hasn't created the column yet so there is nothing to insert to. The relevant parts of my script: Print 'Beginning Upgrade' Begin Transaction -- -------------------------------------------------------------------- USE [MyDatabase]; /* Widgets now can be ordered and the order can be modified */ ALTER TABLE [dbo].[Widgets

How to alter a MySQL table's foreign key using the command line

时光毁灭记忆、已成空白 提交于 2019-12-04 18:19:55
问题 How to alter an existing table in MySQL, setting foreign key to another table, using the command line? 回答1: You have to drop existing foreign key and create another one. For example like this: ALTER TABLE my_table DROP FOREIGN KEY my_key; ALTER TABLE my_table ADD CONSTRAINT my_key FOREIGN KEY ('some_id') REFERENCES some_new_table ('some_other_id') ON UPDATE CASCADE ON DELETE CASCADE; 回答2: Execute help alter table at mysql command prompt and the output is very much self explanatory. Look for

Add a column if it doesn't exist to all tables?

南楼画角 提交于 2019-12-04 09:08:24
问题 I'm using SQL Server 2005/2008. I need to add a column to a table if it does not yet exist. This will apply to all tables in a given database. I hoped I was close, but I'm having issues with this solution. How can this be done? Here's what I have: EXEC sp_MSforeachtable ' declare @tblname varchar(255); SET @tblname = PARSENAME("?",1); if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = @tblname and column_name = ''CreatedOn'') begin ALTER TABLE @tblname ADD

Cannot cast type numeric to boolean

一笑奈何 提交于 2019-12-04 08:50:19
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 Use: ALTER TABLE products ALTER power_price TYPE bool USING (power_price::int::bool); There is no direct cast defined between numeric and boolean . You can use integer as middle-ground. text would be another candidate for middle ground, since

How to insert values into existing SQL Server tables with foreign keys

假装没事ソ 提交于 2019-12-04 05:50:50
问题 I have a database and I want to insert new values to the table, the problem is that I have two tables with FK to each other. I know the problem my be something with the Alter Table , however I can't figure out what cause it. First table: Department CREATE TABLE [dbo].[Department] ( [DID] [int] primary key, [Name] [varchar](255) , [Description] [varchar](255) , [ManagerId] [int] ); Second table: OfficialEmployee CREATE TABLE [dbo].[OfficialEmployee] ( [EID] [int] primary key, [StartDate] [date

Rename a column in mysql table without having to repeat its type definition

我的梦境 提交于 2019-12-03 23:29:34
Is it possible to rename a column in MySQL without having to repeat its type definition? Please without having to hack into information_schema. The ALTER TABLE syntax does not seem to offer such possibility: ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name [alter_specification [, alter_specification] ...] [partition_options] ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name partition_options alter_specification: table_options [...] | CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name] | MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name] [...] More

How to alter “REFERENCES” in PostgreSQL?

为君一笑 提交于 2019-12-03 22:38:29
How can I alter the reference to a table in PostgreSQL when the table name has been changed? Say I have: CREATE TABLE example1 ( id serial NOT NULL PRIMARY KEY, name varchar(100) ); CREATE TABLE example2 ( id serial NOT NULL PRIMARY KEY, example1fk integer REFERENCES example1 (id) DEFERRABLE INITIALLY DEFERRED ); Later I do: ALTER TABLE example1 RENAME TO example3; How to change the definition of the foreign key constraint? example1fk integer REFERENCES example1 (id) DEFERRABLE INITIALLY DEFERRED, Internal dependencies between tables and / or other objects are never bound to the object name.

Add column to table and then update it inside transaction

爷,独闯天下 提交于 2019-12-03 17:55:36
问题 I am creating a script that will be run in a MS SQL server. This script will run multiple statements and needs to be transactional, if one of the statement fails the overall execution is stopped and any changes are rolled back. I am having trouble creating this transactional model when issuing ALTER TABLE statements to add columns to a table and then updating the newly added column. In order to access the newly added column right away, I use a GO command to execute the ALTER TABLE statement,

alter table drop column syntax error using sqlite

大城市里の小女人 提交于 2019-12-03 15:54:14
问题 This is the schema of my table: create table LPCG(ID integer primary key, PCG text, Desc text, test text); I wish to drop the column "test", and hence use the command: alter table LPCG drop column test; This is the error message I get: Error: near "drop": syntax error Can someone please help me correct my error? An additional question is: I understand that ID is the primary key attribute. Would I be able to drop that column? If not, is there a workaround which anyone has used? Thanks in