T-SQL Tutorial

T-SQL CREATE


This article describes how to create and defines objects in a database.
The CREATE statement is part of DDL(Data Definition Language) statements.
Uses CREATE statement to define new objects like: database, schema, user, table, temporary table, view, index, trigger, procedure.



CREATE DATABASE

The T-SQL statement CREATE DATABASE is used to create a SQL Server database.

CREATE DATABASE database_name;

CREATE SCHEMA

The T-SQL statement CREATE SCHEMA is used to create a new schema in the current database.

CREATE SCHEMA schema_name;
CREATE SCHEMA schema_name AUTHORIZATION [user_name];


CREATE USER

The T-SQL statement CREATE USER is used to create a new user in the current database.

CREATE USER user_name;
CREATE USER user_name WITH PASSWORD = user_pass;


CREATE TABLE

The T-SQL statement CREATE TABLE is used to create a new table in SQL Server database.

CREATE TABLE new_table_name(
col_1 datatype,
col_2 datatype,
....
col_n datatype);


CREATE TEMP TABLE

To create a temporary table uses the CREATE TABLE statement.

CREATE TABLE #table_name(
col_1 datatype,
col_2 datatype,
....
col_n datatype);


CREATE VIEW

To define a view in SQL Server database uses CREATE VIEW statement.

CREATE VIEW view_name
AS
SQL statement


CREATE INDEX

The command CREATE INDEX is used to create a index on table column.

CREATE INDEX index_name ON table_name (column_name);

CREATE TRIGGER

To create a trigger is used the command CREATE TRIGGER.

CREATE TRIGGER trigger_name
ON { table_name | view_name }
{ FOR | AFTER | INSTEAD OF }
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
AS { sql_statement [ ; ] }


CREATE PROCEDURE

To create a procedure is used the DDL statement CREATE PROCEDURE.

CREATE PROCEDURE procedure_name
AS
SQL statement


CREATE LOGIN

To create a logins uses DDL statement CREATE LOGIN.

CREATE LOGIN my_login_name WITH PASSWORD='my_login_password';