RSS Feed

Create Table

SQL Create Table

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

Suppose we want to create a table called "Employees" that contains five columns: Emp_Id, LastName, FirstName, Address, and City:

CREATE TABLE statement:

CREATE TABLE Employees
(
Emp_Id int,
LastName varchar(100),
FirstName varchar(100),
Address varchar(200),
City varchar(255)
)


Some other example of create table in a different way:

1.
CREATE TABLE Employees_Duplicate
AS (SELECT *
FROM Employees
WHERE id > 500);

This will create a new table called Employees_Duplicate that will include all columns from the Employees table and all rows fetched by the select statement.

2.
CREATE TABLE Employees
AS (SELECT * FROM customers WHERE 1=2);

This will create a new table called Employees that include all columns from the customers table, but no data from the customers table.

3.
CREATE TABLE Employees_New
AS (SELECT Employees.id, Employees.address, customers.cust_type
FROM Employees, customers
WHERE Employees.id = customers.id
AND Employees.id > 500);

This would create a new table called Employees_New based on columns from both the Employees and customers tables.

No comments:

Post a Comment