0% found this document useful (0 votes)
11 views

SQL Table

Uploaded by

santoshdvg1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Table

Uploaded by

santoshdvg1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SQL

Topperworld.in

Table

• Table is a collection of data, organized in terms of rows and columns.


• In DBMS term, table is known as relation and row as tuple.
• Table is the simple form of data storage.
• A table is also considered as a convenient representation of relations.

Let's see an example of an employee table:

Employee

EMP_NAME ADDRESS SALARY

Ankit Lucknow 15000

Raman Allahabad 18000

Mike New York 20000

In the above table, "Employee" is the table name, "EMP_NAME", "ADDRESS"


and "SALARY" are the column names.

The combination of data of multiple columns forms a row e.g. "Ankit",


"Lucknow" and 15000 are the data of one row.

©Topperworld
SQL

❖ SQL TABLE Variable

• The SQL Table variable is used to create, modify, rename, copy and delete
tables. Table variable was introduced by Microsoft.
• It was introduced with SQL server 2000 to be an alternative of temporary
tables.
• It is a variable where we temporary store records and results. This is same
like temp table but in the case of temp table we need to explicitly drop it.
• Table variables are used to store a set of records.

So declaration syntax generally looks like CREATE TABLE syntax.

create table "tablename"

("column1" "data type",

"column2" "data type",

...

"columnN" "data type");

• When a transaction rolled back the data associated with table variable is
not rolled back.
• A table variable generally uses lesser resources than a temporary variable.
• Table variable cannot be used as an input or an output parameter.

❖ SQL CREATE TABLE

• SQL CREATE TABLE statement is used to create table in a database.

©Topperworld
SQL

• If you want to create a table, you should name the table and define its
column and each column's data type.

Syntax:

Let's see the simple syntax to create the table.

create table "tablename"

("column1" "data type",

"column2" "data type",

"column3" "data type",

...
"columnN" "data type");

The data type of the columns may vary from one database to another.

Example:

Let us take an example to create a STUDENTS table with ID as primary key and
NOT NULL are the constraint showing that these fields cannot be NULL while
creating records in the table.

SQL> CREATE TABLE STUDENTS (

ID INT NOT NULL,

NAME VARCHAR (20) NOT NULL,

AGE INT NOT NULL,

ADDRESS CHAR (25),

PRIMARY KEY (ID)

);

©Topperworld
SQL

FIELD TYPE NULL KEY DEFAULT EXTRA

ID Int(11) NO PRI

NAME Varchar(20) NO

AGE Int(11) NO

ADDRESS Varchar(25) YES NULL

Now you have the STUDENTS table available in your database and you can use
to store required information related to students.

Create a Table using another table :

✓ We can create a copy of an existing table using the create table command.
✓ The new table gets the same column signature as the old table.
✓ We can select all columns or some specific columns.
✓ If we create a new table using an old table, the new table will be filled
with the existing value from the old table.

Syntax:

The basic syntax for creating a table with the other table is:

©Topperworld
SQL

CREATE TABLE table_name AS

SELECT column1, column2,...

FROM old_table_name WHERE ..... ;

The following SQL creates a copy of the employ


ee table.

CREATE TABLE EmployeeCopy AS

SELECT EmployeeID, FirstName, Email

FROM Employee;

SQL Primary Key with CREATE TABLE Statement

The following query creates a PRIMARY KEY on the "D" column when the
"Employee" table is created.

❖ SQL DROP TABLE

• A SQL DROP TABLE statement is used to delete a table definition and all
data from a table.
• This is very important to know that once a table is deleted all the
information available in the table is lost forever, so we have to be very
careful when using this command.

Syntax:

Let's see the syntax to drop the table from the database.

DROP TABLE "table_name";

©Topperworld
SQL

Example:

Let us take an example:

First we verify STUDENTS table and then we would delete it from the database.

SQL> DESC STUDENTS;

FIELD TYPE NULL KEY DEFAULT EXTRA

ID Int(11) NO PRI

NAME Varchar(20) NO

AGE Int(11) NO

ADDRESS Varchar(25) YES NULL

This shows that STUDENTS table is available in the database, so we can drop it
as follows:

SQL>DROP TABLE STUDENTS;

Now, use the following command to check whether table exists or not.

SQL> DESC STUDENTS;

©Topperworld
SQL

❖ SQL DELETE TABLE

• The DELETE statement is used to delete rows from a table. If you want to
remove a specific row from a table you should use WHERE condition.

DELETE FROM table_name [WHERE condition];

• But if you do not specify the WHERE condition it will remove all the rows
from the table.

DELETE FROM table_name;

There are some more terms similar to DELETE statement like as DROP statement
and TRUNCATE statement but they are not exactly same there are some
differences between them.

Difference between DELETE and TRUNCATE statements :

• There is a slight difference b/w delete and truncate statement.


• The DELETE statement only deletes the rows from the table based on the
condition defined by WHERE clause or delete all the rows from the table
when condition is not specified.
• But it does not free the space containing by the table.
• The TRUNCATE statement: it is used to delete all the rows from the
table and free the containing space.

Let's see an "employee" table.

Emp_id Name Address Salary

©Topperworld
SQL

1 Aryan Allahabad 22000

2 Shurabhi Varanasi 13000

3 Pappu Delhi 24000

Execute the following query to truncate the table:

TRUNCATE TABLE employee;

Difference b/w DROP and TRUNCATE statements

