0% found this document useful (0 votes)
318 views11 pages

SQL CREATE TABLE Statement

The document provides information on how to create a table in SQL using the CREATE TABLE statement. It explains the basic syntax for CREATE TABLE, which includes specifying the table name and defining each column name and data type. An example is given that creates a table called "Persons" with columns for PersonID, LastName, FirstName, Address, and City. Additional tips and examples are given on creating a table that copies data from an existing table using the AS keyword, and an exercise is included to test writing the SQL statement to create the Persons table.

Uploaded by

kerya ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
318 views11 pages

SQL CREATE TABLE Statement

The document provides information on how to create a table in SQL using the CREATE TABLE statement. It explains the basic syntax for CREATE TABLE, which includes specifying the table name and defining each column name and data type. An example is given that creates a table called "Persons" with columns for PersonID, LastName, FirstName, Address, and City. Additional tips and examples are given on creating a table that copies data from an existing table using the AS keyword, and an exercise is included to test writing the SQL statement to create the Persons table.

Uploaded by

kerya ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

SQL CREATE TABLE Statement

The SQL CREATE TABLE Statement


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

Syntax

CREATE TABLE table_name (


     column1 datatype,
     column2 datatype,
     column3 datatype,
   ....
);

The column parameters specify the names of the columns of the table.

The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date,
etc.).

Tip: For an overview of the available data types, go to our complete Data Types Reference.

SQL CREATE TABLE Example


The following example creates a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City:

Example

CREATE TABLE Persons (


    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255)
);

The PersonID column is of type int and will hold an integer.


The LastName, FirstName, Address, and City columns are of type varchar and will hold
characters, and the maximum length for these fields is 255 characters.

The empty "Persons" table will now look like this:

PersonID LastName FirstName Address City


         

Tip: The empty "Persons" table can now be filled with data with the SQL INSERT INTO
statement.

Create Table Using Another Table


A copy of an existing table can also be created using CREATE TABLE.

The new table gets the same column definitions. All columns or specific columns can be
selected.

If you create a new table using an existing table, the new table will be filled with the existing
values from the old table.

Syntax

CREATE TABLE new_table_name AS


    SELECT column1, column2,...
    FROM existing_table_name
    WHERE ....;

The following SQL creates a new table called "TestTables" (which is a copy of the "Customers"
table): 

Example

CREATE TABLE TestTable AS


SELECT customername, contactname
FROM customers;

Test Yourself With Exercises


Exercise:
Write the correct SQL statement to create a new table called Persons.

(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

Start the Exercise

Introduction to the SQL Server CREATE TABLE statement


Tables are used to store data in the database. Tables are uniquely named within a database and
schema. Each table contains one or more columns. And each column has an associated data type
that defines the kind of data it can store e.g., numbers, strings, or temporal data.

To create a new table, you use the CREATE TABLE statement as follows:

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
);
Code language: SQL (Structured Query Language) (sql)

In this syntax:

 First, specify the name of the database in which the table is created. The database_name
must be the name of an existing database. If you don’t specify it, the database_name
defaults to the current database.
 Second, specify the schema to which the new table belongs.
 Third, specify the name of the new table.
 Fourth, 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.
 Fifth, a table may have some constraints specified in the table constraints section such as
FOREIGN KEY, PRIMARY KEY, UNIQUE and CHECK.

Note that CREATE TABLE is complex and has more options than the syntax above. We will
gradually introduce you to each individual options in the subsequent tutorials.

SQL Server CREATE TABLE example


The following statement creates a new table named sales.visits to track the customer in-store
visits:

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)
);
Code language: SQL (Structured Query Language) (sql)

In this example:

Because we do not specify the name of the database explicitly in which the table is created, the
visits table is created in the BikeStores database. For the schema, we specify it explicitly,
therefore, the visits table is created in the sales schema.

The visits table contains six columns:

 The visit_id column is the primary key column of the table. The IDENTITY(1,1)
instructs SQL Server to automatically generate integer numbers for the column starting
from one and increasing by one for each new row.
 The first_name and last_name columns are character string columns with VARCHAR
type. These columns can store up to 50 characters.
 The visited_at is a DATETIME column that records the date and time at which the
customer visits the store.
 The phone column is a varying character string column which accepts NULL.
 The store_id column stores the identification numbers which identify the store where
the customer visited.
 At the end of the table’s definition is a FOREIGN KEY constraint. This foreign key ensures
that the values in the store_id column of the visits table must be available in the
store_id column in the  stores table. You will learn more about the FOREIGN KEY
constraint in the next tutorial.

In this tutorial, you have learned how to use the SQL Server CREATE TABLE statement to create a
new table in a database.

How to Create a Table and Insert Data in


SQL
 Post author

 By Dynamic Web Training


 Post date

 December 12, 2019

