×

SQL TABLE

SQL TABLE

Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as tuples, where each row in the table is cited as a tuple. There are certain operations that can be carried out on these SQL tables. Some of them are listed below.

SQL TEMP TABLE

The temporary table concept was initiated in the SQL server. Developers use temporary tables, and it helps them in a number of ways.

Temporary tables can perform all types of operations that a normal table is capable of doing. These tables are generated in the tempdb database, and they can be developed during runtime.

The notion of temporary tables is only supported by MySQL versions 3.23 and above. However, in older versions, there was the concept of heap tables.

Temporary tables can be diverged into two types based on their behaviour and scope.

  1. Local temporary table
  2. Global temporary table

Local temporary table

A Local Temporary Table is available during the present connection time only, and they are deleted automatically once the user disconnects. This type of table is started with a hash (#) symbol.

CREATE TABLE #local table (
 user_id int,
 user_name varchar (100),
 user_addrs varchar (150)
 ); 

The following is an instance of generating a local temporary table.

Global temporary table

A Global temporary table is initiated by a double hash (##) symbol. This type of table does not get deleted and is present for all users. It behaves like a permanent table.

CREATE TABLE #global table (
 user_id int,
 user_name varchar (100),
 user_addrs varchar (150)
 ); 

The following is an instance of generating a global temporary table.

Deleting temporary table

A temporary table can be deleted in two ways. It can be deleted automatically as well as manually.

A local temporary table is inevitably deleted immediately after the user disconnects from the server.

The temporary table can be deleted manually as well by using the DROP TABLE command.

DROP TABLE #tablename

The following is the instance for deleting a temporary table. It is the same as deleting a regular table.

SQL CLONE TABLE

It is possible to replicate or clone one table from another SQL table in the same server. This is done by making use of the SELECT statement.

SELECT *
 INTO <new_table>
 FROM <old_table>; 

The following is the syntax for generating the copy of one table from another table.

The above statement will copy all the content of the old table into the new table.

The following is the syntax to copy specific columns from the old table to the new table.

SELECT column1, column2, column3, …
 INTO <new_table>
 FROM <old_table>; 

The WHERE clause can also be used with the above statements, and certain conditions can be specified as well. Also, new column names can be given using the AS clause.

Example:

Let’s consider the following Source_table.

IDFnameLnameProjectIDEmailProfileCity
1HarryKaneA1harry@abc.comSESKolkata
2RonWesleyB2ron@efg.comSDEMumbai
3DobbySaneC3dobby@hij.comSDEPune
4AlbusDolbyD4albus@klm.comHRAgra
5SnapeWrightE5snape@nop.comSDEDelhi

Query:

CREATE TABLE Contact LIKE Source_table;

The following query will create an empty structure with the same attributes as that of the Source_table.

Output:

IDFnameLnameProjectIDEmailProfileCity

Query:

INSERT INTO Contact SELECT *
 FROM Source_table; 

Now, the following query will clone all the contents of the Source_table into the new Contact table.

Output:

IDFnameLnameProjectIDEmailProfileCity
1HarryKaneA1harry@abc.comSESKolkata
2RonWesleyB2ron@efg.comSDEMumbai
3DobbySaneC3dobby@hij.comSDEPune
4AlbusDolbyD4albus@klm.comHRAgra
5SnapeWrightE5snape@nop.comSDEDelhi

Creating a clone table helps in various database operations like testing since this table does not affect the records of the original table. Hence, the data in the original table remains intact.

These are some of the advanced SQL TABLE statements which are used by developers to deal with the tables in the database. These statements are essential and should be handled carefully.

These statements make database operations like testing, storing data temporarily and all much easier and faster.


Related Topics

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.