When you use the drop statement it deletes the table's row together with the
table's definition so all the relationships of that table with other tables will no
longer be valid.

When you drop a table:

⚫ Table structure will be dropped

⚫ Relationship will be dropped

⚫ Integrity constraints will be dropped

⚫ Access privileges will also be dropped

On the other hand when we TRUNCATE a table, the table structure remains the
same, so you will not face any of the above problems.

©Topperworld
SQL

❖ SQL RENAME TABLE

• In some situations, database administrators and users want to change the


name of the table in the SQL database because they want to give a more
relevant name to the table.
• Any database user can easily change the name by using the RENAME
TABLE and ALTER TABLE statement in Structured Query Language.
• The RENAME TABLE and ALTER TABLE syntax help in changing the name
of the table.

Syntax of RENAME statement in SQL

RENAME old_table _name To new_table_name ;

Examples of RENAME statement in SQL

Here, we have taken the following two different SQL examples, which will help
you how to change the name of the SQL table in the database using RENAME
statement:

Example : Let's take an example of a table named Cars:

Car Name Car Color Car Cost

Hyundai Creta White 10,85,000

©Topperworld
SQL

Hyundai Venue White 9,50,000

Hyundai i20 Red 9,00,000

Kia Sonet White 10,00,000

Kia Seltos Black 8,00,000

Swift Dezire Red 7,95,000

Table: Cars

⚫ Suppose, you want to change the above table name into


"Car_2021_Details". For this, you have to type the following RENAME
statement in SQL:

RENAME Cars To Car_2021_Details ;

⚫ After this statement, the table "Cars" will be changed into table name
"Car_2021_Details".

❖ SQL TRUNCATE TABLE

• A truncate SQL statement is used to remove all rows (complete data) from
a table.
• It is similar to the DELETE statement with no WHERE clause.

©Topperworld
SQL

TRUNCATE TABLE Vs DELETE TABLE

✓ Truncate table is faster and uses lesser resources than DELETE TABLE
command.

TRUNCATE TABLE Vs DROP TABLE

✓ Drop table command can also be used to delete complete table but it
deletes table structure too.
✓ TRUNCATE TABLE doesn't delete the structure of the table.

Syntax:

Let's see the syntax to truncate the table from the database.

TRUNCATE TABLE table_name;

Note: The rollback process is not possible after truncate table statement.
Once you truncate a table you cannot use a flashback table statement to
retrieve the content of the table.

❖ SQL COPY TABLE

• If you want to copy the data of one SQL table into another SQL table in
the same SQL server, then it is possible by using the SELECT INTO
statement in SQL.

©Topperworld
SQL

• The SELECT INTO statement in Structured Query Language copies the


content from one existing table into the new table.
• SQL creates the new table by using the structure of the existing table.

Syntax of SELECT INTO statement in SQL

SELECT * INTO New_table_name FROM old_table_name;

Examples of SELECT INTO statement in SQL :

Example : In this example, we have a table called Cars with three columns:

Car Name Car Color Car Cost

Hyundai Creta White 10,85,000

Hyundai Venue White 9,50,000

Hyundai i20 Red 9,00,000

Kia Sonet White 10,00,000

Kia Seltos Black 8,00,000

Swift Dezire Red 7,95,000

Table: Cars

©Topperworld
SQL

⚫ Suppose you want to copy the content of the above Car table into the new
table Car_Details. For this, you have to type the following query in SQL:

SELECT * INTO Car_Details FROM Cars;

⚫ Let's check the Car_Details table is created successfully or not in the


database:

SELECT * FROM Car_Details;

Car Name Car Color Car Cost

Hyundai Creta White 10,85,000

Hyundai Venue White 9,50,000

Hyundai i20 Red 9,00,000

Kia Sonet White 10,00,000

Kia Seltos Black 8,00,000

Swift Dezire Red 7,95,000

Table: Car_Details

©Topperworld
SQL

❖ SQL TEMP TABLE

• The concept of temporary table is introduced by SQL server. It helps


developers in many ways:
• Temporary tables can be created at run-time and can do all kinds of
operations that a normal table can do.
• These temporary tables are created inside tempdb database.

There are two types of temp tables based on the behavior and scope.

1. Local Temp Variable

2. Global Temp Variable

➢ Local Temp Variable

• Local temp tables are only available at current connection time.


• It is automatically deleted when user disconnects from instances.
• It is started with hash (#) sign.

CREATE TABLE #local temp table (

User id int,

Username varchar (50),

User address varchar (150)

©Topperworld
SQL

➢ Global Temp Variable

• Global temp tables name starts with double hash (##).


• Once this table is created, it is like a permanent table.
• It is always ready for all users and not deleted until the total connection
is withdrawn.

CREATE TABLE ##new global temp table (

User id int,

User name varchar (50),

User address varchar (150)


)

❖ SQL ALTER TABLE

• The ALTER TABLE statement in Structured Query Language allows you to


add, modify, and delete columns of an existing table.
• This statement also allows database users to add and remove various SQL
constraints on the existing tables.
• Any user can also change the name of the table using this statement.

ALTER TABLE ADD Column statement in SQL :

• In many situations, you may require to add the columns in the existing
table.

©Topperworld
SQL

• Instead of creating a whole table or database again you can easily add
single and multiple columns using the ADD keyword.

Syntax of ALTER TABLE ADD Column statement in SQL

ALTER TABLE table_name ADD column_name column-definition;

The above syntax only allows you to add a single column to the existing table.

©Topperworld

You might also like