AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

SQL add columns to a table - SQL ALTER


<< Previous Page
SQL tutorial
Next Page >>
SQL tutorial

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:

ALTER TABLE table_name
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 Email
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:

ALTER TABLE Users
ADD user_name varchar(150);

Output displayed:

ID First Name Last Name Country Email 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.

ALTER TABLE Users
ADD COLUMN user_name varchar(150) AFTER last_name;

Output displayed:

ID First Name Last Name user_name Country Email
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.

ALTER TABLE Users
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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home - Database
SQL - Database
SQL Syntax
SQL Select Data
SQL Insert Data
SQL Update
SQL Delete Row
PHP mysqli_query()
SQL order
SQL Create Database
SQL Display Data
SQL Count()
SQL mysqli_connect
SQL create table
SQL Where And & Or
SQL like Syntax
SQL Between Operator
SQL IN Operator
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Create a Sticky Navbar with and without ...
CSS Grid Layout. Examples of grid-gap, l...
CSS animation examples: delay, rotate, s...
CSS font weight bold, thin, values, semi...
CSS background color - opacity, gradient...
CSS flex - display: flex - CSS Flexbox L...
CSS border padding...
HTML text center horizontally and vertic...
HTML text input <input type="text"&g...
HTML link color...
How to Link CSS to HTML, external file n...
HTML link - hyperlink...
jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...