Mysql special queries

/* select data before today */
SELECT * FROM `students` where created_at < date(curdate()) order by created_at desc;

-- Copy table structure from another database
CREATE TABLE users(SELECT * FROM MyTestDB.users LIMIT 0);

-- create a foreign key for delete child

FOREIGN KEY(foreign_id)
REFERENCES reltional_table_name(relation_table_id)
ON DELETE CASCADE

-- drop foreign column from table
-- _______________________________________

ALTER TABLE table_name DROP FOREIGN KEY `constraint_name_of_foreign_key` ;
ALTER TABLE table_name DROP KEY `column_name`;
ALTER TABLE table_name DROP COLUMN `column_name`;


-- SHow command
SHOW CREATE TABLE table_name;

-- Add Created At Column
ALTER TABLE table_name ADD COLUMN created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;

-- Add Updated At Column
ALTER TABLE students ADD COLUMN update_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

-- Drop Column
ALTER TABLE table_name DROP COLUMN column_name;

--Modify Column
ALTER TABLE USERS MODIFY COLUMN status BOOLEAN DEFAULT FALSE;


Comments