alter-table

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

 ̄綄美尐妖づ 提交于 2019-12-17 19:38:49
问题 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

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

血红的双手。 提交于 2019-12-17 17:59:55
问题 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 回答1: 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

ALTER TABLE my_table ADD @column INT

别来无恙 提交于 2019-12-17 16:44:33
问题 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') 回答1: 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

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

五迷三道 提交于 2019-12-17 16:17:09
问题 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'; 回答1: 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

Error when setting n_distinct using a plpgsql variable

懵懂的女人 提交于 2019-12-17 14:54:54
问题 I tried to use a function to set the n_distinct value for a table. The code is as follows: create temporary table _temp ( id integer ); create function pg_temp.setdistinct(_cnt real) returns void as $$ begin alter table _temp alter column id set (n_distinct=_cnt); end $$ language plpgsql; select pg_temp.setdistinct(1000); Yet receive the following errors: ERROR: invalid value for floating point option "n_distinct": _cnt CONTEXT: SQL statement "alter table _temp alter column id set (n_distinct

SQL SET DEFAULT not working in MS Access

核能气质少年 提交于 2019-12-17 14:05:53
问题 Possible Duplicate: DEFAULT clause in ALTER TABLE statement resulting in syntax error I am trying to execute the following statement using a SQL query within MS Access; ALTER TABLE [table] ALTER COLUMN [column] SET DEFAULT 'default value' However, I get a dialog displaying the error Syntax error in ALTER TABLE statement. And when I click OK it highlights the word DEFAULT . I also tried the following statement; ALTER TABLE [table] ADD CONSTRAINT [Default] DEFAULT 'default value' FOR [column]

SQL SET DEFAULT not working in MS Access

╄→гoц情女王★ 提交于 2019-12-17 14:04:25
问题 Possible Duplicate: DEFAULT clause in ALTER TABLE statement resulting in syntax error I am trying to execute the following statement using a SQL query within MS Access; ALTER TABLE [table] ALTER COLUMN [column] SET DEFAULT 'default value' However, I get a dialog displaying the error Syntax error in ALTER TABLE statement. And when I click OK it highlights the word DEFAULT . I also tried the following statement; ALTER TABLE [table] ADD CONSTRAINT [Default] DEFAULT 'default value' FOR [column]

Default sort-ordering in MySQL (ALTER TABLE … ORDER BY …;)

拜拜、爱过 提交于 2019-12-17 09:47:24
问题 Assume that the default ordering of a MySQL-table (ISAM) is changed by executing: ALTER TABLE tablename ORDER BY columnname ASC; From now on, am I guaranteed to obtain records retrieved from the table in the order of "columnname ASC" assuming no "ORDER BY" is specified in my queries (i.e. "SELECT * FROM tablename WHERE ... LIMIT 10;")? Are there any corner-cases that I should be aware of? Update #1: Thanks a lot to Quassnoi who correctly pointed out that INSERTs and DELETEs messes up the

Alter MySQL table to add comments on columns

旧街凉风 提交于 2019-12-17 08:12:46
问题 I have been checking the MySQL Documentation for ALTER TABLE and it does not seem to include a way to add or modify a comment to a column. How can I do this? -- for table ALTER TABLE myTable COMMENT 'Hello World' -- for columns -- ??? 回答1: try: ALTER TABLE `user` CHANGE `id` `id` INT( 11 ) COMMENT 'id of user' 回答2: You can use MODIFY COLUMN to do this. Just do... ALTER TABLE YourTable MODIFY COLUMN your_column your_previous_column_definition COMMENT "Your new comment" substituting: YourTable

ALTER TABLE without locking the table?

落爺英雄遲暮 提交于 2019-12-17 08:04:16
问题 When doing an ALTER TABLE statement in MySQL, the whole table is read-locked for the duration of the statement. If it's a big table, that means insert or update statements could be locked for a looooong time. Is there a way to do a "hot alter", like adding a column in such a way that the table is still updatable throughout the process? Mostly I'm interested in a solution for MySQL but I'd be interested in other RDBMS if MySQL can't do it. To clarify, my purpose is simply to avoid downtime