SQL Table
SQL Table
Topperworld.in
Table
Employee
©Topperworld
SQL
• 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.
...
• 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.
©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:
...
"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.
);
©Topperworld
SQL
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
Now you have the STUDENTS table available in your database and you can use
to store required information related to students.
✓ 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
FROM Employee;
The following query creates a PRIMARY KEY on the "D" column when the
"Employee" table is created.
• 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.
©Topperworld
SQL
Example:
First we verify STUDENTS table and then we would delete it from the database.
ID Int(11) NO PRI
NAME Varchar(20) NO
AGE Int(11) NO
This shows that STUDENTS table is available in the database, so we can drop it
as follows:
Now, use the following command to check whether table exists or not.
©Topperworld
SQL
• 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.
• But if you do not specify the WHERE condition it will remove all the rows
from the table.
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.
©Topperworld
SQL
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.
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
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:
©Topperworld
SQL
Table: Cars
⚫ After this statement, the table "Cars" will be changed into table name
"Car_2021_Details".
• 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 is faster and uses lesser resources than DELETE TABLE
command.
✓ 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.
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.
• 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
Example : In this example, we have a table called Cars with three columns:
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:
Table: Car_Details
©Topperworld
SQL
There are two types of temp tables based on the behavior and scope.
User id int,
©Topperworld
SQL
User id int,
• 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.
The above syntax only allows you to add a single column to the existing table.
©Topperworld