Understanding SQL Datatypes Numeric Exact numeric data types ● With fixed precision and scale, there are more data types: decimal and numeric.. s scale The number of decimal digits
Trang 1Practice 02 SQL Basics
Outline
I Creating your first table
Naming conventions
Trang 2The above tables does not have naming convention
===>
Trang 3The 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
Trang 5Understanding SQL Datatypes
Numeric
Exact numeric data types
● With fixed precision and scale, there are more data types: decimal and numeric
decimal[ ( p[ ,s] )] and numeric[ (p[ ,s] )]
p (precision)
The maximum total number of decimal digits that will be stored, both to the left and to the right of the decimal point The precision must be a value from 1 through the
maximum precision of 38 The default precision is 18
s (scale)
The number of decimal digits that will be stored to the right of the decimal point This number is subtracted from p to determine the maximum number of digits to the left of
the decimal point Scale must be a value from 0 through p Scale can be specified only
if precision is specified The default scale is 0; therefore, 0 <= s <= p
Trang 6Example: decimal(4,2): To store a four-digit number with only two digits to the right of the decimal place
Approximate numeric data types
● 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
Trang 7Date and time data types
Trang 8Understanding column properties
● Allow null
● Primary key
● Length
● Unique
● Identity
Creating tables
Trang 10Do it yourself with Employee table
Employee(EmployeeID, Firstname, MiddleName, LastName)
EmployeeID: int, not null, identity (1,1)
Firstname: varchar, 50, Not null
MiddleName varchar, 50, null
Lastname, varchar, 50, not null
Altering table
❏ Add one more column Gender to Employee table
Trang 11Understanding computed columns
❏ Add one more column Fullname = FirstName + ‘, ‘ + LastName to Employee table
Trang 12Adding constraints to a table
Primary key constraints
Default constraints
Unique constraints
Check constraints
❏ Set EmployeeID property as primary key
❏ Set Default value (1) for Active property
❏ Set SocialSecurityNumber property as Unique value
❏ Create check constraint Gender = Female or Male
Trang 15Foreign key constraints
❏ Creating foreign key constraint between Address table and Employee table, namely, between EmployeeID on Address table to EmployeeID on Employee table
Trang 17Create DB diagram
Trang 18Practice 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