×

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 Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

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 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.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 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 Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

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 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 Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

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...

3 minutes read.