You are on page 1of 16

DDL Statements

Common DDL statements


CREATE define new entities
ALTER modify existing entities
DROP remove existing entities
CREATE statement
Used to create new entities in SQL Server including some of the most common entities
Database Procedure
Table Trigger
Default View
Index User
Login Role

CREATE DATABASE Sales ON ( NAME = Sales_dat, FILENAME = 'C:\Program Files\Microsoft SQL


Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\sales.mdf', SIZE = 10, MAXSIZE = 50, FILEGROWTH
=5)
LOG ON ( NAME = Sales_log, FILENAME = 'C:\Program Files\Microsoft SQL
Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\salelog.ldf', SIZE = 5MB, MAXSIZE = 25MB,
FILEGROWTH = 5MB ) ;
Create new table
USE SALES
GO

--Create new table called Products


CREATE TABLE dbo.Products1
(
ProductID int NULL,
ProductName varchar(20) NULL,
UnitPrice money NULL,
ProductDescription varchar(50) NULL
);
ALTER statement
Used to modify existing entities in SQL Server including
Database Trigger
Table View
Index User
Login Role
Procedure Schema

ALTER DATABASE Sales


Modify Name = SalesForecast ;
DROP statement
Used to delete existing entities in SQL Server including
Database Trigger
Table View
Index User
Login Role
Procedure Schema

DROP DATABASE SalesForecast


Demo
DML
Using INSERT to Add Data
The INSERT...VALUES statement inserts a
single row by default
INSERT
INSERT INTO
INTO Sales.OrderDetails(
Sales.OrderDetails(
orderid,
orderid, productid,
productid, unitprice,
unitprice, qty,
qty, discount)
discount)
VALUES(12000,39,18,2,0.05);
VALUES(12000,39,18,2,0.05);

Table and row constructors add multi-row


capability to INSERT...VALUES
INSERT INTO Sales.OrderDetails(
Sales.OrderDetails(
orderid,
orderid, productid,
productid, unitprice,
unitprice, qty,
qty, discount)
discount)
VALUES
VALUES
(12001,39,18,2,0.05),
(12002,39,18,5,0.10);
Using INSERT with SELECT and EXEC
INSERT...SELECT is used to insert the result set
of a query
INSERT
INSERT INTO
into an existing table
INTO Sales.OrderHist(
Sales.OrderHist(
orderid,custid,empid,orderdate)
orderid,custid,empid,orderdate)
SELECT
SELECT orderid,custid,empid,orderdate
orderid,custid,empid,orderdate
FROM
FROM Sales.Orders
Sales.Orders
WHERE
WHERE orderdate
orderdate << '20080101';
'20080101';

INSERT...EXEC is used to insert the result of a


stored procedure or dynamic SQL expression
INSERT
into anINTO
INSERT INTO dbo.T1
dbo.T1
existing (productid,
(productid, productname,
table productname,
unitprice)
unitprice)
EXEC
EXEC Production.ProdsByCategory
Production.ProdsByCategory
@numrows
@numrows = 5,5, @catid=1;
@catid=1;
Using SELECT isINTO
SELECT...INTO similar to
INSERT...SELECT but SELECT...INTO
creates a new table each time the
statement is executed
Copies column names, data types, and
nullability
Does not copy constraints or indexes
SELECT orderid,
orderid, custid,
custid, empid,
empid, orderdate,
orderdate,
shippeddate
shippeddate
INTO Sales.OrderArchive
Sales.OrderArchive
FROM Sales.Orders
Sales.Orders
WHERE orderdate < < '20080101';
'20080101';
Using UPDATE
Updates all rowsto
in aModify Data
table or view
Set can be filtered with a WHERE clause
Set can be defined with a JOIN clause

Only columns specified in the SET clause


are modified

UPDATE
UPDATE Production.Products
Production.Products
SET
SET unitprice
unitprice =
= (unitprice
(unitprice ** 1.04)
1.04)
WHERE
WHERE categoryid
categoryid == 1 AND
AND discontinued
discontinued =
= 0;
0;
Using DELETE
DELETE to Remove
without a WHERE Data
clause deletes all rows

DELETE
DELETE FROM
FROM dbo.Nums;
dbo.Nums;

Use a WHERE clause to delete specific rows


DELETE FROM
FROM Sales.OrderDetails
Sales.OrderDetails
WHERE orderid =
= 10248;
10248;
Using TRUNCATE TABLE to Remove
Data Storage physically deallocated, rows not individually
TRUNCATE TABLE clears the entire table

removed
Minimally logged
Can be rolled back if TRUNCATE issued within a
transaction
TRUNCATE TABLE will fail if the table is
referenced by a foreign key constraint in
another table TABLE
TRUNCATE
TRUNCATE TABLE dbo.Nums;
dbo.Nums;
Using IDENTITY
IDENTITY property of a column generates
sequential numbers automatically for insertion
into a table
Can specify optional seed and increment values
Only one column in a table may have IDENTITY
property defined
IDENTITY column omitted in INSERT statements
Functions provided
CREATE
CREATE TABLE to return last generated
TABLE Production.Products(
Production.Products(
values
productid int
int IDENTITY(1,1)
IDENTITY(1,1) NOT
NOT NULL,
NULL,
productname nvarchar(40)
nvarchar(40) NOT
NOT NULL,
NULL,
categoryid int
int NOT
NOT NULL,
NULL,
unitprice money
money NOT
NOT NULL)
NULL)

You might also like