0% found this document useful (0 votes)
41 views9 pages

What Is SQL?: A Glimpse of Schema

SQL is a special-purpose language used to define, access, and manipulate data in relational database management systems (RDBMS). A schema specifies the overall design and structure of a database and does not change frequently. SQL statements are categorized into data definition language (DDL) statements to define database structure and data manipulation language (DML) statements to manage data. MySQL is an open-source relational database management system commonly used with web applications.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
41 views9 pages

What Is SQL?: A Glimpse of Schema

SQL is a special-purpose language used to define, access, and manipulate data in relational database management systems (RDBMS). A schema specifies the overall design and structure of a database and does not change frequently. SQL statements are categorized into data definition language (DDL) statements to define database structure and data manipulation language (DML) statements to manage data. MySQL is an open-source relational database management system commonly used with web applications.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

What is SQL?

SQL, which stands for Structured Query Language, is a special-purpose language used to define, access, and
manipulate data. Every SQL implementation sits atop a database engine, whose job it is to interpret SQL
statements and determine how the various data structures in the database should be accessed in order to
accurately and efficiently produces the desired outcome.

A glimpse of Schema…
Schema – the description of database. (Elmasri & Navathe)
– the overall design of the database.( Silberschatz−Korth−Sudarshan)
A schema is specified during database design and is not expected to change frequently. Most data models have
certain conventions for displaying schema as diagrams. A displayed schema is called schema diagram. Figure 1
shows a schema diagram of a database and the structure of each record type but not the actual instances of
records.

STUDENT
Name Student_Number Class Major
COURSE
Course_Name Course_Number Credit_Hours Department

Figure 1. A sample schema diagram.

A query is a statement requesting the retrieval of information.

A Brief History of SQL


In the early 1970s, an IBM research fellow named Dr. E. F. Codd endeavored to apply the rigors of mathematics to
the then-untamed world of data storage and retrieval. Codd's work led to the definition of the relational data
model and a language called DSL/Alpha for manipulating data in a relational database. IBM liked what they saw, so
they commissioned a project called System/R to build a prototype based on Codd's work. Among other things, the
System/R team developed a simplified version of DSL called SQUARE, which was later, renamed SEQUEL, and
finally renamed SQL.
The work done on System/R eventually led to the release of various IBM products based on the relational model.
Other companies, such as Oracle, rallied around the relational flag as well. By the mid 1980's, SQL had gathered
sufficient momentum in the marketplace to warrant oversight by the American National Standards Institute (ANSI).
ANSI released its first SQL standard in 1986, followed by updates in 1989, 1992, and 1999.
Thirty years after the System/R team began prototyping a relational database, SQL is still going strong. While there
have been numerous attempts to dethrone relational databases in the marketplace, well-designed relational
databases coupled with well-written SQL statements continue to succeed in handling large, complex data sets
where other methods fail.

DBMS Languages
Data Definition Language (DDL) statements are used to define the database structure or schema. Once the design
of a database is completed and a DBMS is chosen to implement the database, the first step is to specify conceptual
schema for the database.
Some examples:
CREATE - to create objects in the database.
ALTER - alters the structure of the database.
DROP - delete objects from the database.
TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed.
COMMENT - add comments to the data dictionary.
RENAME - rename an object.
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some
examples:
SELECT - retrieve data from the database.
INSERT – adds data to a database.
UPDATE - modifies data in a database.
DELETE – removes data in a database.

Some people feel that DDL is the sole property of database administrators, while database developers are
responsible for writing DML statements, but the two are not so easily separated. It is difficult to efficiently access
and manipulate data without an understanding of what data structures are available and how they are related;
likewise, it is difficult to design appropriate data structures without knowledge of how the data will be accessed.

