Change a Nullable column to NOT NULL with Default Value

I think you will need to do this as three separate statements. I’ve been looking around and everything i’ve seen seems to suggest you can do it if you are adding a column, but not if you are altering one. ALTER TABLE dbo.MyTable ADD CONSTRAINT my_Con DEFAULT GETDATE() for created UPDATE MyTable SET Created = … Read more

ALTER table – adding AUTOINCREMENT in MySQL

CREATE TABLE ALLITEMS( itemid INT(10)UNSIGNED, itemname VARCHAR(50) ); ALTER TABLE ALLITEMS CHANGE itemid itemid INT(10)AUTO_INCREMENT PRIMARY KEY; DESC ALLITEMS; INSERT INTO ALLITEMS(itemname) VALUES (‘Apple’), (‘Orange’), (‘Banana’); SELECT * FROM ALLITEMS; I was confused with CHANGE and MODIFY keywords before too: ALTER TABLE ALLITEMS CHANGE itemid itemid INT(10)AUTO_INCREMENT PRIMARY KEY; ALTER TABLE ALLITEMS MODIFY itemid INT(5); … Read more

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

To add a constraint to a column It needs to exists first into the table there is no command in Postgresql that you can use that will add the column and add the constraint at the same time. It must be two separate commands. You can do it using following commands: First do as: ALTER … Read more

add column to mysql table if it does not exist

Here is a working solution (just tried out with MySQL 5.0 on Solaris): DELIMITER $$ DROP PROCEDURE IF EXISTS upgrade_database_1_0_to_2_0 $$ CREATE PROCEDURE upgrade_database_1_0_to_2_0() BEGIN — rename a table safely IF NOT EXISTS( (SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=’my_old_table_name’) ) THEN RENAME TABLE my_old_table_name TO my_new_table_name, END IF; — add a column safely … Read more

Create an index on a huge MySQL production table without table locking

[2017] Update: MySQL 5.6 has support for online index updates https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html#online-ddl-index-syntax-notes In MySQL 5.6 and higher, the table remains available for read and write operations while the index is being created or dropped. The CREATE INDEX or DROP INDEX statement only finishes after all transactions that are accessing the table are completed, so that the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)