0% found this document useful (0 votes)
26 views32 pages

SQL Day 1

SQL is a language used to work with relational databases and manage data. It provides commands to query, insert, update, and delete data. SQL statements begin with keywords like SELECT, INSERT, UPDATE, DELETE. There are three main categories of SQL: DDL for defining objects, DML for manipulating data, and DCL for controlling access. A relational database stores data in tables that can be related through primary and foreign keys. Entities have attributes that are represented as columns, and each row stores data about a specific entity instance.

Uploaded by

RAA GHAV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views32 pages

SQL Day 1

SQL is a language used to work with relational databases and manage data. It provides commands to query, insert, update, and delete data. SQL statements begin with keywords like SELECT, INSERT, UPDATE, DELETE. There are three main categories of SQL: DDL for defining objects, DML for manipulating data, and DCL for controlling access. A relational database stores data in tables that can be related through primary and foreign keys. Entities have attributes that are represented as columns, and each row stores data about a specific entity instance.

Uploaded by

RAA GHAV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

DAY-1

CHAPTER 1

INTRODUCTION TO SQL
What is SQL?
SQL is an acronym of Structured Query Language. It is built to work around
relational databases which are sets of related information stored in tables. SQL
was introduced by IBM as the language to interface with its prototype relational
database management was introduced in 1979 by Oracle Corporation.
Benefits of SQL
• SQL has become the database language of choice because it is flexible, powerful,
and easy to learn.
• SQL is a non- procedural language. Process sets of records rather than just one at
a time and provides automatic navigation to the data.
• SQL provides commands for a variety of tasks including: querying data, creating,
updating and replacing objects and inserting, updating and deletion rows.
• All major relational database management systems support SQL thus one can
transfer all the skills gained with SQL from one RDBMS to another. Programs
written in SQL are portable; they can often be moved from one database system
to another with little modification.
Division of SQL
Every SQL statement begins with a command word key phrase. There are three major
categories.
Data definition language(DDL)
Create (new database objects)
Alter (Existing database objects)
Drop (existing database objects)
Data manipulation language (DML)
Select (to query)
Update (modifying tables)
Insert (adding new records)
Delete (remove existing records)
Data control language (DCL)
Grant (create user, provides database objects access)
Revoke (revokes the user, revokes database objects access)
Commit (commit the changes)
Rollback (nullify the changes)
Concepts of RDBMS
Database system
A database system is a computerized information
system for the management of data by means of a
general purpose software package called a database
management system. (DBMS). The major components
of a database management system are as follows:
• Computer hardware
• Database management system (software)
• Data to be manipulated
• User
Objectives of database system
1. Data independence: Different DBMS’s provide different of data independence to
protect application programs from modifications due to change in the database file
structure.
2. Minimizing data redundancy: Data from the different users are pooled and integrated
into the database in order to be shared by all users. Redundant data is thus minimized.
A properly designed database will reduce the redundant data storable to a minimum.
3. Data integrity: Some areas of concern in enforcing data integrity are input data
validation, concurrence interference and deadlock.
4. Data security: Because of extensive sharing in the database environment, the DBMS
protects an individual’s user’s data from unauthorized or destruction.
The DBA (Database Administrator) computer a programmer’s access right to a certain
portion of a database. There is control over access to the database. Access to the
database is controlled at a number of levels.
5. Efficiency: Providing fast response to user’s request and doing it economically is the
overall goal of the database system. Typically, a database query requires access to only
small portion of a large integrated database.
6. Data models: Data in the network is represented by a collection of records and
relationships among data are represented by links, which can be viewed as pointers.
Relational model
A relational model stores closely related data
in logical units called tables. A table can be
visualized as a series of columns and rows, in
which each column represents one type of
information and each row represents all the
types of information about a specific entity. In
relational system, there are no fixed
relationships between tables and no parent-
child requirements.
Tuple: Rows of a table are called tuples
Primary key: Primary key signifies a column whose values uniquely identify
the records in a file. E.g. employee number is primary key in employee
table, as it uniquely identifies any records in the employee table.
Foreign key: An ordinary column in a relation acting as a primary key, in
another relation is a foreign key. E.g. department number is a primary key
in department table, whereas it is foreign key in employee table.
Entity:
An entity is an object that exists and is distinguishable from other objects.
E.g. seema virnave with social security number 1001 is an entity, since it
uniquely identifies one particular person in the universe. Similarly 82001
in canara bank at Nehru place is an entity that uniquely identifies one
particular account.
Attribute:
Each column in a relation represents an attribute. It is equivalent to a
field. An entity is described by one or more attributes.
Examples
Consider the following relation
No name place score
1 SURESH THIRUNELVELI 100
2 RAJA TUTICORIN90
3 KUMAR PALAYAMKOTTAI 90
4.VENKATESH NAGERCOVIL 68
No, Name, Place, Score  attributes
100,90,90,68  domain
1, SURESH, THIRUNELVELI, 100  tuple
Entity relationship
E-R diagram models data and depicts how the
business views its structure. Entity relationship
diagram defines the relationships and dependencies
between various entities. It helps in identifying
information and leads to identifying various attributes
of an entity.
Types of relations
one to one (1:1)
An entity in A associated with at most one entity in B,
and an entity in B is associated with a most one entity
in A.
Example 1: employee is allocated a company car.

