28 Kasım 2021 Pazar

ALTER TABLE

Giriş
Açıklaması şöyle. Yani ALTER TABLE işlemi tablonun kopyası üzerinde yapar.
ALTER TABLE performs all of its operations on a copy of the table, and not on the table itself. In other words, when MySQL is ordered to modify a table and an ALTER TABLE statement is run, MySQL makes a copy of the table that is being modified, inserts the data that we currently have inside of our table into it, performs all of the required operations there, and copies the data back into our table — only then we see the results.
ADD
Örnek
BOOLEAN sütun eklemek için şöyle yaparız.
ALTER TABLE `customer` ADD `smsSent` BOOLEAN NOT NULL AFTER `time_paid`;
Örnek
VARCHAR sütun eklemek için şöyle yaparız.
alter table tbl_sample add COLUMN column1 VARCHAR(50) default 'just a sample';
CHANGE
Örnek
VARCHAR sütunu UTF8 yapmak için şöyle yaparız.
ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>` VARCHAR(100) 
CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;
DROP COLUMN
Örnek 
Şöyle yaparız
-- içinde DATE veri varsa
ALTER TABLE table_name DROP COLUMN expirationdate;
ALTER TABLE table_name ADD COLUMN expirationdate TIMESTAMP DEFAULT '1970-01-01 01:00:00';
UPDATE table_name SET expirationdate ='2022-07-14 00:00:00'
-- tüm değerler NULL ise ALTER TABLE table_name MODIFY COLUMN dynamiclifetime TIMESTAMP;
MODIFY COLUMN
Örnek 
Şöyle yaparız
ALTER TABLE table_name MODIFY COLUMN column_name TIMESTAMP;
Örnek 
Şöyle yaparız
ALTER TABLE demo_table MODIFY demo_column VARCHAR(150) NOT NULL AFTER another_column;
RENAME COLUMN
Örnek
Şöyle yaparız
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
Şöyle yaparız
ALTER TABLE table_name RENAME COLUMN FUNCTIONAL_NUMERIC TO FUNCTIONAL_NUMBER;
RENAME TABLE
Örnek
Şöyle yaparız
ALTER TABLE table_name RENAME NOTIFICATION1;

Hiç yorum yok:

Yorum Gönder

Soft Delete

Giriş Açıklaması  şöyle When using the soft delete mechanism on the database, you might run into a situation where a record with a unique co...