0% found this document useful (0 votes)
9 views

SQL Lecture 2

- SQL is the standard language for relational database management systems and was first developed in the 1970s. It has undergone several updates and standards to add new features. - SQL allows for the definition, querying, and manipulation of data stored in tables. It includes commands for data definition, data manipulation, and data control. - Data types in SQL include numeric, character, date/time, and miscellaneous types to store different kinds of data. Tables are created using the CREATE TABLE statement which defines the columns and their data types.

Uploaded by

Waleed Usman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Lecture 2

- SQL is the standard language for relational database management systems and was first developed in the 1970s. It has undergone several updates and standards to add new features. - SQL allows for the definition, querying, and manipulation of data stored in tables. It includes commands for data definition, data manipulation, and data control. - Data types in SQL include numeric, character, date/time, and miscellaneous types to store different kinds of data. Tables are created using the CREATE TABLE statement which defines the columns and their data types.

Uploaded by

Waleed Usman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Database Systems

Week 3: Introduction to SQL


SQL Overview
• Structured Query Language
• The standard for relational database management systems (RDBMS)
• RDBMS: A database management system that manages data as a
collection of tables in which all relationships are represented by
common values in related tables
History of SQL
• 1970 - Edgar F. Codd - Proposed relational database model.
• 1974 - IBM - Developed System R, the first SQL-based database management system.
• 1979 - Oracle Corporation - Released the first commercial SQL database management system, Oracle
V2.
• 1986 - ANSI - Established the first SQL standard, SQL-86.
• 1987 - IBM and others - Developed the first SQL-92 standard.
• 1992 - Microsoft - Released Microsoft SQL Server, which became a popular SQL-based database
management system.
• 1999 - ANSI and ISO - Released the SQL:1999 standard, which included object-oriented programming
features.
• 2003 - ANSI and ISO - Released the SQL:2003 standard, which added support for XML data.
• 2011 - ANSI and ISO - Released the SQL:2011 standard, which introduced temporal data support and
improved window functions.
• Current–SQL is supported by most major database vendors
Purpose of SQL Standard
• Specify syntax/semantics for data definition and manipulation
• Define data structures and basic operations
• Enable portability of database definition and application modules
• Specify minimal (level 1) and complete (level 2) standards
• Allow for later growth/enhancement to standard (referential integrity,
transaction management, user-defined functions, extended join
operations, national character sets)
Benefits of a Standardized Relational
Language
• Reduced training costs
• Productivity
• Application portability
• Application longevity
• Reduced dependence on a single vendor
• Cross-system communication
SQL Environment
• Catalog
• A set of schemas that constitute the description of a database
• Schema
• The structure that contains descriptions of objects created by a user (base tables, views,
constraints)
• Data Definition Language (DDL)
• Commands that define a database, including creating, altering, and dropping tables and
establishing constraints
• Data Manipulation Language (DML)
• Commands that maintain and query a database
• Data Control Language (DCL)
• Commands that control a database, including administering privileges and committing data
A simplified schematic of a typical SQL environment, as
described by the SQL: 2008 standard
DDL, DML, DCL, and the database development process
SQL Database Definition
• Data Definition Language (DDL)
• Major CREATE statements:
• CREATE SCHEMA–defines a portion of the database owned by a particular
user
• CREATE TABLE–defines a new table and its columns
• CREATE VIEW–defines a logical table from one or more tables or views
• Other CREATE statements: CHARACTER SET, COLLATION,
TRANSLATION, ASSERTION, DOMAIN
Data Types in SQL Server 2005
• SQL Server supports different data types. Will discuss the most
commonly used data types by dividing the data types into four major
categories:
1. numeric,
2. character,
3. date and time, and
4. miscellaneous.
Numeric Data Types
• Numeric data types should be used for storing numeric data, for data
on which you want to perform numeric comparisons or arithmetic
operations.
• Numeric data types can be divided into two groups:
• integers and
• decimals.
Integer data types
• Integer data types have no digits after the decimal point, and range in
size from 1 to 8 bytes of internal storage. Integer data types in SQL
Server include:
• BIGINT, which uses 8 bytes of storage and can be used to store numbers from
-263 to 263 -1. Avoid using the BIGINT data type unless you really need its
additional storage capacity.
• INT, which uses 4 bytes of storage and can be used to store numbers from -231
to 231 -1.
• SMALLINT, which uses 2 bytes of storage and can be used to store numbers
from -215 to 215 -1.
• TINYINT, which uses 1 byte of storage and can be used to store numbers from
0 to 255.
Integer data types
• Integer data types have no digits after the decimal point, and range in
size from 1 to 8 bytes of internal storage. Integer data types in SQL
Server include:
• MONEY, which uses 8 bytes of storage.
• SMALLMONEY, which uses 4 bytes of storage.
• MONEY and SMALLMONEY are included among integer types because they
are internally stored the same way as integers.
• The synonym for INT is INTEGER.
Decimal data types
• Decimal data types allow a larger range of values.
• These types can specify a precision and a scale.
• Precision is the total number of digits stored, and scale is the
maximum number of digits to the right of the decimal point.
• The storage space of decimal data varies according to the precision.
Decimals with a precision of 1 to 9 would take up 5 bytes of storage
space; decimals with a precision of 10 to 19 would take up 9 bytes of
storage, and so on.
Decimal data types
• Decimal data types include:
• REAL, which uses 4 bytes for storage and has a precision of 7 digits. The
synonym for REAL is FLOAT[(n)] for n = 1 to 7.
• FLOAT, which uses 8 bytes for storage and has a precision of 15 digits. The
synonym for FLOAT is DOUBLE PRECISION and FLOAT[(n)] for n = 8 to 15.
• DECIMAL, whose storage size varies based on the specified precision and uses
217 bytes for storage. The synonyms for DECIMAL are DEC and NUMERIC.
Character data types
• Character data types are used to store any combination of letters,
numbers and symbols.
• Single quotes have to be used when entering character data.
• SQL Server has five types of character data types:
• CHAR,
• VARCHAR,
• TEXT,
• NCHAR,
• NVARCHAR.
The CHAR data type
• CHAR(n)s are fixed-length single-byte character strings that can be used
to store up to 8,000 bytes of data.
• CHAR data is used when the column length is known and unvarying; for
example, a Social Security number could be of CHAR(9) data type.
Because CHARs use a fixed storage length, CHARs are accessed faster
than VARCHARs (varying length character strings).
• You can and should specify the maximum byte length of a CHAR(n) data
type with a value for n; otherwise, the default size will be used and the
default size may be set to a size much higher than what you need.
• The synonym for CHAR is CHARACTER.
The VARCHAR data type
• VARCHAR(n)s are variable length single-byte character strings that can
also be used to store up to 8000 bytes of data. You can and should
also specify the maximum byte length of VARCHARs with n.
• Variable length means that if less data than the specified n bytes is
used, the storage size will be the actual length of the data entered.
• The synonym for VARCHAR is CHAR VARYING. VARCHAR is the most
commonly used character (string) type.
• VARCHAR2 is the Oracle equivalent of VARCHAR.
The TEXT data type
• TEXTs are also variable-length single-byte character strings, but may
be used to store more than 8,000 bytes.
• TEXT has extra overhead that drags down performance. Therefore,
the use of the TEXT data type is not encouraged.
• LONG is the Oracle equivalent of TEXT.
The NCHAR and NVARCHAR data type
• NCHARs are fixed-length Unicode character strings . You can also
specify the maximum byte length of NCHAR with n.
• NVARCHARs are variable-length Unicode character strings. You can
specify the maximum byte of NVARCHAR length with n.
• NVARCHARs are variable-length Unicode character strings. You can
specify the maximum byte of NVARCHAR length with n.
Date and Time Data Types
• Two data types for storing date and time information:
• DATETIME and
• SMALLDATETIME.
• DATETIME uses 8 bytes. SMALLDATETIME uses 4 bytes of storage.
Miscellaneous Data Types
• BINARY,
• to store strings of bits, and values are entered and displayed using their
hexadecimal (hex) representation. The maximum length of the BINARY data
type is 8,000 bytes.
• IMAGE,
• The IMAGE data type is used to store binary values and is also used to store
pictures.
• BIT,
• can store only a 0 or a 1 and can consume only a single bit of storage space,
used for true/false or yes/no types of data.
Miscellaneous Data Types
• TABLE,
• TABLE data type can be used to store the result of a function and can be used as
the data type of local variables. Table variables are sometimes preferable to
temporary tables, because table variables are cleaned up automatically at the
end of a function or stored procedure.
• UNIQUEIDENTIFIER,
• referred to as globally unique identifier (GUID) or universal unique identifier
(UUID), is a 128-bit generated value that guarantees uniqueness worldwide,
even among unconnected computers. and the
• XML data type
• to handle XML data
• to store complete XML documents or fragments of XML documents.
Creating tables
In SQL, the CREATE TABLE command is used to create a table.
The general syntax of the CREATE TABLE statement is:
CREATE TABLE Tablename
(column_name type,
column_name type, .....
)
Creating tables
• Create a table called Employee that has four columns (attributes) in
Company database.

