Lab Manual 02 PDF
Lab Manual 02 PDF
Lab Instructor:
Ms. Hira Kanwal
Student Name
Student Roll #
Department
Batch/Year/Section
Marks Signature
vii
Lab Manual # 2 Creating & Manipulating Databases
2.1. Objective
After this lab you would be able to understand:
1. How to create Database and Data Tables
2. Primary key and Foreign key
3. Datatypes in SQL
4. Components of SQL
5. SQL is a RDBMS
A foreign key is a column in one table that uniquely identifies a row of another table. In
simpler words, the foreign key is defined in a second table, but it refers to the primary key in
the first table.
For example, a table called Employee has a primary key called employee_id. Another table
called Employee Details has a foreign key which references employee_id in order to
uniquely identify the relationship between both the tables.
Note:
Foreign key is used to link 2 tables, sometimes it is also called referencing key
Precision 1 – 9 = 5
bytes P = Precision, (Number of
Precision 10 - 19 = Values of precision p digits in a number)
9bytes and scale s. Precision is S = Scale, (Number of
Decimal (p, s)
Precision 20- 28 = specified from 1 to 38 digits to the right of decimal
13 bytes point)
Precision 29 - 38 =
17 bytes
Numeric Same as Decimal Same as decimal Same as Decimal
values from -
922,337,203,685,477.5808
Money 8 bytes Monetary value
to
+922,337,203,685,477.5807
Small money 4 bytes Monetary value values from -214,748.3648
to +214,748.3647
2. Approximate Numeric
Precision< 25 = 4
bytes Precision is specified from
Float
Precision>25 = 8 1 to 53
bytes
Real 4 bytes Precision is fixed to 7
3. DateTime
dates and times b/w
January 1, 1753, and
Instead of using DateTime,
Datetime 8 bytes December 31, 9999,
use datetime2 to save 1 byte
with an accuracy of 3.33
milliseconds
Dates and time b/w
January 1, 1900, and
June 6, 2079, with an
Smalldatetime 4 bytes
accuracy of 1 minute
(the seconds are always
listed as “:00”)
dates between January 1,
Date 3 bytes 0001, and December 31,
9999
Specifying the precision is
possible. TIME(3) will have
milliseconds precision.
TIME(7) is the highest and
Time 3 -5 bytes
the default precision.
Casting values to a lower
precision will round the
value.
Precision 1- 2 =
Stores date and times
6bytes
between January 1,
Precision 3 -4 = 7 Combination of Date and
Datetime2 0001, and December 31,
bytes Time datatype
9999, with an accuracy
Precision 5-7 = 8
of 100 nanoseconds
bytes
4. Character String
Char Max 8000 characters Fixed length
2 bytes + number
Varchar Max 8000 characters Variable length
of chars
2 bytes + number
Varchar (max) Max 231 characters Variable length
of chars
4 bytes + number Max 2147483647
Text Variable length
of chars character
5. Unicode Character String
Nchar Max 4000 characters Fixed Length
Nvarchar Max 4000 characters Variable Length
Nvarchar (max) Max 231 characters Variable length
Ntext Max 1073741823 Variable length
characters
6. Binary Strings
Binary Max 8000 bytes Fixed Length
Varbinary Max 8000 bytes Variable length
Varbinary
Max 231 bytes Variable length
(max)
Variable length ,
Image Max 2147483647 bytes
Deprecated
7. Other Datatypes
Sql_variant
Timestamp
Uniqueidentifier
Xml
Cursor
Stores a temporary table (a
Table
query result)
Note: Fixed Length datatypes are given a fixed storage space to hold one item, while in
variable length data type, the amount of memory must be specified by the consumer.
TINYINT: A very small integer that can be signed or unsigned. If signed, the allowable
range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. You can specify
a width of up to 4 digits.
SMALLINT: A small integer that can be signed or unsigned. If signed, the allowable range
is from -32768 to 32767. If unsigned, the allowable range is from 0 to 65535. You can
specify a width of up to 5 digits.
INT: A normal-sized integer that can be signed or unsigned. If signed, the allowable range is
from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to 4294967295.
You can specify a width of up to 11 digits.
BIGINT: A large integer that can be signed or unsigned. If signed, the allowable range is
from -9223372036854775808 to 9223372036854775807. If unsigned, the allowable range is
from 0 to 18446744073709551615. You can specify a width of up to 20 digits.
FLOAT (M, D): A floating-point number that cannot be unsigned. You can define the
display length (M) and the number of decimals (D). Decimal precision can go to 24 places for
a FLOAT.
DOUBLE (M, D): A double precision floating-point number that cannot be unsigned. You
can define the display length (M) and the number of decimals (D). Decimal precision can go
to 53 places for a DOUBLE.
DATE: A date in YYYY-MM-DD format. For example, February 29th, 2016 would be
stored as 2016-02-29.
TIMESTAMP: A timestamp, this looks like the previous DATETIME format, only without
the hyphens between numbers; 4:30 in the afternoon on February 29th, 2016 would be stored
as 20160229143000 ( YYYYMMDDHHMMSS).
CHAR(M): A fixed-length string between 1 and 255 characters in length (for example
CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length is
not required, but the default is 1.
A table can have only one Primary key, but can have several foreign keys.
Note: SQL Server by default denies to accept any changes made to table structure after
table creation. If you want to modify table structure, you have to change its options:
Design a Database in SQL Server 2008 R2, keeping in mind the following:
Database name should be ‘Students’.
a. Database Students contain a table named ‘StdInfo’.
b. ‘StdInfo’ table should contain 7 columns namely (RollNo, Name,
Program, Semester, Contact#, Address)
c. ‘RollNo’ should be the primary key.
d. Contact# and Address columns can allow NULL values
Choose the primary keys of the tables (Std_Id, Gr_Id, Teacher_Id, Discipline_Id).
Now modify the structure of the students table by adding a new field, “Has_scholarship”
that can take the values “yes” or “no”.