DATA with BARAA

DATA with BARAA

fun with data

SQL Basics

Querying Data

Filtering Data

Joining Tables

SQL Functions

Modifying Data

Defining Data

SQL ALTER Table

In this tutorial, you will learn how to modify an existing database table using the SQL ALTER statements.

Syntax

The basic syntax of the SELECT statement to select all columns, can be given with:

				
					ALTER TABLE table_name 
ADD column_name data_type constraints
				
			

The asterisk (*) means everything. Once you use it after SELECT, you tell the database that you want all columns from the table.

Examples

To understand the SELECT statement in a better way, let’s look at the following customers and orders tables in our tutorial database:

Now, let’s check out some examples that demonstrate how it actually works.

We have the following task be to solve using SQL statements

Add new column called email to table Persons

The following Query will returns all the rows from customers table.

				
					ALTER TABLE persons
ADD email VARCHAR(15) NOT NULL
				
			

After executing the above query, you’ll get the result set something like this:

As you can see the output contains everything the whole customers tables including all rows and columns.

SELECT * helps you to examine the content of table that you are not familiar with. But be careful using it specially with big tables because database will retrieve everything which costs a lot of data movement across the network and might slow down the application

Select specific Columns from Table

Let’s say we are only interested in getting only specific columns from a table, then we could use the following  syntax of the SELECT statement:

				
					SELECT 
    column1,
    column2,
    columnN
FROM table_name
				
			

Insead of using asterisk (*) after SELECT, we will be now listing the name of columns of a table whose you want to retrive.

We have the following task to be solve using SQL statements

Retrieve only the first name and country of all customers

In the task we dont reuqire all the data, we need only specific columns. The following SQL statement selcts only first_name and country from table customers.

				
					SELECT 
    first_name,
    country
FROM customers
				
			

After executing the above query, you’ll get the result set something like this:

As you can see the result contains only the columns that we specified after SELECT.

Share it !