T-SQL-Practice 02
T-SQL-Practice 02
SQL Basics
Outline
Create DB diagram 17
Naming conventions
The above tables does not have naming convention
===>
The above tables have naming convention.
Understanding schemas
While a database is the primary container of all objects, schemas offer another level of
containment and organization within a database.
Using a schema, a user can group objects of similar scope or ownership together By default, the
database owner (dbo) schema is automatically created within a database. Any object that is
created is added to this schema.
Understanding SQL Datatypes
Numeric
● With fixed precision and scale, there are more data types: decimal and numeric.
● Approximate-number data types for use with floating point numeric data. Floating
point data is approximate; therefore, not all values in the data type range can be
represented exactly. The ISO synonym for real is float(24).
float [ (n) ] Where n is the number of bits that are used to store the mantissa of the float
number in scientific notation and, therefore, dictates the precision and storage size. If n
is specified, it must be a value between 1 and 53. The default value of n is 53.
STRING
Date and time data types
Understanding column properties
● Allow null
● Primary key
● Length
● Unique
● Identity
Creating tables
Do it yourself with Employee table
Altering table
❏ Add one more column Gender to Employee table
Understanding computed columns
❏ Add one more column Fullname = FirstName + ‘, ‘ + LastName to Employee table
Adding constraints to a table
Primary key constraints
Default constraints
Unique constraints
Check constraints
❏ Creating foreign key constraint between Address table and Employee table, namely,
between EmployeeID on Address table to EmployeeID on Employee table.
Create DB diagram
Practice with the HR database
SQL: https://www.w3resource.com/sql-exercises/sorting-and-filtering-hr/index.php
1. Write a query in SQL to display the full name (first and last name), and salary
for those employees who earn below 6000.
2. Write a query in SQL to display the first and last_name, department number
and salary for those employees who earn more than 8000.
3. Write a query in SQL to display the first and last name, and department
number for all employees whose last name is “McEwen”.
4. Write a query in SQL to display all the information for all employees without
any department number
5. Write a query in SQL to display all the information about the department
Marketing.
Homework
https://en.wikibooks.org/wiki/SQL_Exercises/The_Hospital
https://www.w3resource.com/sql-exercises/hospital-database-exercise/sql-exerci
se-on-hospital-database.php