Professional Documents
Culture Documents
************************************
Basic database manipulation
************************************
BikeStores Sample Database - create objects.sql – this file is for creating database objects
including schemas and tables.
BikeStores Sample Database - load data.sql – this file is for inserting data into the tables
*********************
Next class
*********************
10. SELECT first_name + ' ' + last_name AS full_name
FROM sales.customers
ORDER BY first_name;
11. SELECT category_name 'Product Category'
FROM production.categories;
12. CREATE TABLE sales.quotations (
quotation_no INT IDENTITY PRIMARY KEY,
valid_from DATE NOT NULL,
valid_to DATE NOT NULL );
13. ALTER TABLE sales.quotations
ADD description VARCHAR (255) NOT NULL;
amount DECIMAL (10, 2) NOT NULL,
customer_name VARCHAR (50) NOT NULL;
The above query adds description, amount and customer_name columns to the table
created in no 12.
WE CAN ALSO DROP A COLUMN FROM THE ABOVE TABLE
14. ALTER TABLE sales.quotations
DROP COLUMN amount;
WE CAN ALSO DROP MORE THAN 2 columns BY SEPARATING VIA
COMMAS
ALTER TABLE sales.quotations
DROP COLUMN amount, customer_name;
We can delete some top of the row as below
15. DELETE TOP (21)
FROM sales.customers;
We can also delete some percent (%) of the row as below
16. DELETE TOP (3) PERCENT
FROM sales.customers;
WHERE city=’buffalo’
We can also delete all the rows as below
*****************************
Database constraints
*****************************
1. SQL Server PRIMARY KEY constraint
A primary key is a column or a group of columns that uniquely identifies each row in a table.
You create a primary key for a table by using the PRIMARY KEY constraint.
Left join selects data starting from the left table and matching rows in the right table. The left
join returns all rows from the left table and the matching rows from the right table. If a row in the
left table does not have a matching row in the right table, the columns of the right table will have
nulls.
The left join is also known as left outer join. The outer keyword is optional.
The following statement joins the candidates table with the employees table using left join:
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM
hr.candidates c
LEFT JOIN hr.employees e
ON e.fullname = c.fullname
WHERE
e.id IS NULL;
iii. SQL Server Right Join
The right join or right outer join selects data starting from the right table. It is a reversed version
of the left join. The right join returns a result set that contains all rows from the right table and the
matching rows in the left table. If a row in the right table that does not have a matching row in the
left table, all columns in the left table will contain nulls.
The following example uses the right join to query rows from candidates and employees tables:
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM
hr.candidates c
RIGHT JOIN hr.employees e
ON e.fullname = c.fullname;
************************************
SQL Server Views
************************************
A view is a named query stored in the database catalog that allows you to refer to it later.
When you use the SELECT statement to query data from one or more tables, you get a result set.
For example, the following statement returns the product name, brand, and list price of all products
from the products and brands tables:
SELECT
product_name,
brand_name,
list_price
FROM
production.products p
INNER JOIN production.brands b
ON b.brand_id = p.brand_id;
Next time, if you want to get the same result set, you can save this query into a text file, open it,
and execute it again.
SQL Server provides a better way to save this query in the database catalog through a view. A
view is a named query stored in the database catalog that allows you to refer to it later.
So the query above can be stored as a view using the CREATE VIEW statement as follows:
CREATE VIEW sales.product_info
AS
SELECT
product_name,
brand_name,
list_price
Advantages of views
Generally speaking, views provide the following advantages:
Security: You can restrict users to access directly to a table and allow them to access a subset of
data via views.
For example, you can allow users to access customer name, phone, email via a view but restrict
them to access the bank account and other sensitive information.
Simplicity: A relational database may have many tables with complex relationships e.g., one-to-
one and one-to-many that make it difficult to navigate. However, you can simplify the complex
queries with joins and conditions using a set of views.
Consistency: Sometimes, you need to write a complex formula or logic in every query. To make
it consistent, you can hide the complex queries logic and calculations in views.
Once views are defined, you can reference the logic from the views rather than rewriting it in
separate queries.
*********************
Next class
*********************
************************************
SQL Server Stored Procedures
************************************
i. Creating a simple stored procedure
The following SELECT statement returns a list of products from the products table in the
BikeStores sample database.
************************************
SQL Server triggers
************************************
SQL Server triggers are special stored procedures that are executed automatically in response to
the database object, database, and server events. SQL Server provides three type of triggers:
Data manipulation language (DML) triggers which are invoked automatically in response to
INSERT , UPDATE , and DELETE events against tables.
Data definition language (DDL) triggers which fire in response to CREATE, ALTER , and DROP
statements. DDL triggers also fire in response to some system stored procedures that perform
DDL-like operations.
Logon triggers which fire in response to LOGON events
in this section, you will learn how to effectively use triggers in SQL Server.
Creating a trigger in SQL Server – show you how to create a trigger in response to insert and delete
events.
Creating an INSTEAD OF trigger – learn about the INSTEAD OF trigger and its practical
applications.
To be continued ...