Beruflich Dokumente
Kultur Dokumente
COMP- 213
By:
Shahid Mahmood
Warning!
Lecture slides are high-level description
of lectures
Historical aspects
Application Portability
Application sustainability
Required items {}
Choices |
(* | select list)
FROM
char(n) Fixed width character string. Maximum 8,000 characters Defined width
2 bytes +
varchar(n) Variable width character string. Maximum 8,000 characters
number of chars
2 bytes +
varchar(max) Variable width character string. Maximum 1,073,741,824 characters
number of chars
4 bytes +
text Variable width character string. Maximum 2GB of text data
number of chars
nchar Fixed width Unicode string. Maximum 4,000 characters Defined width x 2
Fixed precision and scale numbers. Allows numbers from -10^38 +1 to 10^38 –1.
The p parameter indicates the maximum total number of digits that can be stored
(both to the left and to the right of the decimal point). p must be a value from 1 to
decimal(p,s) 5-17 bytes
38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the
decimal point. s must be a value from 0 to p. Default value is 0
SQL Server Data Type (Number
Type:) Cont.
Data type Description Storage
Fixed precision and scale numbers. Allows numbers from -10^38 +1 to 10^38 –1.
The p parameter indicates the maximum total number of digits that can be stored
(both to the left and to the right of the decimal point). p must be a value from 1 to
numeric(p,s) 5-17 bytes
38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the
decimal point. s must be a value from 0 to p. Default value is 0
datetime2 From January 1, 0001 to December 31, 9999 with an accuracy of 100 nanoseconds 6-8 bytes
smalldatetime From January 1, 1900 to June 6, 2079 with an accuracy of 1 minute 4 bytes
date Store a date only. From January 1, 0001 to December 31, 9999 3 bytes
datetimeoffset The same as datetime2 with the addition of a time zone offset 8-10 bytes
Stores a unique number that gets updated every time a row gets created or modified. The timestamp value is
timestamp based upon an internal clock and does not correspond to real time. Each table may have only one timestamp
variable
SQL Server Data Type (Other
Type:)
Data type Description
sql_variant Stores up to 8,000 bytes of data of various data types, except text, ntext, and timestamp
ALTER: TABLE
SELECT
INSERT
UPDATE
DELETE
Important SQL commands
SELECT - extracts data from a database
[FOR ATTACH]
FOR ATTACH
CREATE Commands
CREATE TABLE command is used to:
- Create a table
CREATE TABLE
[database_name.[OWNER]. | OWNER.]
[CONSTRAINT constrain_name]
{[NULL|NOT NULL]
|[[FOREIGN KEY]
| CHECK (logical_expression)}
System databases of SQL Server
System database Description
Records all the system-level information for an instance of SQL Server. It
master Database records the existence and location of all other databases as well as the
initialization information for SQL Server
msdb Database Is used by SQL Server Agent for scheduling alerts and jobs.
Is used as the template for all databases created on the instance of SQL Server.
Modifications made to the model database, such as database size, collation,
model Database
recovery model, and other database options, are applied to any databases
created afterward.
Is a read-only database that contains system objects that are included with SQL
Resource Database Server. System objects are physically persisted in the Resource database, but
they logically appear in the sys schema of every database.
tempdb Database Is a workspace for holding temporary objects or intermediate result sets.
CREATE Table (for Paper work)
Column Data Type Data Length identity Allow Null Default
Name Value
ID Int 4 yes No No
Cust_name nvarchar 50 No No No
Cust_phone Int 15 No No 0
StPhone NVARCHAR(20),
CGPA REAL)
Example: Create Table (Semester)
CREATE TABLE Semester
| ADD
{[<column_definition >]
| column_name AS
computed_column_expression
}[ ,…n]
Alter Table command
| DROP
{[CONSTRAIN] constraint_name
}
Alter Table command
e.g., ALTER TABLE Student
program (prName)
ON Invoices (VendorID)
Removing rows and/or table
TRUNCATE table table_name
{ [(column_list)
{VALUES
| DEFAULT VALUES
‘SELECT’ statement
FROM table-name
SELECT *
FROM student;
The ‘SELECT’ command (DML)
Selecting certain COLUMNS from a table (PROJECTION)
FROM student;
The ‘SELECT’ command (DML)
Selecting certain COLUMNS from a table using alias
FROM table-name
FROM student;
The ‘SELECT’ command (DML)
Adding the amounts of two columns or concatenating with
“+”
From Enroll
FROM Student
The SQL COUNT(), AVG() and
SUM() Functions
The COUNT() function returns the number of rows that matches
a specified criteria.
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT SUM(column_name)
FROM table_name WHERE condition;
Questions?