DB07 SQL
DB07 SQL
Outline
• Ref: Chapter 6
Basic SQL
• SQL language
• Considered one of the major reasons for the commercial success of
relational databases
• SQL
• The origin of SQL is relational predicate calculus called tuple calculus which
was proposed initially as the language SQUARE.
• SQL Actually comes from the word “SEQUEL” which was the original term
used in the paper: “SEQUEL TO SQUARE” by Chamberlin and Boyce. IBM
could not copyright that term, so they abbreviated to SQL and copyrighted
the term SQL.
• Now popularly known as “Structured Query Language”.
• SQL is an informal or practical rendering of the relational data model with
syntax
DDL – DML
• Terminology:
• Table, row, and column used for relational model terms relation, tuple, and
attribute
• CREATE statement
• Main SQL command for data definition
• The language has features for :
• Data definition, Data Manipulation, Transaction control (Transact-SQL, Ch. 20),
• Indexing (Ch.17),
• Security specification (Grant and Revoke- see Ch.30),
• Active databases (Ch.26),
• Multi-media (Ch.26),
• Distributed databases (Ch.23) etc.
SQL Standards
• Domain
• Name used with the attribute specification
• Makes it easier to change the data type for a domain that is used by
numerous attributes
• Improves schema readability
• Example:
• CREATE DOMAIN SSN_TYPE AS CHAR(9);
• TYPE
• User Defined Types (UDTs) are supported for object-oriented
applications. (See Ch.12) Uses the command: CREATE TYPE
Basic constraints:
• Relational Model has 3 basic constraint types that are supported
in SQL:
• Key constraint: A primary key value cannot be duplicated
• Entity Integrity Constraint: A primary key value cannot be null
• Referential integrity constraints : The “foreign key “ must have a
value that is already present as a primary key, or may be null.
• Example
INSERT
• Specify the relation name and a list of values for the tuple. All
values including nulls are supplied.
• U1:
INSERT INTO EMPLOYEE
VALUES ( ‘Richard’, ‘K’, ‘Marini’, ‘653298653’, ‘1962-12-
30’, ‘98 Oak Forest, Katy, TX’, ‘M’, 37000, ‘653298653’, 4
);
• U1A:
INSERT INTO EMPLOYEE (Fname, Lname, Dno, Ssn) VALUES
(‘Richard’, ‘Marini’, 4, ‘653298653’);
• U3B:
NSERT INTO WORKS_ON_INFO ( Emp_name, Proj_name,
Hours_per_week )
SELECT [Link], [Link], [Link]
FROM PROJECT P, WORKS_ON W, EMPLOYEE E WHERE
[Link] = [Link] AND [Link] = [Link];
DELETE
SQL Server
• …
• 2000: MS SQL Server 2000.
• Nov 2005: MS SQL Server 2005.
• Aug 2008: MS SQL Server 2008.
• Apr 2010: MS SQL Server 2008 R2
• Mar 2012: MS SQL Server 2012
• Apr 2014: MS SQL Server 2014
• Feb 2016: MS SQL Server 2016
• Oct 2017: MS SQL Server 2017
• Nov 2019: MS SQL Server 2019
• Nov 2022: MS SQL Server 2022
• Database Architecture
• SQL Server – OS
Ref.: [Link]
• Integer
Name Bytes Range
bigint 8 -263 (-9,223,372,036,854,775,808) to
263 - 1 (9,223,372,036,854,775,807)
int 4 -231 (-2,147,483,648) to
231 - 1 (2,147,483,647)
smallint 2 -215 (-32,768) to 215 - 1 (32,767)
tinyint 1 0 to 255
• Exact numeric
Name Bytes Range
decimal[p[,s]] 5 – 17 - 1038 +1 to 1038 - 1.
numeric[p[,s]] 5 – 17 - 1038 +1 to 1038 - 1.
p (precision)
The maximum total number of decimal digits that can 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 maximum number of decimal digits that can be stored to the right 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. Maximum
storage sizes vary, based on the precision.
• Appropriate numeric
Name Bytes Range
Float[(n)] n - 1.79E+308 to -2.23E-308,
0 and 2.23E-308 to 1.79E+308
real 4 - 3.40E + 38 to -1.18E - 38,
0 and 1.18E - 38 to 3.40E + 38
• Monetary
Name Bytes Range
Money 8 -922,337,203,685,477.5808 to
922,337,203,685,477.5807
smallmoney 4 - 214,748.3648 to
214,748.3647
44 Design and Manage Database
FIT
• Characters
Name Bytes Comments
char[(n)] 0-8000 non-Unicode
varchar[(n)] 0-8000 non-Unicode
varchar(max) 0-2 GB non-Unicode, 16 bytes pointer on row,
preferred over text data type
text 0-2 GB non-Unicode, 16 bytes pointer or in row,
obsolete, varchar(max) preferred
nchar[(n)] 0-8000 max 4000 unicode characters
nvarchar[(n)] 0-8000 max 4000 unicode characters
nvarchar(max) 0-2 GB 16 bytes pointer or in row, preferred over ntext
data type
ntext 0-2 GB 16 bytes pointer, obsolete, nvarchar(max)
46 Design and Manage Database
prefered
FIT
• Binary
Name Bytes Comments
binary[(n)] 0-8000
varbinary[(n)] 0-8000
varbinary(max) 0-2 GB 16 bytes pointer or in row, preferred over
image data type
• Image
Name Bytes Comments
Image 0-2GB 16 bytes pointer, obsolete, varbinary(max)
preferred
• Global identifier
Name Bytes Comments
uniqueidentifier 16
• XML
Name Bytes Comments
xml 0-2GB 16 bytes pointer
Create Database
• Syntax:
Create Database <DB_Name> On
(Name <Logical_File_Name>,
Filename = <OS_File_Name.mdf>,
Size = <size> [ KB | MB | GB | TB ],
Maxsize = <size> [ KB | MB | GB | TB ] | UNLIMITED,
Filegrowth = growth_increment [ KB | MB | GB | TB | % ])
Log on
(Name <Logical_File_Name>,
Filename = <OS_File_Name.ldf>,
Size = <size> [ KB | MB | GB | TB ],
Maxsize = <size> [ KB | MB | GB | TB ] | UNLIMITED,
Filegrowth = growth_increment [ KB | MB | GB | TB | % ]);
Delete Database
• Syntax:
Drop Database <DB_Name>, …;
• Example:
Drop Database Company;
Go
Create table
• Syntax
Create table <tabName>
(<FieldName> <DataType (length)> Null| Not Null
[<FieldName> <DataType (length)> …);
• Example
CREATE TABLE Department(
DName varchar(15) NOT NULL,
DNumber numeric(4, 0) NOT NULL,
MgrSsn char(9) NULL,
MgrStartdate datetime NULL);
GO
• Example:
Alter Table Department
Add Constraint pk_Dept Primary Key (DNumber);
GO
• Foreign key
Alter table <tabName>
Add Constraint <consName> Foreign Key (<FieldName>)
References <tabName>(FieldName);
• Example:
Alter Table Department
Add Constraint fk_DepMgrSSN Foreign Key (MgrSSN)
Refrences Employee(SSN);
GO
Drop table
• Syntax
Drop Table <tabName>;
• Example
Drop Table Department;
Go
Insert
• Syntax:
Insert [Into] <tabName>(<fieldName1>, <fieldName2>, …)
Values(<value1>, <value2>, … );
Value: constant, expression, Null
• Example:
Insert Into Department(Dname, Dnumnbe, MrgSSN, MgrStartDate)
Values(‘Research’, 5, Null, ‘22-May-1988’);
Go
Or
Insert Into Department
Values(‘Research’, 5, Null, ‘22-May-1988’);
Go
Update
• Syntax
Update <tabName>
Set <fieldName> = <Value>;
[Where <condition>];
• Example:
Update Department
Set MgrSSN = ‘333445555’
Where DNumber = 5;
Go
Delete
• Syntax
Delete From <tabName>
[Where <condition>];
• Example
Delete From Department
Where Dnumber = 5;
Go
Or
Delete From Department;
Go