0% found this document useful (0 votes)
3 views6 pages

055 CreateTABLE S AndTempTable S

The document explains the concept of tables and temporary tables in databases, detailing their structure and usage. It outlines the CREATE TABLE statement syntax, including specifications for primary keys, data types, and constraints. Additionally, it describes temporary tables, their purpose, and methods for creating them in SQL Server.

Uploaded by

muzeemusicplay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

055 CreateTABLE S AndTempTable S

The document explains the concept of tables and temporary tables in databases, detailing their structure and usage. It outlines the CREATE TABLE statement syntax, including specifications for primary keys, data types, and constraints. Additionally, it describes temporary tables, their purpose, and methods for creating them in SQL Server.

Uploaded by

muzeemusicplay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Create TABLE(s) and Temp Table(s)

What is Table ?
• Tables are database objects that contain all the data in a database. .

• In tables, data is logically organized in a row-and-column format.

• Each row represents a unique record.

• Each column represents a field in the record.

• With up to 1024 columns in each table.


CREATE TABLE Statement
CREATE TABLE [database_name.][schema_name.]table_name
( pk_column data_type PRIMARY KEY,
column_1 data_type NOT NULL,
column_2 data_type,
...,
table_constraints );

• Specify the name of the database in which the table is created.

• Specify the schema to which the new table belongs.

• Specify the name of the new table.

• Each table should have a primary key which consists of one or more columns. Typically, you list the primary key columns first and then other
columns.

• If the primary key contains only one column, you can use the PRIMARY KEY keywords after the column name. If the primary key consists of
two or more columns, you need to specify the PRIMARY KEY constraint as a table constraint.

• Each column has an associated data type specified after its name in the statement.

• A column may have one or more column constraints such as NOT NULL and UNIQUE.

• A table may have some constraints specified in the table constraints section such as FOREIGN KEY, PRIMARY KEY, UNIQUE and CHECK.
CREATE table example
• CREATE TABLE sales.visits (

visit_id INT PRIMARY KEY IDENTITY (1, 1),

first_name VARCHAR (50) NOT NULL,

last_name VARCHAR (50) NOT NULL,

visited_at DATETIME, phone VARCHAR(20),

store_id INT NOT NULL,

FOREIGN KEY (store_id) REFERENCES sales.stores (store_id) );


What is Temporary (temp) Table
• A temporary table in SQL Server, as the name suggests, is a database table that exists temporarily on the
database server.

• A temporary table stores a subset of data from a normal table for a certain period of time.

• Temporary tables are particularly useful when you have a large number of records in a table and you repeatedly need
to interact with a small subset of those records.

• In such cases instead of filtering the data again and again to fetch the subset, you can filter the data once and store it
in a temporary table.

• Temporary tables are stored inside “tempdb” which is a system database.


Create Temporary (temp) Table
• The name of a temporary table must start with a hash (#).
• Method 1.select BusinessEntityID,firstname,lastname
into #TempPersonTable

from [Person].[Person] where title = 'mr. ‘

• Method 2. Create table #TempPersonTable (


BusinessEntityID int, Firstname nvarchar(50), lastname nvarchar(50) )
INSERT INTO #TempPersonTable
SELECT BusinessEntityID,FirstName,LastName
from [Person].[Person]
where title = 'mr.'

You might also like