• CREATE TABLE Employee (names VARCHAR(20),


address VARCHAR(20),
employee_number INT,
salary SMALLMONEY)
Creating tables
• Execute the query.
• You will get: Command(s) completed successfully.
• To view the Employee table in the Company database, expand the Company
node (under the Object Explorer) and the Tables node, and you should be
able to see the Employee table
• To look at the table definition of the table you just created, right-click on the
table, Employee, and select Modify.

• Create a table called Names (type the following query):


• CREATE TABLE Names (fullname VARCHAR(20))
Inserting Values into a Table
• Two most commonly used ways:
• using INSERT INTO .. VALUES and
• using INSERT INTO .. SELECT.
Using INSERT INTO .. VALUES
• INSERT INTO .. VALUES option needs the column list and all the
columns in the correct order.
• The general syntax for the INSERT INTO .. VALUES option is:

INSERT INTO TableName VALUES ('character_attribute_value',


numeric_attribute_value, ...)

INSERT INTO Names VALUES ('Joe Smith')


Using INSERT INTO .. VALUES
• To insert into the Employee table that you created earlier, the INSERT
INTO .. VALUES statement to insert a row would have to match
column for column and would look like this:

INSERT INTO Employee VALUES ('Joe Smith', '123 4th St.', 101, 2500)
Using INSERT INTO .. VALUES
• You may INSERT a row with less than all the columns by naming the
columns you want to insert into, like this:
• INSERT INTO Employee (names, address) VALUES ('Joe Smith', '123
4th St.')
• The row will contain nulls or default values for the values left out,
which you will see if you type:
SELECT * FROM Employee
• Values MUST be inserted in the same order as the definition of the
table:
Using INSERT INTO .. SELECT
• INSERT INTO .. VALUES option, you insert only one row at a time into a
table. With the INSERT INTO .. SELECT option, you may (and usually
do) insert many rows into a table at one time.
• The general syntax for the INSERT INTO .. SELECT option is:
INSERT INTO target_table(column1, column2, column3, ...)
"SELECT clause"
• To copy all the names from the Employee table into the Names table,
type the following:
INSERT INTO Names(fullname)
SELECT names FROM Employee
Using INSERT INTO .. SELECT
• You could restrict the INSERT .. SELECT like this:
INSERT INTO Names(fullname)
SELECT names FROM Employee
WHERE salary > 2600
Using UPDATE
• You often UPDATE more than one row. To examine how the UPDATE
command works, we will use the tables we created.
• The general format for the UPDATE command is:
UPDATE TableName SET fieldname...
• For example, if you want to set all salaries in the table Emp2 to zero,
you may do so with one UPDATE command:
UPDATE Emp2 SET sal = 0
Using UPDATE
• Useful to include a WHERE clause in the UPDATE command so that
values are set selectively.
• UPDATE Employee SET salary = 0
• WHERE employee_number=101
Using DELETE FROM
• To delete all the rows in the Employee table as well as in the Names
table, type:
DELETE FROM Employee
• Then:
DELETE FROM Names
ALTER TABLE Command
• To add, change, and update rows in a table we use INSERT and
UPDATE commands.
• you can add, change (modify), and delete columns in a table's
definition by using SQL's ALTER TABLE command. ALTER TABLE
commands are known as data definition (DDL) commands, because
they change the definition of a table.
Adding a Column to a Table
• The general syntax for adding a column to a table is:
ALTER TABLE Tablename ADD column-name type

• For example, to add a column called bonus (a SMALLMONEY column) to


the Employee table, you type in the following:
ALTER TABLE Employee
ADD bonus SMALLMONEY
• When columns are added to existing tables, they will initially contain
null values. Data may be added to the new column using an UPDATE
command.
Deleting a Column from a Table
• The following is the general syntax for deleting a column from a table:
ALTER TABLE Tablename
DROP column column-name
• For example, to delete the column called bonus from the Employee
table, type the following:
ALTER TABLE Employee
DROP column bonus
The DELETE Command
• Following is the general syntax of the DELETE command used to delete rows from a
table:
DELETE FROM Table WHERE (condition)

(condition) determines which rows of the table will be deleted. As you saw earlier, if
no WHERE condition is used, all the rows of the table will be deleted.

• Multiple rows can be affected by the DELETE command, so be careful when using it.
Here is an example of using the DELETE command on our original Employee table:
DELETE FROM Employee WHERE salary < 1500
Deleting a Table
• The general syntax to delete or remove an entire table and its
contents is:
DROP TABLE Tablename
• For example, to delete the table called Names from your database,
you would type the following:
DROP TABLE Names

You might also like