Introducing MySQL
MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user
access to a number of databases. MySQL is officially pronounced ("My S-Q-L"), but is often also pronounced ("My
Sequel"). It is named for original developer Michael Widenius's daughter My.

MySQL was owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now
owned by Sun Microsystems, a subsidiary of Oracle Corporation.

Free-software projects that require a full-featured database management system often use MySQL. Such
projects include (for example) WordPress, phpBB, Drupaland other software built on the LAMP software stack.
MySQL is also used in many high-profile, large-scale World Wide Web products including Wikipedia, Google
and Facebook.

DML Sample Applications


SQL: DISTINCT Clause
The DISTINCT clause allows you to remove duplicates from the result set. The DISTINCT clause can only be used with
select statements.
The syntax for the DISTINCT clause is:
SELECT DISTINCT columns
FROM tables
WHERE predicates;

Example #1
Let's take a look at a very simple example.
SELECT DISTINCT city
FROM suppliers;
This SQL statement would return all unique cities from the suppliers table.

Example #2
The DISTINCT clause can be used with more than one field.
For example:
SELECT DISTINCT city, state
FROM suppliers;
This select statement would return each unique city and state combination. In this case, the distinct applies to each field
listed after the DISTINCT keyword.

SQL: "OR" Condition


The OR condition allows you to create an SQL statement where records are returned when any one of the conditions are
met. It can be used in any valid SQL statement - select, insert, update, or delete.
The syntax for the OR condition is:
SELECT columns
FROM tables
WHERE column1 = 'value1'
or column2 = 'value2';
The OR condition requires that any of the conditions be must be met for the record to be included in the result set. In this
case, column1 has to equal 'value1' OR column2 has to equal 'value2'.

Example #1
The first example that we'll take a look at involves a very simple example using the OR condition.
SELECT *
FROM suppliers
WHERE city = 'New York'
or city = 'Newark';
This would return all suppliers that reside in either New York or Newark. Because the * is used in the select, all fields from
the suppliers table would appear in the result set.

Example #2
The next example takes a look at three conditions. If any of these conditions is met, the record will be included in the result
set.
SELECT supplier_id
FROM suppliers
WHERE name = 'IBM'
or name = 'Hewlett Packard'
or name = 'Gateway';
This SQL statement would return all supplier_id values where the supplier's name is either IBM, Hewlett Packard or
Gateway.

SQL: LIKE Condition


The LIKE condition allows you to use wildcards in the where clause of an SQL statement. This allows you to perform pattern
matching. The LIKE condition can be used in any valid SQL statement - select, insert, update, or delete.
The patterns that you can choose from are:
% allows you to match any string of any length (including zero length)
_ allows you to match on a single character

Examples using % wildcard


The first example that we'll take a look at involves using % in the where clause of a select statement. We are going to try to
find all of the suppliers whose name begins with 'Hew'.
SELECT * FROM suppliers
WHERE supplier_name like 'Hew%';

You can also using the wildcard multiple times within the same string. For example,
SELECT * FROM suppliers
WHERE supplier_name like '%bob%';
In this example, we are looking for all suppliers whose name contains the characters 'bob'.

You could also use the LIKE condition to find suppliers whose name does not start with 'T'. For example,
SELECT * FROM suppliers
WHERE supplier_name not like 'T%';
By placing the not keyword in front of the LIKE condition, you are able to retrieve all suppliers whose name does not start
with 'T'.

Examples using _ wildcard


Next, let's explain how the _ wildcard works. Remember that the _ is looking for only one character.
For example,
SELECT * FROM suppliers
WHERE supplier_name like 'Sm_th';
This SQL statement would return all suppliers whose name is 5 characters long, where the first two characters is 'Sm' and
the last two characters is 'th'. For example, it could return suppliers whose name is 'Smith', 'Smyth', 'Smath', 'Smeth', etc.
Here is another example,
SELECT * FROM suppliers
WHERE account_number like '12317_';
You might find that you are looking for an account number, but you only have 5 of the 6 digits. The example above, would
retrieve potentially 10 records back (where the missing value could equal anything from 0 to 9). For example, it could return
suppliers whose account numbers are:
123170
123171
123172
123173
123174
123175
123176
123177
123178
123179.

SQL: BETWEEN Condition


The BETWEEN condition allows you to retrieve values within a range.
The syntax for the BETWEEN condition is:
SELECT columns
FROM tables
WHERE column1 between value1 and value2;
This SQL statement will return the records where column1 is within the range of value1 and value2 (inclusive). The
BETWEEN function can be used in any valid SQL statement - select, insert, update, or delete.

Example #1 - Numbers
The following is an SQL statement that uses the BETWEEN function:
SELECT *
FROM suppliers
WHERE supplier_id between 5000 AND 5010;
This would return all rows where the supplier_id is between 5000 and 5010, inclusive. It is equivalent to the following SQL
statement:
SELECT *
FROM suppliers
WHERE supplier_id >= 5000
AND supplier_id <= 5010;

Example #2 - Dates
You can also use the BETWEEN function with dates.
SELECT *
FROM orders
WHERE order_date between to_date ('2003/01/01', 'yyyy/mm/dd')
AND to_date ('2003/12/31', 'yyyy/mm/dd');
This SQL statement would return all orders where the order_date is between Jan 1, 2003 and Dec 31, 2003 (inclusive).
It would be equivalent to the following SQL statement:
SELECT *
FROM orders
WHERE order_date >= to_date('2003/01/01', 'yyyy/mm/dd')
AND order_date <= to_date('2003/12/31','yyyy/mm/dd');

Example #3 - NOT BETWEEN


The BETWEEN function can also be combined with the NOT operator.
For example,
SELECT *
FROM suppliers
WHERE supplier_id not between 5000 and 5500;
This would be equivalent to the following SQL:
SELECT *
FROM suppliers
WHERE supplier_id < 5000
OR supplier_id > 5500;
In this example, the result set would exclude all supplier_id values between the range of 5000 and 5500 (inclusive).

SQL: Joins
A join is used to combine rows from multiple tables. A join is performed whenever two or more tables is listed in the FROM
clause of an SQL statement.
There are different kinds of joins. Let's take a look at a few examples.

Inner Join (simple join)


Chances are, you've already written an SQL statement that uses an inner join. It is the most common type of join. Inner joins
return all rows from multiple tables where the join condition is met.
For example,
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
This SQL statement would return all rows from the suppliers and orders tables where there is a matching supplier_id value
in both the suppliers and orders tables.

Let's look at some data to explain how inner joins work:


We have a table called suppliers with two fields (supplier_id and supplier_ name).
It contains the following data:
supplier_id supplier_name
10000 IBM
10001 Hewlett Packard
10002 Microsoft
10003 NVIDIA

We have another table called orders with three fields (order_id, supplier_id, and order_date).
It contains the following data:
order_id supplier_id order_date
500125 10000 2003/05/12
500126 10001 2003/05/13

If we run the SQL statement below:


SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;

Our result set would look like this:


supplier_id Name order_date
10000 IBM 2003/05/12
10001 Hewlett Packard 2003/05/13
The rows for Microsoft and NVIDIA from the supplier table would be omitted, since the supplier_id's 10002 and 10003 do not
exist in both tables.

Outer Join
Another type of join is called an outer join. This type of join returns all rows from one table and only those rows from a
secondary table where the joined fields are equal (join condition is met).
For example,
select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where suppliers.supplier_id = orders.supplier_id(+);
This SQL statement would return all rows from the suppliers table and only those rows from the orders table where the
joined fields are equal.
The (+) after the orders.supplier_id field indicates that, if a supplier_id value in the suppliers table does not exist in the
orders table, all fields in the orders table will display as <null> in the result set.
The above SQL statement could also be written as follows:
select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where orders.supplier_id(+) = suppliers.supplier_id

Let's look at some data to explain how outer joins work:


We have a table called suppliers with two fields (supplier_id and name).
It contains the following data:
supplier_id supplier_name
10000 IBM
10001 Hewlett Packard
10002 Microsoft
10003 NVIDIA

We have a second table called orders with three fields (order_id, supplier_id, and order_date).
It contains the following data:
order_id supplier_id order_date
500125 10000 2003/05/12
500126 10001 2003/05/13

If we run the SQL statement below:


select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where suppliers.supplier_id = orders.supplier_id(+);

Our result set would look like this:


supplier_id supplier_name order_date
10000 IBM 2003/05/12
10001 Hewlett Packard 2003/05/13
10002 Microsoft <null>
10003 NVIDIA <null>
The rows for Microsoft and NVIDIA would be included because an outer join was used. However, you will notice that the
order_date field for those records contains a <null> value.

SQL: ORDER BY Clause


The ORDER BY clause allows you to sort the records in your result set. The ORDER BY clause can only be used in
SELECT statements.
The syntax for the ORDER BY clause is:
SELECT columns
FROM tables
WHERE predicates
ORDER BY column ASC/DESC;
The ORDER BY clause sorts the result set based on the columns specified. If the ASC or DESC value is omitted, it is sorted
by ASC.
ASC indicates ascending order. (default)
DESC indicates descending order.

Example #1
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city;
This would return all records sorted by the supplier_city field in ascending order.
Example #2
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC;
This would return all records sorted by the supplier_city field in descending order.

SQL: SUM Function


The SUM function returns the summed value of an expression.
The syntax for the SUM function is:
SELECT SUM(expression )
FROM tables
WHERE predicates;
expression can be a numeric field or formula.

Simple Example
For example, you might wish to know how the combined total salary of all employees whose salary is above $25,000 / year.
SELECT SUM(salary) as "Total Salary"
FROM employees
WHERE salary > 25000;
In this example, we've aliased the sum(salary) field as "Total Salary". As a result, "Total Salary" will display as the field name
when the result set is returned.

Example using DISTINCT


You can use the DISTINCT clause within the SUM function. For example, the SQL statement below returns the combined
total salary of unique salary values where the salary is above $25,000 / year.
SELECT SUM(DISTINCT salary) as "Total Salary"
FROM employees
WHERE salary > 25000;
If there were two salaries of $30,000/year, only one of these values would be used in the SUM function.

DDL Sample Application

SQL: DROP TABLE Statement


The DROP TABLE statement allows you to remove a table from the database.
The basic syntax for the DROP TABLE statement is:
DROP TABLE table_name;

For example:
DROP TABLE supplier;
This would drop table called supplier.

SQL: ALTER TABLE Statement


The ALTER TABLE statement allows you to rename an existing table. It can also be used to add, modify, or drop a column
from an existing table.

Renaming a table
The basic syntax for renaming a table is:
ALTER TABLE table_name
RENAME TO new_table_name;
For example:
ALTER TABLE suppliers
RENAME TO vendors;
This will rename the suppliers table to vendors.
Adding column(s) to a table
Syntax #1
To add a column to an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name
ADD column_name column-definition;
For example:
ALTER TABLE supplier
ADD supplier_name varchar2(50);
This will add a column called supplier_name to the supplier table.
Syntax #2
To add multiple columns to an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name
ADD ( column_1 column-definition,
column_2 column-definition,
...
column_n column_definition );
For example:
ALTER TABLE supplier
ADD ( supplier_name varchar2(50),
city varchar2(45) );
This will add two columns (supplier_name and city) to the supplier table.

Modifying column(s) in a table


Syntax #1
To modify a column in an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name
MODIFY column_name column_type;
For example:
ALTER TABLE supplier
MODIFY supplier_name varchar2(100) not null;
This will modify the column called supplier_name to be a data type of varchar2(100) and force the column to not allow null
values.

Syntax #2
To modify multiple columns in an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name
MODIFY ( column_1 column_type,
column_2 column_type,
...
column_n column_type );
For example:
ALTER TABLE supplier
MODIFY ( supplier_name varchar2(100) not null,
City varchar2(75) );
This will modify both the supplier_name and city columns.

Drop column(s) in a table


Syntax #1
To drop a column in an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name
DROP COLUMN column_name;
For example:
ALTER TABLE supplier
DROP COLUMN supplier_name;
This will drop the column called supplier_name from the table called supplier.

Rename column(s) in a table

Syntax:
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
For example:
ALTER TABLE supplier
RENAME COLUMN supplier_name to sname;
This will rename the column called supplier_name to sname.

You might also like