Click here to Skip to main content
15,880,469 members
Articles / Database Development / SQL Server / SQL Server 2008

Different Ways to Create a Table and Insert Data into the Table

Rate me:
Please Sign up or sign in to vote.
4.54/5 (6 votes)
17 Nov 2010CPOL2 min read 84.3K   5   8
Basic information about the different ways of creating a table

Tables can be defined as the structure which contains the data in the database. There can be many ways to create table and to insert data into the table. Some of the ways to create a table are given below.

1. Creation of a Table with the Help of a Create Statement

For example, suppose we want to create a table called tbl_student, then the syntax of creating this table is given below:

SQL
CREATE TABLE tbl_Students( [Studentid] [int] IDENTITY(1,1) NOT NULL, 
    [Firstname] [nvarchar](200) , [Lastname] [nvarchar](200) , [Email] [nvarchar](100) )

Let us suppose we want to insert data into the table tbl_students, then we can use the insert statements to insert data into the table.

The insert statement used for inserting the data is given below:

SQL
Insert into tbl_students(Firstname,lastname,email) select 'Vivek','Johari',
    'Vivek@abc.com' union all select 'Pankaj','Kumar',
    'Pankaj@abc.com' union all Select 'Amit','Singh','amit@abc.com'

Here, we can insert multiple rows into the table with the help of a single query mentioned above.

Another way to insert the values into the table tbl_students is:

SQL
insert into tbl_students(Firstname,lastname,
    email) Values('Manish','Kumar',
    'manish@abc.com') Insert into tbl_students(Firstname,
    lastname,email) Values('Abhishek','Singh',
    'abhishek@abc.com') Insert into tbl_students(Firstname,
    lastname,email) Values ('Uma','Sharma','Uma@abc.com')

Here, whenever we need to insert a row into the table, we have to use a separate insert statement.

The result of the above SQL queries can be verified with the help of the following query:

SQL
SELECT * FROM tbl_students

Result:

Note: We don't need to insert data into the column Studentid since it is defined as the Identity column and whenever a record is inserted into the table, SQL Server automatically inserts value into this column.

2. Creation of Table with the Help of Another Table

2.1. Suppose we want to create a table tbl_studentinfo which contains a subset of the columns (studentid, Firstname, Lastname) of the table tbl_student, then we can use the following query. Select studentid, Firstname, Lastname into tbl_studentinfo from tbl_students.

This query will create a table tbl_studentinfo having columns studentid, Firstname, Lastname and it contains data of these columns from the table tbl_students. This can be verified with the help of the query given below:

SQL
SELECT * FROM tbl_studentinfo

Result:

2.2. Suppose we want to create a table which is exactly the copy of a given table, then we can also use the following SQL query:

SQL
SELECT * INTO tbl_studentscopy FROM tbl_students

The table tbl_studentscopy created by the above SQL query will contain exactly the same data as tbl_students. This can be verified with the help of the query given below:

SQL
SELECT * FROM tbl_studentscopy

Result:

There is another interesting way to insert the values into a table with the help of another table. For example, suppose we have a table named as tbl_Studentsdemo whose structure is given below:

SQL
CREATE TABLE tbl_Studentsdemo( [Studentid] [int] IDENTITY(1,
    1) NOT NULL, [Firstname] [nvarchar](200) , [Lastname] [nvarchar](200) ,
    [Email] [nvarchar](100) )

Now if we want to insert values into the table from the table tbl_students, then we can use the following SQL query to insert the data into the table.

SQL
Insert into tbl_Studentsdemo(Firstname,lastname,
    email) SELECT Firstname,lastname,email FROM tbl_students

The above query will insert all the data from the table tbl_students into the table tbl_Studentsdemo. Again we can verify it with the help of the given query:

SQL
SELECT * FROM tbl_Studentsdemo

Result:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Database Administrator
India India
I am currently working as a Senior DBA and have around 11 years of experience in database.

Degree:-
Master Degree in Computer(MCA)

Work experience:-
Designing of the database.
Database Optimization.
Writing Complex Stored Procedures,Functions,Triggers etc.
Designing and developing SSIS & DTS packages.
Designing SQL Reports using SSRS.
Database Server Maintenance.

Certification:-
Microsoft certified Sql DBA in Sql server 2008 (MCTS).
Microsoft certified BI professional in Sql server 2008 (MCTS).
Oracle certified profession DBA in ORACLE 10g (OCP)
certified profession DBA in ORACLE 9i (OCP)

My other publication
Technical Blog:- Technologies with Vivek Johari

Moderator and Blogger at BeyondRelational.com

Guest Author and Blogger at sqlservercentral.com

Comments and Discussions

 
GeneralMy vote of 4 Pin
Member 1185523422-Jul-15 3:57
Member 1185523422-Jul-15 3:57 
GeneralRe: My vote of 4 Pin
Vivek Johari27-Nov-18 22:09
Vivek Johari27-Nov-18 22:09 
Generalthanks for sharing - have 5 Pin
Pranay Rana24-Jan-11 17:38
professionalPranay Rana24-Jan-11 17:38 
thanks for sharing

GeneralRe: thanks for sharing - have 5 Pin
Vivek Johari26-Feb-11 5:20
Vivek Johari26-Feb-11 5:20 
GeneralFlaw in the Second Method Pin
tarun_j20017-Nov-10 20:02
tarun_j20017-Nov-10 20:02 
GeneralRe: Flaw in the Second Method Pin
Vivek Johari2-Dec-10 16:36
Vivek Johari2-Dec-10 16:36 
GeneralMy vote of 5 Pin
Avinash C Dubey17-Nov-10 6:41
Avinash C Dubey17-Nov-10 6:41 
GeneralRe: My vote of 5 Pin
Vivek Johari17-Nov-10 6:57
Vivek Johari17-Nov-10 6:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.