Before you learn how to create various
objects within the database, it is important to review the general
constructs of the T-SQL language. T-SQL is Microsoft’s version of the
Structured Query Language (SQL) programming language. SQL is a
declarative programming language, which means that the SQL code
describes what information should be returned, or what the end goal is,
as opposed to how to go about retrieving information or doing some
work. The SQL language contains categories of statements. These
categories are Data Definition Language (DDL), Data Manipulation
Language (DML), and Data Control Language (DCL).
Data Definition Language (DDL)
DDL statements describe the creation or
modification of objects within the database server. In our previous
example using SSMS, you noticed that when you created the
SmartCommunityBank database, SSMS really executed a T-SQL script using
the CREATE DATABASE
DDL statement. The script that SSMS
generates is sometimes more verbose than what is required at a minimum.
For example, the minimum code that is needed to create a database is as
follows:
USE [master]
GO
CREATE DATABASE [VetClinic]
GO
This will create a database named VetClinic
using default values .
DDL statements also deal with the modification of objects. An example of modifying an existing object using the ALTER
DDL statement is as follows:
USE [master]
GO
ALTER DATABASE VetClinic
SET RECOVERY FULL
GO
The previous example changes the database recovery mode to FULL
for an existing database.
DDL statements also apply to the creation of
security principals and objects. For example, if you wanted to create a
new SQL Server login named ReceptionistUser
, you would execute the following script:
USE [master]
GO
CREATE LOGIN ReceptionistUser WITH PASSWORD='hj2(*h2hBM!@jsx'
GO
Data Manipulation Language (DML)
DML statements read and modify the actual data
within the database. For example, assume you created the following
table using DDL statements within the VetClinic database:
USE [VetClinic]
GO
CREATE TABLE [Pets]
(pet_id INT PRIMARY KEY,
pet_name VARCHAR(50) NOT NULL,
pet_weight INT NOT NULL)
GO
Now, let’s add some data to the Pets table using the INSERT
DML statement. The code follows:
USE [VetClinic]
GO
INSERT INTO Pets VALUES
(1,'Zeus',185),
(2,'Lady',155),
(3,'Deno',50)
GO
If you want to query the data, you can use the SELECT
DML statement as follows:
SELECT * FROM Pets
If you execute this statement within SSMS, it
will return the three pets defined previously in the results grid.
Other DML statements include UPDATE
and DELETE
, which are two important actions to perform against data.
Data Control Language (DCL)
DCL statements control access to data. For example, if you wanted to give SELECT
access to ReceptionistUser
, you could use the GRANT
DCL statement as follows:
USE [VetClinic]
GO
GRANT SELECT ON Pets TO ReceptionistUser
GO
Other DCL statements include REVOKE
and DENY
. These are used to either remove a previously granted permission or deny someone access to a particular object. Note that DENY
takes precedence over GRANT
at a higher scope. For example, ReceptionistUser
is granted SELECT
on the Pets
table. This enables ReceptionistUser
to read all the columns within the table. The administration could DENY
that user specific access to a column, and even though ReceptionistUser
has SELECT
access for the entire table, the specific column would not be available.