SQL add columns to a table - SQL ALTER
SQL Tutorial » SQL add columns to a table - SQL ALTER
How to create a new column in MySQL? using SQL ADD COLUMN statement.
To add a new column to an existing table, you use the ALTER TABLE ADD COLUMN statement.
ALTER TABLE statement is used to add, delete, or modify columns in an existing table and also used to add and drop various constraints on a table.
Syntax:
ADD column_name datatype;
Let's look at the "Users" table: We will have five columns as: "id", "first_name", "last_name", "country" and "email":
ID | First Name | Last Name | Country | |
---|---|---|---|---|
1 | Clark | Kent | USA | clarkkent@mail.com |
2 | Pire | Joveau | France | pierej@mail.com |
3 | Nicky | Anderson | USA | nickya@mail.com |
4 | Pepe | Martínez | Spain | martinezpeper@mail.com |
5 | Peter | Parker | SUA | peterparker@mail.com |
SQL add columns to a table - SQL ALTER
Let's add a column named "user_name" to the "Users" sql table above:
ADD user_name varchar(150);
Output displayed:
ID | First Name | Last Name | Country | UserName | |
---|---|---|---|---|---|
1 | Clark | Kent | USA | clarkkent@mail.com | |
2 | Pire | Joveau | France | pierej@mail.com | |
3 | Nicky | Anderson | USA | nickya@mail.com | |
4 | Pepe | Martínez | Spain | martinezpeper@mail.com | |
5 | Peter | Parker | SUA | peterparker@mail.com |
Related subjects:
How to create database phpmyadmin?
Insert multiple rows
PHP select / view data
Conect to MySQLi
SQL insert into table
Tags: sql add column: to table, default value, if not exists, after another column, after, with value, between two columns, to view, with calculated value. How do I create a new column in MySQL?, How do I add a column to an existing table in MySQL?
SQL add columns after to a table - SQL ALTER
Let's add a column named "user_name" after a certain specified column to the "Users" sql table above. Because we specify the position of the "user_name" column explicitly after the name column, MySQL will obey this.
ADD COLUMN user_name varchar(150) AFTER last_name;
Output displayed:
ID | First Name | Last Name | user_name | Country | |
---|---|---|---|---|---|
1 | Clark | Kent | USA | clarkkent@mail.com | |
2 | Pire | Joveau | France | pierej@mail.com | |
3 | Nicky | Anderson | USA | nickya@mail.com | |
4 | Pepe | Martínez | Spain | martinezpeper@mail.com | |
5 | Peter | Parker | SUA | peterparker@mail.com |
SQL add more columns to a table - SQL ALTER TABLE
You can add two more columns as "user_name" and "date" to the Users table at the same time.
ADD COLUMN user_name VARCHAR(100) NOT NULL,
ADD COLUMN date datetime NOT NULL;
sql add column: to table, default value, if not exists, after another column, after, with value, between two columns, to view, with calculated value. How do I create a new column in MySQL?, How do I add a column to an existing table in MySQL?
SQL add columns to a table - SQL ALTER - sql tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.