alter-table

How to convert a table column to another data type

ぃ、小莉子 提交于 2019-12-03 14:25:45
I have a column with the type of Varchar in my Postgres database which I meant to be integers... and now I want to change them, unfortunately this doesn't seem to work using my rails migration. change_column :table1, :columnB, :integer Which seems to output this SQL: ALTER TABLE table1 ALTER COLUMN columnB TYPE integer So I tried doing this: execute 'ALTER TABLE table1 ALTER COLUMN columnB TYPE integer USING CAST(columnB AS INTEGER)' but cast doesn't work in this instance because some of the column are null... any ideas? Error: PGError: ERROR: invalid input syntax for integer: "" : ALTER TABLE

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

早过忘川 提交于 2019-12-03 12:09:31
How to alter an existing table in MySQL, setting foreign key to another table, using the command line? 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; Execute help alter table at mysql command prompt and the output is very much self explanatory. Look for add constraint with foreign key clause and apply it on your table. mysql> help alter table Name: 'ALTER TABLE'

Alter table to modify default value of column

心已入冬 提交于 2019-12-03 08:22:13
问题 I have a requirement where we need to modify a column's default value in database table. The table is already an existing table in database and currently the default value of the column is NULL. Now if add a new default value to this column, If I am correct it updates all the existing NULLs of the column to new DEfault value. Is there a way to not to do this but still set a new default value on column. I mean I do not want the existing NULLs to be updated and want them to remain as NULLs. Any

Adding column to table & adding data right away to column in PostgreSQL

人走茶凉 提交于 2019-12-03 08:04:00
I am trying to create a new column in an existing table, and using a select statement to do division to create the data I want to insert into my new column. Several of the statements I have written out will work as separate queries but I have not been able to string together the statements into one single query. I am still learning SQL and using it with mySQL and PostgreSQL. I took a class last month on SQL and now I am trying to do my own projects to keep my skills sharp. I am doing some work with elections results from 2012 in MN to use in my tables, to understand data I am working with. I

Change a Nullable column to NOT NULL with Default Value

江枫思渺然 提交于 2019-12-03 06:27:43
问题 I came across an old table today with a datetime column called 'Created' which allows nulls. Now, I'd want to change this so that it is NOT NULL, and also include a constraint to add in a default value (getdate()). So far I've got the following script, which works fine provided that i've cleaned up all the nulls beforehand: ALTER TABLE dbo.MyTable ALTER COLUMN Created DATETIME NOT NULL Is there any way to also specify the default value as well on the ALTER statement? 回答1: I think you will

Adding a column as a foreign key gives ERROR column referenced in foreign key constraint does not exist

半世苍凉 提交于 2019-12-03 05:23:53
问题 I have the following set up, CREATE TABLE auth_user ( id int PRIMARY KEY ); CREATE TABLE links_chatpicmessage (); I'm trying to add a column named sender to links_chatpicmessage which is a foreign key to another table called auth_user 's id column. To achieve the above, I'm trying the following on terminal: ALTER TABLE links_chatpicmessage ADD FOREIGN KEY (sender) REFERENCES auth_user; But this gives me an error: ERROR: column "sender" referenced in foreign key constraint does not exist How

alter table drop column syntax error using sqlite

北战南征 提交于 2019-12-03 05:21:49
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 advance for any help. SQLite does not fully support ALTER TABLE statements . You can only rename table, or

how to modify the size of a column

ε祈祈猫儿з 提交于 2019-12-03 04:59:10
问题 I created the table Test_Project2 in Oracle SQL Developer. After that I realized that the column proj_name is of a small size, so I decided to modify the column using the follwoing statement ALTER TABLE TEST_PROJECT2 MODIFY proj_name VARCHAR2(300); but for some reason Oracle SQL Developer underscores the semi-colon with red and I do not what is mistake and how to correct it Test_Project2 : CREATE TABLE Test_Project2 ( proj_id number(30), proj_name VARCHAR2 (30), proj_desc VARCHAR2(300) ); 回答1

Hive Alter table change Column Name

随声附和 提交于 2019-12-03 03:42:02
问题 I am trying to rename a columnName in Hive. Is there a way to rename column name in Hive . tableA (column1 ,_c1,_c2) to tableA(column1,column2,column3) ?? 回答1: Change Column Name/Type/Position/Comment: ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name] Example: CREATE TABLE test_change (a int, b int, c int); // will change column a's name to a1 ALTER TABLE test_change CHANGE a a1 INT; 回答2: Command works only if "use"

Does adding a null column to a postgres table cause a lock?

荒凉一梦 提交于 2019-12-03 02:18:05
I think I read somewhere that running an ALTER TABLE foo ADD COLUMN baz text on a postgres database will not cause a read or write lock. Setting a default value causes locking, but allowing a null default prevents a lock. I can't find this in the documentation, though. Can anyone point to a place that says, definitively, if this is true or not? The different sorts of locks and when they're used are mentioned in the doc in Table-level Locks . For instance, Postgres 11's ALTER TABLE may acquire a SHARE UPDATE EXCLUSIVE , SHARE ROW EXCLUSIVE , or ACCESS EXCLUSIVE lock. Postgres 9.1 through 9.3