If you know one or two things in about using a computer, you definitely might have heard about
the term SQL. But maybe you don’t know what that is, and you have never bothered to
understand it. Well, this is not rocket science, especially for you, but a person who is utterly
green about technology it probably is. So first of all, what is the meaning of SQL? TO start with
the initials SQL stand for Structured Query Language.
A not so detailed definition of SQL is that this is what is used when communicating to a
database. Again, what is a database? A database is an electronic system where one can easily
access data. Not only it obtains the data, but also the user can also manipulate it as well as update
it. Enterprises use databases to store, manage, and retrieve information.

The new-age database is maintained by what we know as a DBMS, which stands for the database
management system. Examples of databases comprise of Microsoft Access, FileMaker, FoxPro,
Oracle, Clipper, dBASE, RDBMS, PostgreSQL, MySQL, and SQL Server. The simplest
definition of what a database is a gathering of information, professionally called data, which is
stored in a server.

Therefore SQL, according to the American National Standards Institute, ANSI, and SQL, is the
standard language for the database management system to relate. An SQL statement is used to
undertake tasks like updating the data on the existing database, retrieving that data from the
database and equally, managing it.
Application of SQL

As said earlier, SQL has many uses, including data integration script, analytical queries,
retrieving information, and so many other applications.

Data integration script

SQL is used to write data integration scripts by a database administrator or a developer

Analytical queries

Data analysts use structured question language for setting as well as running analytical questions
all the time.

Retrieve data

A database administrator uses SQL to retrieve a subset of the data within a database for analytics
application and also processing transactions. Commonly used SQL elements include insert,
select, delete, add, create, alter, and truncate.

Important SQL applications

SQL modifies the index structures as well as a database table. Also, the administrator can add,
delete, and update the rows of the information using SQL.

Benefits of SQL

There is a myriad of benefits of why one should use SQL here are just a few reasons and
benefits.

SQL Standard
The first standard for this language was developed in 1986; the International National Standards
followed suite in 1987. Today we use the latest standard that was established in 2011.

Portable

It runs on PCs, mainframes, servers, and mobile devices. This language also runs in local
systems, the internet, and the intranet. SQL databases can be comfortably moved from a system
to another without any compilations.

Open Source

SQL is open source, meaning that you can use it at a low cost with large communities.

It is easy to learn and use

This language consists of English statements, meaning that you can quickly learn and use it.
Writing an SQL query has never been easier.

It is a high-value skill to have

Many jobs such as IT support, business data analysis, and web development require a candidate
who is good at SQL. It is the reason we want to teach you how you can create a table and insert
data in the Structured Query Language.

Creating a Table in SQL and Inserting DATA

First of all, there are various ways of creating tables and inserting data in SQL. To start with,

One can create a table using the create statement; for instance, you want to create a table
titled pupils. Use the syntax below.
CREATE TABLE PUPILS (
Pupilid int IDENTITY (1.1) NOT NULL,
Firstname varchar (200),
Lastname varchar (200),
Email varchar (100)
)

Therefore, this syntax will get you the table Pupil, where the pupil id is not null. If you want to
insert the data into that table titled ‘Pupil’ here is what you do using the first method.

Insert into Pupils,


(
Pupilid, Firstname, Lastname, email
)
Values
(
1, ’Watson’, ’Keter, ’’ Watsonketer@gmail.com’
)

You can verify this query to get the following result

Student ID First Name Last Name Email


1 Watson Keter watsonketer@gmail.com

The second method is how you can insert values into the table using a different table. See below.

For instance, we already have a table named Pupils, and here we want to insert the table’s values
into the other table called Pupildemo.

Create table ‘Pupildemo’

CREATE TABLE Pupildemo (


Pupilid int IDENTITY (1.1) NOT NULL,
Firstname varchar (200),
Lastname varchar (200),
Email varchar (100)
)

At this juncture, to insert data of table ‘Pupils’ into the table ‘Pupildemo’, you can use the
following statement.
Insert into Pupildemo (
Pupilid, Firstname, Lastname, email
) Then,
SELECT
Pupilid,
Firstname,
Lastname,
email
from
Pupils

You can verify this statement results to get the results using this method.

SELECT, FROM Pupildemo, and you will get this result.

Student ID First Name Last Name Email


1 Watson Keter watsonketer@gmail.com

Note: For you to insert the data on one table to the other, the data type of the column needs to be
the same.

Rename the current table

Alter the name of ‘Pupildemo’ table to Pupilscopy

ALTER TABLE
Pupildemo RENAME TO Pupilscopy

Consider the table ‘Pupils’ that is given down here, and ADD column ‘Phone’ in that table.

Student ID First Name Last Name Email


1 Watson Keter watsonketer@gmail.com
2 Kimberly Jones kimjones@gmail.com
Here, you are required to use the ALTER TABLE command to add a column to the existing
table.

Here is what happens

ALTER TABLE
Pupils,
then,
ADD
Phone INT Null

This is what you get

Student ID First Name Last Name Email Phone


1 Watson Keter watsonketer@gmail.com Null
2 Kimberly Jones kimjones@gmail.com Null

Please Note –
If you use the ALTER TABLE statement to add another column to the existing table, and don’t
add values, you get a default Null on those columns. It is why the phone row has Null since we
did not add any phone data.

You might also like