alter-table

Problem adding Foreign Key using Alter Table with existing MYSQL Database - can't add it! Help!

廉价感情. 提交于 2019-11-28 11:14:27
I have a production database where I have renamed several column's that are foreign keys. Obviously mysql makes this a real pain to do in my experience. My solution was to drop all the indexes and foreign keys, rename the id columns, and then re-add the indexes and foreign keys. This works great on mysql 5.1 on windows for the development database. I went to run my migration script on my debian server, which is also using mysql 5.1, and it gives the following error: mysql> ALTER TABLE `company_to_module` -> ADD CONSTRAINT `FK82977604FE40A062` FOREIGN KEY (`company_id`) REFERENCES `company` (

How do I Alter Table Column datatype on more than 1 column?

邮差的信 提交于 2019-11-28 06:08:41
For example: ALTER TABLE webstore.Store MODIFY COLUMN ( ShortName VARCHAR(100), UrlShort VARCHAR(100) ); The above however does not work. I am using MySql 5.x ALTER TABLE can do multiple table alterations in one statement, but MODIFY COLUMN can only work on one column at a time, so you need to specify MODIFY COLUMN for each column you want to change: ALTER TABLE webstore.Store MODIFY COLUMN ShortName VARCHAR(100), MODIFY COLUMN UrlShort VARCHAR(100); Also, note this warning from the manual: When you use CHANGE or MODIFY, column_definition must include the data type and all attributes that

How do I alter the position of a column in a PostgreSQL database table?

此生再无相见时 提交于 2019-11-28 04:48:07
I've tried the following, but I was unsuccessful: ALTER TABLE person ALTER COLUMN dob POSITION 37; " Alter column position " in the PostgreSQL Wiki says: PostgreSQL currently defines column order based on the attnum column of the pg_attribute table. The only way to change column order is either by recreating the table, or by adding columns and rotating data until you reach the desired layout. That's pretty weak, but in their defense, in standard SQL, there is no solution for repositioning a column either. Database brands that support changing the ordinal position of a column are defining an

How do I add more members to my ENUM-type column in MySQL?

自作多情 提交于 2019-11-28 03:13:22
The MySQL reference manual does not provide a clearcut example on how to do this. I have an ENUM-type column of country names that I need to add more countries to. What is the correct MySQL syntax to achieve this? Here's my attempt: ALTER TABLE carmake CHANGE country country ENUM('Sweden','Malaysia'); The error I get is: ERROR 1265 (01000): Data truncated for column 'country' at row 1. The country column is the ENUM-type column in the above-statement. SHOW CREATE TABLE OUTPUT: mysql> SHOW CREATE TABLE carmake; +---------+---------------------------------------------------------------------+ |

How can I add a column to a Postgresql database that doesn't allow nulls?

穿精又带淫゛_ 提交于 2019-11-28 02:59:41
I'm adding a new, "NOT NULL" column to my Postgresql database using the following query (sanitized for the Internet): ALTER TABLE mytable ADD COLUMN mycolumn character varying(50) NOT NULL; Each time I run this query, I receive the following error message: ERROR: column "mycolumn" contains null values I'm stumped. Where am I going wrong? NOTE: I'm using pgAdmin III (1.8.4) primarily, but I received the same error when I ran the SQL from within Terminal. You have to set a default value. ALTER TABLE mytable ADD COLUMN mycolumn character varying(50) NOT NULL DEFAULT 'foo'; ... some work (set real

Alter table after keyword in Oracle

陌路散爱 提交于 2019-11-28 02:59:20
问题 ALTER TABLE testTable ADD column1 NUMBER(1) DEFAULT 0 NOT NULL AFTER column2; Why can't I use mySql syntax in Oracle too? The above command works in MySql. Can you give me an equivalent that works? Error report: SQL Error: ORA-01735: invalid ALTER TABLE option 01735. 00000 - "invalid ALTER TABLE option" I am asking if there is any way to use after clause in Oracle command that I provided? 回答1: Because SQL is a relational algebra. It doesn't care one bit about "where" columns are located

ALTER TABLE my_table ADD @column INT

半城伤御伤魂 提交于 2019-11-28 00:58:22
If i want to use a variable as name of the new column, is this posible in MS SQL? Example that dont work: ALTER TABLE my_table ADD @column INT This worked great for me: EXEC ('ALTER TABLE my_table ADD ' + @column + ' INT') This is possible using dynamic sql to build your DDL and using the EXEC command to execute the string. Declare @SQL VarChar(1000) SELECT @SQL = 'ALTER TABLE my_table ADD ' + @column + ' INT' Exec (@SQL) See this article. I will also add that the moment you venture to the land of dynamic sql, you need to take care to not expose yourself to SQL Injection attacks . Always clean

Add not null DateTime column to SQLite without default value?

允我心安 提交于 2019-11-28 00:51:07
问题 It looks like i cant add a not null constraint or remove a default constraint. I would like to add a datetime column to a table and have all the values set to anything (perhaps 1970 or year 2000) but it seems like i cant use not null without a default and i cant remove a default once added in. So how can i add this column? (once again just a plain datetime not null) 回答1: Instead of using ALTER TABLE ADD COLUMN , create a new table that has the extra column, and copy your old data. This will

MySQL: How to add a column if it doesn't already exist?

一曲冷凌霜 提交于 2019-11-27 22:16:36
I want to add a column to a table, but I don't want it to fail if it has already been added to the table. How can I achieve this? # Add column fails if it already exists ALTER TABLE `TableName` ADD `ColumnName` int(1) NOT NULL default '0'; Use the following in a stored procedure: IF NOT EXISTS( SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'tablename' AND table_schema = 'db_name' AND column_name = 'columnname') THEN ALTER TABLE `TableName` ADD `ColumnName` int(1) NOT NULL default '0'; END IF; Reference: The INFORMATION_SCHEMA COLUMNS Table 来源: https://stackoverflow.com

SQL Server - script to update database columns from varchar to nvarchar if not already nvarchar

安稳与你 提交于 2019-11-27 18:09:33
问题 I am in a situation where I must update an existing database structure from varchar to nvarchar using a script. Since this script is run everytime a configuration application is run, I would rather determine if a column has already been changed to nvarchar and not perform an alter on the table. The databases which I must support are SQL Server 2000, 2005 and 2008. 回答1: The following query should get you what you need: IF EXISTS (SELECT * FROM sysobjects syo JOIN syscolumns syc ON syc.id = syo