Example 2: one student is issue only one ID card


One to many (1: M)
For one occurrence of the first entity there can
exist many related occurrences of the second
entity and for every occurrence of the second
entity there exist only one associated
occurrence of the first.
Example 1

Example 2:
Many to one (M: 1)
An entity in A associated with at most one
entity in B. An entity in B, however can be
associated with any number of entities.
Example 1:
Many to Many (M: M)
An entity A is associated with any number of
entities in B, and an entity in B is associated
with number of entities in A.
E.F. codds’s rule
1. Information rule (representation of information): All
information in a RDBMS including tables, column names is
represented by values in tables. Benefit- this simple view of data
speeds up design and learning. Information is stored in the form
of tables containing rows and column.
2. Guaranteed access rule: Every piece of data in a RDMS can be
accessed by using a combination of a table name, and a primary
key value that identifies a row. Benefits- retrieving each
individual piece of data stored in a database becomes easy.
3. Systematic treatment of NULL values: RDBMS handles records
that have unknown or inapplicable values in a redefined fashion.
RDBMS also distinguishes between ZEROS, BLANKS, NULLS in
records and handles such values in a consistent manner to
produce correct answers.
4. High-level insert, update and delete: The
capability of handling a base relation or a
derived as a single operand applies and only to
the retrieval of data, but also to the insertion,
updation, and deletion of data.
Normalization
Normalization is the process of simplifying the
relationship between data elements in a record.
Through normalization a collection of data in a
record structure is replaced by successive record
structures that are more simpler.
CHAPTER 2

SQL SERVER AND T-SQL


Microsoft SQL server is a
• Structured query language (SQL) based
• Client/ server
• Relational database (RDBMS)
Database components
• The data in a Microsoft SQL server database is
organized into several different objects these
objects are what a user can see when they connect
to the database.
• In SQL server, these components are defined as
objects:
Objects Description
Databases A collection of information tables, and their objects organized and
presented to serve a specific purpose, such as facilitate searching,
sorting, and recombining data. Databases are stored in files.
Tables Describes how tables are used to store rows of data and define the
relationships between multiple tables.
Indexes Describes how indexes are used to increase the speed of accessing
the data in the table.
Views Describes views and their usefulness in providing an alternative way
of looking at the data in one or more tables.
Triggers A stored procedure that executes when data in a specified table is
modified. Triggers are often created to enforce referential integrity or
consistency among logically related data in different tables.
Stored procedure Describes how these transact-SQL programs centralized business
rules, tasks, and processes within the server.
Defaults A value inserted into a column automatically if the user does not
enter one.
Rules A database object bound to a column or user-defined data type that
specifies what data can be entered in that column.
Constraints A component, which is used for maintaining data integrity. It controls
the value that can be stored in a column.
User-defined data types A data type, based on a SQL server data type, created by the user for
custom data storage. Rules and defaults can be bound to user-
defined data types.
System databases and data
Microsoft SQL server systems have four system databases:
Master
The master database records all of the system level information for
a SQL server system. It records all login accounts and all system
configuration settings. Master is the database that records the
existence of all other databases and the location of the primary files
that contain the initialization information for the user databases.
Master records the initialization information for SQL server; always
have a recent backup of master available.
Tempdb
Tempdb holds all temporary tables and temporary stored
procedures.. Tempdb is re-created every time SQL server is started
so the system starts with a clean copy of the database. Because
temporary tables and stored procedure are dropped automatically
on disconnect, and no connections are active when the system is
shut down, there is never anything in tempdb to be saved from one
session of SQL server to another.
Model
The model database is used as the template for all
databases created on a system. When a create
database statement is issued, the first part of the
database is created by copying in the contents of
the model database, then the remainder of the new
database is filled with empty pages. Because
tempdb is created every time SQL server is started,
the model database must always exist on a SQL
server system.
Msdb
The msdb database is used by SQL server agent for
scheduling alerts and jobs, and recording operators.
Introduction to transact-SQL
To communicate with the SQL server and to
manipulate objects stored in the SQL server
client programs and stored procedures use a
variation of structured query language (SQL)
called transact-SQL or T-SQL.
ANSI-SQL
• In 1989, the American National Standards
Institute (ANSI) published the first set of
standards for a database query language, called
ANSI-SQL.
• ANSI- SQL contains standard mechanisms to
manipulate and arrange datas
SQL based systems.
Oracle DB2 SQL server access
Oracle
• It is a database of choice in internet- based companies and those with homogenous
database systems.
• It is reliable and portable.
• It is expensive and requires expert tuning.
• It is a product of oracle corporation
SQL server
• It is a database of choice in small networked systems.
• It runs only under the windows operating system
• It is inexpensive and easy to manage.
• It is a Microsoft corporation product.
DB2
• It is a database of choice in large organizations with legacy IBM mainframe systems.
• It is expensive.
• It is reliable and easy to maintain.
Access
Microsoft access is intended for single – user or departmental databases.
T-SQL data types.
A data type specifies the kind of information
the column is intended to store. In addition, a
data type determines the operations that can
be performed on the column.
The SQL server data types can be divided into
2 categories:
• System-supplied data types
• User-defined data types
Numeric data types
Integer data types
The store whole numbers, which are numbers
with no digits to the right of the decimal point.

