SQL and Schema Definition
SQL and Schema Definition
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
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.
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
SELECT
The SELECT statement retrieves rows from the database and enables the selection of one or many rows
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
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 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'