oracle 修改表结构语句
oracle 修改表结构语句
Oracle中可以使用ALTER TABLE语句来修改表的结构。
添加列:
ALTER TABLE table_name ADD column_name data_type;
示例:ALTER TABLE employees ADD email VARCHAR2(50);
删除列:
ALTER TABLE table_name DROP COLUMN column_name;
示例:ALTER TABLE employees DROP COLUMN email;
修改列名称及数据类型:
ALTER TABLE table_name RENAME COLUMN old_column TO new_column;
ALTER TABLE table_name MODIFY (old_column new_data_type);
示例:ALTER TABLE employees RENAME COLUMN salary TO monthly_salary;
ALTER TABLE employees MODIFY (monthly_salary NUMBER(8,2));
修改列默认值:
ALTER TABLE table_name MODIFY column_name DEFAULT default_value;
示例:ALTER TABLE employees MODIFY (email DEFAULT 'example@gmail.com');
修改主键或外键约束:
ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column_list) USING INDEX index_name;
ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (column_list) REFERENCES referenced_table (referenced_columns);
示例:ALTER TABLE orders ADD CONSTRAINT pk_orders PRIMARY KEY (order_id) USING INDEX idx_pk_orders;
ALTER TABLE order_items ADD CONSTRAINT fk_order_items FOREIGN KEY (order_id) REFERENCES orders (order_id);