SQL Server Create Table

By
Create Table in Sql Server
There are normally two way to create a table in SQL Server Database. One is with the SQL Server wizard and another is through query. I will discuss last method (SQL Server query) here.
In SQL Server table is just like a container which contains the data. Table are in organized way and it have rows and columns format.In SQL Server without table you can not assume the SQL Server because if table is not exist then there are no need of procedure, function , trigger etc. So in this article we discuss how to create a table . All query is tested in SQL Server 2008 version.
Before write down the query for create a table one thing i do first just understand the syntax of create table
The syntax for the CREATE TABLE statement in SQL Server (Transact-SQL) is given below:

Create table SQL Server/ SQL Server table

CREATE TABLE table_name
(
column1 datatype [ NULL | NOT NULL ] DEFAULT(Value),
column2 datatype [ NULL | NOT NULL ] DEFAULT(Value),
column3 datatype [ NULL | NOT NULL ] DEFAULT(Value)
);

Explanation Of TableSyntax

Now lets understand what the parameter meaning in above syntax
1. table_name : its the name of table that you want to create.Means table is created with this name
2. column1, column2, column3 : its basically the column Name that you want to create in table it could be more than three and so on
3. datatype: Each column must have the datatype we will discuss datatype in SQL server later. There are so many datatype in SQL Server like varchar,int, bit etc
4. [ NULL | NOT NULL ] : The column should either be defined as NULL or NOT NULL and if this value is left blank, the database assumes NULL as the default.
5.DEFAULT(Value) : This is optional in table if you set the default value then it store the default
value rather then null value when you not insert any data in this column.
Let's look at an example of how to use the CREATE TABLE statement in SQL Server .

Create table Query

CREATE TABLE quiz25
(
quiz_id INT NOT NULL,
question VARCHAR(50) NOT NULL,
answer VARCHAR(50),
totalQuestion int Default(10)
);

Explanation of create table query

This SQL Server CREATE TABLE example creates a table called quiz25 which has 4 columns.
The first column is called quiz_id  which is created as an INT datatype and can not contain NULL values.
The second column is called question  which is a VARCHAR datatype (50 maximum characters in length) and also can not contain NULL values.
The third column is called answer  which is a VARCHAR datatype but can contain NULL values.
The fourth column is called totalQuestion  which is a again int datatype which can contain NULL values but i set the default value 10 thats why it
insert 10 .

Now the only problem with this CREATE TABLE statement is that you have not defined a primary key for the table in SQL Server. We could modify

this CREATE TABLE statement and define the quiz_id  as the primary key as
follows:
you have to chnage above line like quiz_id INT NOT NULL  PRIMARY KEY

0 comments:

Post a Comment