SQL 修改列
在这篇 SQL 文章中,我们将讨论如何在SQL语言中修改表的列。
一、什么是 MODIFY COLUMN 语句?
在许多情况下,您可能需要修改现有表的现有列或字段。因此,SQL 提供了带有 ALTER TABLE 命令的 MODIFY 关键字来解决这个问题。
MODIFY 关键字修改 SQL 表的现有字段的大小、数据类型和约束。
二、MODIFY Column 语句的语法
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 中的 MODIFY Column 语句示例
如果你想学习如何修改表的列,你必须创建一个新表。
以下语句在 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)
);
下面的 INSERT 查询在Employee_Grade表中插入员工的成绩和备注记录:
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 |
查询 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 表的名字、姓氏和第一个城市的大小。
热门文章
优秀文章