Trong bài này sẽ hướng dẫn bạn cách sử dụng lệnh ALTER TABLE để thêm một column vào một table trong SQL Server, lệnh này sẽ hữu ích khi bạn muốn bổ sung column vào table.
Mục lục
- 1. Cú pháp lệnh ALTER TABLE ADD column
- 2. Ví dụ thêm column vào table với lệnh ALTER TABLE
1. Cú pháp lệnh ALTER TABLE ADD column
Để thêm một column thì bạn sử dụng lệnh ALTER TABLE ADD với cú pháp sau:
| 1 2 | ALTERTABLEtable_nameADDcolumn_name data_type column_constraint; | 
Trong đó:
- table_namelà tên table cần thêm
- column_namelà tên column muốn thêm
- data_typelà kiểu dữ liệu cho- column_name
- column_constraintlà các tùy chọn cho- column_name
Nếu bạn muốn thêm một lúc nhiều column thì sử dụng cú pháp sau:
| 1 2 3 4 5 6 | ALTERTABLEtable_nameADD    column_name_1 data_type_1 column_constraint_1,    column_name_2 data_type_2 column_constraint_2,    ...,    column_name_n data_type_n column_constraint_n; | 
Phần giải thích tương tự như trên.
2. Ví dụ thêm column vào table với lệnh ALTER TABLE
Giả sử ta có bảng sales.quotations như sau:
| 1 2 3 4 5 | CREATETABLEsales.quotations (    quotation_no INTIDENTITY PRIMARYKEY,    valid_from DATENOTNULL,    valid_to DATENOTNULL); | 
Bây giờ mình muốn thêm một column tên là description vào bảng sales.quotations thì sẽ viết lệnh SQL như sau:
| 1 2 | ALTERTABLEsales.quotations ADDdescription VARCHAR(255) NOTNULL; | 
Bây giờ mình muốn thêm 2 column amount và customer_name thì câu SQL lúc này là:
| 1 2 3 4 | ALTERTABLEsales.quotations     ADD        amount DECIMAL(10, 2) NOTNULL,        customer_name VARCHAR(50) NOTNULL; | 
Trên là hai ví dụ về cách sử dụng lệnh ALTER TABLE để thêm một hoặc nhiều column vào table trong SQL Server.
* KHOÁ HỌC ORACLE DATABASE A-Z ENTERPRISE trực tiếp từ tôi giúp bạn bước đầu trở thành những chuyên gia DBA, đủ kinh nghiệm đi thi chứng chỉ OA/OCP, đặc biệt là rất nhiều kinh nghiệm, bí kíp thực chiến trên các hệ thống Core tại VN chỉ sau 1 khoá học.
* CÁCH ĐĂNG KÝ: Gõ (.) hoặc để lại số điện thoại hoặc inbox https://m.me/tranvanbinh.vn hoặc Hotline/Zalo 090.29.12.888
* Chi tiết tham khảo:
https://bit.ly/oaz_w
=============================
KẾT NỐI VỚI CHUYÊN GIA TRẦN VĂN BÌNH:
📧 Mail: binhoracle@gmail.com
☎️ Mobile/Zalo: 0902912888
👨 Facebook: https://www.facebook.com/BinhOracleMaster
👨 Inbox Messenger: https://m.me/101036604657441 (profile)
👨 Fanpage: https://www.facebook.com/tranvanbinh.vn
👨 Inbox Fanpage: https://m.me/tranvanbinh.vn
👨👩 Group FB: https://www.facebook.com/groups/DBAVietNam
👨 Website: https://www.tranvanbinh.vn
👨 Blogger: https://tranvanbinhmaster.blogspot.com
🎬 Youtube: https://www.youtube.com/@binhguru
👨 Tiktok: https://www.tiktok.com/@binhguru
👨 Linkin: https://www.linkedin.com/in/binhoracle
👨 Twitter: https://twitter.com/binhguru
👨 Podcast: https://www.podbean.com/pu/pbblog-eskre-5f82d6
👨 Địa chỉ: Tòa nhà Sun Square - 21 Lê Đức Thọ - Phường Mỹ Đình 1 - Quận Nam Từ Liêm - TP.Hà Nội
=============================
oracle tutorial, học oracle database, Tự học Oracle, Tài liệu Oracle 12c tiếng Việt, Hướng dẫn sử dụng Oracle Database, Oracle SQL cơ bản, Oracle SQL là gì, Khóa học Oracle Hà Nội, Học chứng chỉ Oracle ở đầu, Khóa học Oracle online,sql tutorial, khóa học pl/sql tutorial, học dba, học dba ở việt nam, khóa học dba, khóa học dba sql, tài liệu học dba oracle, Khóa học Oracle online, học oracle sql, học oracle ở đâu tphcm, học oracle bắt đầu từ đâu, học oracle ở hà nội, oracle database tutorial, oracle database 12c, oracle database là gì, oracle database 11g, oracle download, oracle database 19c, oracle dba tutorial, oracle tunning, sql tunning , oracle 12c, oracle multitenant, Container Databases (CDB), Pluggable Databases (PDB), oracle cloud, oracle security, oracle fga, audit_trail,oracle RAC, ASM, oracle dataguard, oracle goldengate, mview, oracle exadata, oracle oca, oracle ocp, oracle ocm , oracle weblogic, postgresql tutorial, mysql tutorial, mariadb tutorial, ms sql server tutorial, nosql, mongodb tutorial, oci, cloud, middleware tutorial, hoc solaris tutorial, hoc linux tutorial, hoc aix tutorial, unix tutorial, securecrt, xshell, mobaxterm, putty
