0% found this document useful (0 votes)
9 views

SQL and Schema Definition

The document discusses SQL and its use for schema definition and data manipulation. It describes SQL's data definition language (DDL) and data manipulation language (DML). DDL is used to define database objects like tables and views, while DML allows querying, inserting, updating and deleting data. Examples of DDL statements like CREATE, ALTER and DROP are provided.

Uploaded by

09whitedevil90
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL and Schema Definition

The document discusses SQL and its use for schema definition and data manipulation. It describes SQL's data definition language (DDL) and data manipulation language (DML). DDL is used to define database objects like tables and views, while DML allows querying, inserting, updating and deleting data. Examples of DDL statements like CREATE, ALTER and DROP are provided.

Uploaded by

09whitedevil90
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

lOMoARcPSD|39225634

SQL and Schema Definition

Intro To Database Appl (Montgomery College)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by White Devil (09whitedevil90@gmail.com)
lOMoARcPSD|39225634

SQL: Schema Definition, Data Definition Language (DDL)


and Data Manipulation Language (DML)
SQL
SQL is a comprehensive database language: It has statements for data definitions, queries, and updates.
It is both a DDL and a DML.

SQL Data Definition and Data Types


SQL uses the terms table, row and column for the formal relational model terms relation, tuple, and
attribute, respectively.
The main SQL command for data definition is the CREATE statement, which can be used to create
schemas, tables (relations), and domains (as well as other constructs such as views, and triggers).

Data Definition Language


Data definition language, which is usually part of a database management system, is used to define and
manage all attributes and properties of a database, including row layouts, column definitions, key
columns, file locations, and storage strategy.
A DDL statement supports the definition or declaration of database objects such as databases, tables,
and views

Most DDL statements take the following form:

CREATE object_name

ALTER object_name

DROP object_name

CREATE TABLE
USE Northwind
CREATE TABLE Importers ( CompanyID int NOT NULL, CompanyName varchar(40) NOT NULL, Contact
varchar(40) NOT NULL )

ALTER TABLE
The ALTER TABLE statement enables you to modify a table definition by altering, adding, or dropping
columns and constraints or by disabling or enabling constraints and triggers.

The following statement will alter the Importers table in the Northwind database by adding a column
named ContactTitle to the table.
USE Northwind
ALTER TABLE Importers
ADD ContactTitle varchar(20) NULL

Downloaded by White Devil (09whitedevil90@gmail.com)


lOMoARcPSD|39225634

DROP TABLE
The DROP TABLE statement removes a table definition and all data, indexes, triggers, constraints, and
permission specifications for that table.

Any view or stored procedure that references the dropped table must be explicitly dropped by using the
DROP VIEW or DROP PROCEDURE statement.

The following statement drops the Importers table from the Northwind database.

USE Northwind DROP TABLE Importers

Data Control Language


Data control language is used to control permission on database objects. Permissions are controlled by
using the SQL-92 GRANT and REVOKE statements and the Transact-SQL DENY statement.

GRANT
The GRANT statement creates an entry in the security system that enables a user in the current database
to work with data in that database or to execute specific Transact-SQL statements.

The following statement grants the Public role SELECT permission on the Customers table in the
Northwind database:

USE Northwind
GRANT SELECT ON Customers TO PUBLIC

REVOKE
The REVOKE statement removes a previously granted or denied permission from a user in the current
database.
The following statement revokes the SELECT permission from the Public role for the Customers table in
the Northwind database:

USE Northwind
REVOKE SELECT ON Customers TO PUBLIC

DENY

The DENY statement creates an entry in the security system that denies a permission from a security
account in the current database and prevents the security account from inheriting the permission
through its group or role memberships.

USE Northwind
DENY SELECT ON Customers TO PUBLIC

Data Manipulation Language


Data manipulation language is used to select, insert, update, and delete in the objects defined with DDL.

SELECT
The SELECT statement retrieves rows from the database and enables the selection of one or many rows

Downloaded by White Devil (09whitedevil90@gmail.com)


lOMoARcPSD|39225634

or columns from one or many tables. SELECT statement, sometimes called a mapping or a select-from-
where block, is formed of the three clauses SELECT, FROM, and WHERE and has the following form

SELECT <attribute list> FROM <table list> WHERE <condition>

Where <attribute list> is a list of attribute names whose values are to be retrieved by the query.

<table list> is a list of the relation names required to process the query.

<condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query.

QUERY 1: Retrieve the birth date and address of the employee(s)


whose name is ‘John B. Smith’.
SELECT Bdate, Address
FROM EMPLOYEE
WHERE Fname = ‘John’ AND Minit = ‘B’ AND Lname = ‘Smith’;

QUERY 2: Retrieve the name and address of all employees who work
for the 'Research' department.
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND DNUMBER=DNO

QUERY 3: For every project located in 'Stafford', list the project number,
the controlling department number, and the department manager's
last name, address, and birthdate.
SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford'

QUERY 4: The following statement retrieves the CustomerID,


CompanyName, and ContactName data for companies who have a
CustomerID value equal to alfki or anatr. The result is ordered
according to the ContactName value:
USE Northwind
SELECT CustomerID, CompanyName, ContactName
FROM Customers
WHERE (CustomerID = ‘alfki’ OR CustomerID =‘anatr’)
ORDER BY ContactName

Downloaded by White Devil (09whitedevil90@gmail.com)

You might also like