Sunday 24 July 2011

Create Database And Tables in SQL Server


The CREATE DATABASE statement is used to create a database.

Syntax:

CREATE DATABASE database_name

Example:

Now we want to create a database called "my_Database", We use the following CREATE DATABASE statement:
CREATE DATABASE my_Database


SQL ALTER DATABASE

For More Details follow these links:

http://msdn.microsoft.com/en-us/library/ms174269.aspx

http://msdn.microsoft.com/en-us/library/aa275464(v=sql.80).aspx


SQL DROP DATABASE

The DROP DATABASE statement is used to delete a database.

Example:

DROP DATABASE database_name



SQL CREATE TABLE

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

Syntax:

CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
* data_type : Specify Column Datatype (ie, int,varchar(n) etc.)

SQL CREATE TABLE Example

Now we want to create a table called "Persons" that contains five columns: P_Id, LastName, FirstName, Address, and City.
We use the following CREATE TABLE statement:
CREATE TABLE Persons
(
P_Id int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

The P_Id column is of type int and will hold a number. The LastName, FirstName, Address, and City columns are of type varchar with a maximum length of 255 characters.
Note: You can see the table created by select * from Persons Statement.
The empty "Persons" table will now look like this:
P_Id
LastName
FirstName
Address
City






SQL DROP TABLE Statement

The DROP TABLE statement is used to delete a table.
Syntex:
DROP TABLE table_name

Example:

DROP TABLE Persons


SQL TRUNCATE TABLE Statement

What if we only want to delete the data inside the table, and not the table itself?
Then, use the TRUNCATE TABLE statement:
Syntex:
TRUNCATE TABLE table_name

Example:

TRUNCATE TABLE Persons

No comments:

Post a Comment