在 SQL 中重命名列名

2024年8月29日 | 阅读 7 分钟

列名的用途是标识表中的特定列。列名必须与列中存在的数据相关,以提高清晰度和理解。

有时,我们需要更改数据库中的列名,以保持其中包含数据的相关性。SQL 允许使用以下命令更改列名:

  • ALTER TABLE 命令可以更改表的数据类型、列名和其他属性。
  • RENAME COLUMN 命令与 ALTER TABLE 命令一起使用,以重命名数据库中的列。

在 SQL 中重命名列的好处

  • 与列中的数据相关的清晰名称有助于提高清晰度,并易于区分不同的列,避免混淆。
  • 更具描述性的列名有助于提高可读性,使表中的数据更易于理解。
  • 它有助于有效地组织数据库中的数据。
  • 它简化了列名,以便于理解。
  • 它有助于避免因在表中为列使用相似名称而可能出现的错误。

在 SQL 中重命名列名的语法将取决于您使用的数据库,但重命名 SQL 中列名的基本语法如下:

我们将通过示例来了解如何在 MySQL、PostgreSQL、Microsoft SQL Server、MariaDB、Oracle 等数据库中更改列名。

在 MySQL、PostgreSQL、MariaDB 和 Oracle 中重命名列名的示例

示例 1

让我们考虑一个名为 student_table 的表,其中包含 stu_number、stu_name、stu_age、stu_address、stu_stream 和 stu_marks 字段。

stu_numberstu_namestu_agestu_addressstu_streamstu_marks
1Inaya20法里达巴德科学95
2Jai22Noida商业80
3Ishaan20Noida艺术74
4Milan18Roorkee科学72
5Pari21古尔冈科学81
6Khushi20果阿商业82
7Himanshu23Meerut商业75
8Rohit22Meerut商业86
9Sadaf20果阿艺术74
10Asha19古尔冈艺术70

我们将把列名从 stu_number 重命名为 stu_id。下面的查询将更改列名:

查询

输出

从输出中可以明显看出,列名已从 stu_number 更改为 stu_id。

stu_idstu_namestu_agestu_addressstu_streamstu_marks
1Inaya20法里达巴德科学95
2Jai22Noida商业80
3Ishaan20Noida艺术74
4Milan18Roorkee科学72
5Pari21古尔冈科学81
6Khushi20果阿商业82
7Himanshu23Meerut商业75
8Rohit22Meerut商业86
9Sadaf20果阿艺术74
10Asha19古尔冈艺术70

示例 2

让我们考虑另一个名为 product_table 的表,其中包含 id、name、quantity 和 money 字段。

idnamequantitymoney
1Chocolates20200
2Candies22110
3Lollipops20100
4Chip1890
5苹果21600
6酥饼(Biscuits)20200
7Chochos23230
8Waffle Chips22220
9Jelly Candies20400
10Popcorn19380

我们将把列名从 money 重命名为 price。下面的查询将更改列名:

查询

输出

从输出中可以清楚地看出,列名已从 money 更改为 price。

idnamequantityprice
1Chocolates20200
2Candies22110
3Lollipops20100
4Chip1890
5苹果21600
6酥饼(Biscuits)20200
7Chochos23230
8Waffle Chips22220
9Jelly Candies20400
10Popcorn19380

示例 3

让我们考虑一个名为 student_table 的表,其中包含 stu_number、stu_name、stu_age、stu_address、stu_stream 和 marks 字段。

stu_numberstu_namestu_agestu_addressstu_streammarks
11Robin19Seattle商业85
12杰克20Washington艺术95
13Michael21Austin科学84
14Justin19Columbus商业75
15Ariana20Austin商业80
16赛琳娜21Washington艺术90
17Katrina22Columbus艺术89
18Tom23Columbus商业85
19亨利21Austin科学79
20Alexander20Seattle科学90

我们将把列名从 marks 重命名为 stu_marks。下面的查询将更改列名:

查询

输出

从输出中可以明显看出,列名已从 marks 更改为 stu_marks。

stu_numberstu_namestu_agestu_addressstu_streamstu_marks
11Robin19Seattle商业85
12杰克20Washington艺术95
13Michael21Austin科学84
14Justin19Columbus商业75
15Ariana20Austin商业80
16赛琳娜21Washington艺术90
17Katrina22Columbus艺术89
18Tom23Columbus商业85
19亨利21Austin科学79
20Alexander20Seattle科学90