Type Bytes Description


Bit 1 Integer data with either a 1 or 0 value

Int 4 Integer data from -2,147,483,648 through


2,147,483,647

Smallint 2 Integer data from -32,768 through 32,767.

Tinyint 1 Integer data from 0 through 255


Decimal data types:
Type Bytes Description
Decimal 5-17 Decimal numbers with fixed precision (p) and scale (s)
from -1038+1 through 1038-1. The precision can be any
number between 1 and 38; the default is 18
Numeric 5-17 Synonymous with decimal
Money 8 Monetary data values from -922,337,203,685,477.5808
through +922,337,203,685,477.5807, with accuracy to a
ten-thousandth of monetary unit.
Smallmoney 8 Monetary data values from -214,748.3648 through
+214,748.3647, with accuracy to a ten-thousandth of a
monetary unit.
Real data types
Type Bytes Description
Float[(n)] 4 or 8 Double-precision floating precision number data
from -1.79E+ 308 through 1.79E + 308
Real 4 Single-precious floating number data from -3.40E
+ 38 through 3.40E + 38

Date and time types


Type Bytes Description
Datetime 8 Date and time data from January 1, 1753, to
December 31, 9999, with an accuracy of three-
hundredths of second or 3.33 milliseconds.
Smalldatetime 8 Date and time data from January 1,1900,
through June 6, 2079, with an accuracy of one
minute.
• String data types
• String data types used to store standard
characters
• These data types use a single byte of storage.
Type Bytes description
Char[(n)] N Fixed-length non-Unicode character data with a
maximum length of 8,000 characters. The default is
1.
Varchar[(n)] Variable-length non-unicode data with a maximum
of 8,000 characters. The default is 1.
Text Variable-length non-unicode data with a maximum
length of 2,147,483,647 characters.
T-SQL operators
Arithmetic operators
• + Addition
• -subtraction
• * Multiplication
• / Division
• % modulo (remainder of division)
Relational operators
• = equal to
• >Greater than
• < Less than
• >= greater than or equal
• <= lesser than or equal to
• <> not equal to
• != not equal to
• !> not greater than
• () controls precedence
Logical operators
Operator Meaning
ALL TRUE if all of a set of comparisons are TRUE.
AND TRUE if both Boolean expressions are TRUE.
ANY TRUE if any one of a set of comparisons is TRUE.
BETWEEN TRUE if the operand is within a range.
EXISTS TRUE if a sub query contains any rows.
IN TRUE if the operand is equal to one of a list of
expressions.
LIKE TRUE if the operand matches a pattern.
NOT Reverses the value of any other Boolean.
OR TRUE if either Boolean expression is TRUE
SOME TRUE if some of set of comparisons are TRUE.
IS NULL TRUE if the operand is NULL.
IS NOT NULL TRUE TRUE if the operand is not NULL.

You might also like