sqlÖÐÐ޸ıíµÄ½á¹¹µÄÓï¾ä
sql ÖÐÐ޸ıí½á¹¹µÄÓï¾ä°üÀ¨£ºalter table table_name add column_name data_type Ìí¼ÓÁÐalter table table_name alter column column_name data_type ÐÞ¸ÄÁÐÊý¾ÝÀàÐÍalter table table_name drop column column_name ɾ³ýÁÐalter table table_name rename column old_column_name to new_column_
SQL ÖÐÐ޸ıí½á¹¹µÄÓï¾ä
Ð޸ıí½á¹¹ÊÇ SQL Öг£¼ûµÄ²Ù×÷£¬ËüÔÊÐíÄú¸ü¸Ä±íµÄÁС¢Êý¾ÝÀàÐͺÍÔ¼Êø¡£ÒÔÏÂÊÇһЩ³£ÓõÄÓï¾ä£º
1. Ìí¼ÓÁÐ
ALTER TABLE table_name ADD column_name data_type
ÀýÈ磺
ALTER TABLE employees ADD salary INT
µÇ¼ºó¸´ÖÆ
2. ÐÞ¸ÄÁÐ
ALTER TABLE table_name ALTER COLUMN column_name data_type
ÀýÈ磺
ALTER TABLE employees ALTER COLUMN salary DECIMAL(10,2)
µÇ¼ºó¸´ÖÆ
3. ɾ³ýÁÐ
ALTER TABLE table_name DROP COLUMN column_name
ÀýÈ磺
ALTER TABLE employees DROP COLUMN bonus
µÇ¼ºó¸´ÖÆ
4. ¸ü¸ÄÁÐÃû³Æ
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name
ÀýÈ磺
ALTER TABLE employees RENAME COLUMN first_name TO name
µÇ¼ºó¸´ÖÆ
5. Ìí¼ÓÔ¼Êø
Ö÷¼ü£º
ALTER TABLE table_name ADD PRIMARY KEY (column_name)
ÀýÈ磺
ALTER TABLE employees ADD PRIMARY KEY (employee_id)
µÇ¼ºó¸´ÖÆ
Íâ¼ü£º
ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES referenced_table(column_name)
ÀýÈ磺
ALTER TABLE orders ADD FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
µÇ¼ºó¸´ÖÆ
ΨһԼÊø£º
ALTER TABLE table_name ADD UNIQUE (column_name)
ÀýÈ磺
ALTER TABLE employees ADD UNIQUE (email)
µÇ¼ºó¸´ÖÆ
6. ɾ³ýÔ¼Êø
Ö÷¼ü£º
ALTER TABLE table_name DROP PRIMARY KEY
Íâ¼ü£º
ALTER TABLE table_name DROP FOREIGN KEY column_name
ΨһԼÊø£º
ALTER TABLE table_name DROP INDEX index_name (ÆäÖÐ index_name ÊÇΨһԼÊøµÄÃû³Æ)
ÒÔÉϾÍÊÇsqlÖÐÐ޸ıíµÄ½á¹¹µÄÓï¾äµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