Professional Documents
Culture Documents
Introduction
Structured Query Language, is a database computer language designed for managing data in relational database management systems (RDBMS), and originally based upon relational algebra and calculus. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.
Introduction (Cont.)
SQL was developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL (Structured English Query Language), was designed to manipulate and retrieve data stored in IBM's original quasi-relational database management system, System R, which a group at IBM San Jose Research Laboratory had developed during the 1970s. The acronym SEQUEL was later changed to SQL because "SEQUEL" was a trademark of the UK-based Hawker Siddeley aircraft company.
What is SQL?
SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL is an ANSI (American National Standards Institute) standard
Database Systems
delete records from a database create new databases create new tables in a database create stored procedures in a database create views in a database set permissions on tables, procedures, and views
Database Systems
DROP INDEX - deletes an index CREATE VIEW - creates a view DROP VIEW - deletes a view
3 of 22
Database Systems
4 of 22
Database Systems
SQL Wildcards
SQL wildcards can substitute for one or more characters when searching for data in a database. SQL wildcards must be used with the SQL LIKE operator. With SQL, the following wildcards can be used:
Wildcard % _ [charlist] Description A substitute for zero or more characters A substitute for exactly one character Any single character in charlist
5 of 22
Database Systems
6 of 22
Database Systems
SELECT column_name(s) FROM table_name WHERE column_name IS NULL How do we select only the records with no Null values in the column We will have to use the IS NOT Null operator: SELECT column_name(s) FROM table_name WHERE column_name IS NOT NULL
Cartesian product
To make a cartesion product the only thing that you need to do is add more than one table on the FROM statement. Cartesion product Syntax SELECT column_name(s) FROM table_name1, table_name2
7 of 22
Database Systems
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys. A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
8 of 22
Database Systems
9 of 22
Database Systems
AVG() - Returns the average value COUNT() - Returns the number of rows FIRST() - Returns the first value LAST() - Returns the last value MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum
10 of 22
Database Systems
Database Systems
Each column in an SQL table declares the type(s) that column may contain. ANSI SQL includes the following datatypes. Character (alphanumeric) strings CHARACTER(n) or CHAR(n) - fixed-width n-character string, padded with spaces as needed CHARACTER VARYING(n) or VARCHAR(n) - variable-width string with a maximum size of n characters NATIONAL CHARACTER(n) or NCHAR(n) - fixed width string supporting an international character set NATIONAL CHARACTER VARYING(n) or NVARCHAR(n) variable-width NCHAR string
SQL Constraints
Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE
12 of 22
Database Systems
statement). We will focus on the following constraints: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT
13 of 22
Database Systems
14 of 22
Database Systems
Database Systems
specifies that the column "column_name1" must check the condition. CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, .... CONSTRAINT constraint_name CHECK (column_name1 operator value) )
Indexes
An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes, they are just used to speed up searches/queries. Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So you should only create indexes on columns (and tables) that will be frequently searched against.
16 of 22
Database Systems
Indexes (Cont.)
SQL CREATE INDEX Syntax CREATE INDEX index_name ON table_name (column_name(s))
17 of 22
Database Systems
18 of 22
Database Systems
Database Systems
(Cont.)
SQL STORE PROCEDURE syntax: CREATE PROCEDURE procedure_name param1 data_type = default_value, param2 data_type = default_value, ... AS sql statement
Database TRIGGER
A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is mostly used for keeping the integrity of the information on the database.
Database Systems
Database Systems
22 of 22