SQL修改列
在这篇SQL文章中,我们将讨论如何在结构化查询语言中修改表的列。
什么是修改列语句?
在许多情况下,您可能需要修改现有表的现有列或字段。因此,SQL提供了MODIFY关键字和ALTER TABLE命令来克服这个问题。
MODIFY关键字用于修改SQL表已有字段的大小、数据类型和约束。
修改列语句的语法
ALTER TABLE Table_Name MODIFY Column_Name New_Definition_of_Existing_Column;
ALTER语法允许我们修改现有表的单个字段。
如果你想在一条语句中修改同一个SQL表的多个新字段,那么你必须使用以下语法:
ALTER TABLE Table_Name MODIFY Column_Name_1 New_Definition_of_Existing_Column_1,
MODIFY Column_Name_2 New_Definition_of_Existing_Column_2,
MODIFY Column_Name_3 New_Definition_of_Existing_Column_3,
.......,
MODIFY Column_Name_N New_Definition_of_Existing_Column_N;
SQL中修改列语句的示例
如果您想学习如何修改表的列,您必须创建一个新表。
以下语句在SQL中创建了Teacher_Details表:
CREATE TABLE Employee_Grade
(
Employee_ID INT PRIMARY KEY,
First_Name VARCHAR (100),
Last_Name VARCHAR (100),
First_City Varchar(120),
Second_City Varchar(120),
New_City Varchar(120),
Attendance_Remarks INT,
Work_Remarks INT,
Grade Varchar (80)
);
以下是将员工的等级和备注记录插入到 Employee_Grade 表的INSERT查询:
INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (10, Ramesh, Sharma, Lucknow Aurangabad, Ghaziabad, 88, 95, A2);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES ( 02, Yadu, Sharma, Aurangabad, Ghaziabad, Noida, 95, 82, A1 );
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (07, Vijay, Ramna, Noida, Ghaziabad, Lucknow, 91, 95, A1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (04, Bhanu, Rangopalr, Ghaziabad, Noida, Lucknow, 85, 82, NULL);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (11, Harry, Roy, Noida, Kanpur, Ghaziabad, 95, 97, A1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (16, Akash, Ramna, Ghaziabad, Meerut, Aurangabad, 95, 90, B1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (19, Ram, Ramna, Lucknow, Ghaziabad, Aurangabad, 89, 95, NULL);
下面的SELECT语句显示了上述插入记录的 Employee_Grade 表:
SELECT * FROM Employee_Grade;
Employee_ID | First_Name | Last_Name | First_City | Second_City | New_City | Attendance_Remarks | Work_Remarks | Grade |
---|---|---|---|---|---|---|---|---|
10 | Ramesh | Sharma | Lucknow | Aurangabad | Ghaziabad | 88 | 95 | A2 |
02 | Yadu | Sharma | Aurangabad | Ghaziabad | Ghaziabad | 95 | 82 | A1 |
07 | Vijay | Ramna | Noida | Ghaziabad | Lucknow | 91 | 95 | A1 |
04 | Bhanu | Rangopalr | Ghaziabad | Noida | Lucknow | 85 | 82 | NULL |
11 | Harry | Roy | Noida | Kanpur | Ghaziabad | 95 | 97 | A1 |
16 | Akash | Ramna | Ghaziabad | Meerut | Aurangabad | 95 | 90 | B1 |
19 | Ram | Ramna | Lucknow | Ghaziabad | Aurangabad | 89 | 95 | NULL |
查询1: 下面的ALTER查询修改了上述表格的Grade列:
ALTER TABLE Employee_Grade MODIFY Grade Varchar(80) NOT NULL;
这个查询将表中的Grade列设置为’NOT NULL’。现在,这个列将不再存储NULL值。
查询2: 以下ALTER查询修改了上述Employee_Grade表的New_City列:
ALTER TABLE Employee_Grade MODIFY New_City Varchar(80);
这个查询会改变表中新城市列的大小。
查询3: 以下ALTER查询修改了以上表格的多列:
ALTER TABLE Employee_Grade MODIFY First_Name Varchar(150), MODIFY Last_Name Varchar(150), MODIFY First_City Varchar(70);
这个查询会改变Employee_grade表中的名字、姓氏和第一个城市的大小。