sqlserver 表结构调整
时间: 2023-12-05 07:05:13 浏览: 76
sql server 表结构修改方法
为了调整SQL Server表结构,可以使用以下SQL语句:
1. 重命名表
```sql
EXEC sp_rename 'old_table_name', 'new_table_name';
```
2. 添加列
```sql
ALTER TABLE table_name ADD column_name datatype;
```
3. 删除列
```sql
ALTER TABLE table_name DROP COLUMN column_name;
```
4. 修改列
```sql
ALTER TABLE table_name ALTER COLUMN column_name datatype;
```
5. 添加主键
```sql
ALTER TABLE table_name ADD CONSTRAINT pk_constraint_name PRIMARY KEY (column1, column2, ... column_n);
```
6. 删除主键
```sql
ALTER TABLE table_name DROP CONSTRAINT pk_constraint_name;
```
7. 添加外键
```sql
ALTER TABLE table_name ADD CONSTRAINT fk_constraint_name FOREIGN KEY (column1, column2, ... column_n) REFERENCES parent_table (column1, column2, ... column_n);
```
8. 删除外键
```sql
ALTER TABLE table_name DROP CONSTRAINT fk_constraint_name;
```
阅读全文