DATA with BARAA

SQL Create Table

SQL CREATE Table

In this tutorial, you will learn how to create a new database table using the SQL CREATE statements.

Syntax

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

				
					CREATE TABLE table_name (
    column1_name data_type constraints,
    column2_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

Create new SQL table called Persons with 4 columns: ID, person name, birth date, and phone

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

				
					CREATE TABLE db_sql_tutorial.persons (
    id INT PRIMARY KEY AUTO_INCREMENT,
    person_name VARCHAR(50) NOT NULL,
    birth_Date DATE,
    phone VARCHAR(15) NOT NULL UNIQUE
);
				
			

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 !
Exit mobile version