在 Microsoft SQL Server 中重命名列名的示例

示例 1

让我们考虑另一个名为 employee_table 的表,其中包含 emp_id、emp_id、emp_address、department 和 emp_salary 字段。

emp_idemp_nameemp_addressdepartmentemp_salary
50JohnWashington销售25000
51ShirleyAustinSoftware Testing50000
52丹尼尔Columbus销售40000
53AnthonyAustinContent Writing20000
54JamesChicagoHR45000
55海伦PhoenixSoftware Testing55000
56NicoleChicago销售35000
57NickColumbusHR40000
58彼得PhoenixContent Writing25000
59AshleyWashingtonContent Writing35000

我们将把列名从 department 重命名为 emp_department。下面的查询将更改列名:

查询

输出

从输出中可以清楚地看出,列名已从 department 更改为 emp_department。

emp_idemp_nameemp_addressemp_departmentemp_salary
50JohnWashington销售25000
51ShirleyAustinSoftware Testing50000
52丹尼尔Columbus销售40000
53AnthonyAustinContent Writing20000
54JamesChicagoHR45000
55海伦PhoenixSoftware Testing55000
56NicoleChicago销售35000
57NickColumbusHR40000
58彼得PhoenixContent Writing25000
59AshleyWashingtonContent Writing35000

示例 2

让我们考虑另一个名为 book_table 的表,其中包含 id、book_name、author、pages 和 price 字段。

idbook_name作者pagesprice
1A Place Called HomePreeti Shenoy324250
2Pride, Prejudice, and PunditryShashi Tharoor600630
3Lal SalamSmriti Irani25665
4The Fault in Our StarsJohn Green352250
5The Book ThiefMarkus Zusak624295
6Animal FarmGeorge Orwell88120
7The Ten Trillion DreamSubhash Chandra Garg712650
8UnfinishedPriyanka Chopra Jonas280420
9The Princess BrideWilliam Goldman512780
10Life of PiYann Martel352240

我们将把列名从 book_name 重命名为 book_list。下面的查询将更改列名:

查询

输出

从输出中可以清楚地看出,列名已从 book_name 更改为 book_list。

idbook_list作者pagesprice
1A Place Called HomePreeti Shenoy324250
2Pride, Prejudice, and PunditryShashi Tharoor600630
3Lal SalamSmriti Irani25665
4The Fault in Our StarsJohn Green352250
5The Book ThiefMarkus Zusak624295
6Animal FarmGeorge Orwell88120
7The Ten Trillion DreamSubhash Chandra Garg712650
8UnfinishedPriyanka Chopra Jonas280420
9The Princess BrideWilliam Goldman512780
10Life of PiYann Martel352240

示例 3

让我们考虑另一个名为 employee_table 的表,其中包含 emp_id、emp_name、emp_address、department 和 salary 字段。

emp_idemp_nameemp_addressdepartmentsalary
50JohnWashington销售25000
51ShirleyAustinSoftware Testing50000
52丹尼尔Columbus销售40000
53AnthonyAustinContent Writing20000
54JamesChicagoHR45000
55海伦PhoenixSoftware Testing55000
56NicoleChicago销售35000
57NickColumbusHR40000
58彼得PhoenixContent Writing25000
59AshleyWashingtonContent Writing35000

我们将把列名从 salary 重命名为 emp_salary。下面的查询将更改列名:

查询

输出

从输出中可以清楚地看出,列名已从 salary 更改为 emp_salary。

emp_idemp_nameemp_addressdepartmentemp_salary
50JohnWashington销售25000
51ShirleyAustinSoftware Testing50000
52丹尼尔Columbus销售40000
53AnthonyAustinContent Writing20000
54JamesChicagoHR45000
55海伦PhoenixSoftware Testing55000
56NicoleChicago销售35000
57NickColumbusHR40000
58彼得PhoenixContent Writing25000
59AshleyWashingtonContent Writing35000

结论

在本文中,您学习了如何在 SQL 中重命名列名。您了解到,在不同的 SQL 数据库中重命名列名的方式略有不同。您学习了在 MySQL、Oracle、MariaDB、PostgreSQL 和 Microsoft SQL Server 等 SQL 数据库中重命名列名的不同方法。


下一个主题SQL EXISTS