Intro To SQL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

What is SQL?

SQL is Structured Query Language, which is a computer


language for storing, manipulating and retrieving data stored
in relational database.

SQL is the standard language for Relation Database System. All


relational database management systems like MySQL, MS Access,
Oracle, Sybase, Informix, postgres and SQL Server use SQL as
standard database language.
Rational Database

Is a collection of Tables
Table name Attribute names
Tables

Product

PName Price Category Manufacturer


Is just a set of rows
Gizmo $19.99 Gadgets GizmoWorks
and columns like a
spread sheet which Powergizmo $29.99 Gadgets GizmoWorks
represent exactly one
SingleTouch $149.99 Photography Canon
type of entity
MultiTouch $203.99 Household Hitachi

Tuples or rows
Contents of a Table

• A table contains the actual data in records (rows).


• A record is composed of fields (columns).

• Each record contains one set of data values.

• The schema of a table is the table name and its attributes:


Product(PName, Price, Category, Manfacturer)
• Each raw or record of table contains information about a
single entity

Ex: in a table representing employees each row represents


a single person

• Each column or field of table contains a single attribute


for all rows in the table

Ex: in a table representing employees , we might have a


column containing first and last names for all employees
Key field for Identifying Rows
▪ A table contains a primary key that uniquely identifies a row of data.

▪ Each record must have a distinct value of primary key

▪ The primary key is used to relate (join) tables.

ID is the primary key in City table.

+------+------------+-------+-------------+---------+
| ID | Name | CCode | District | Populatn
+------+---------------+------------------+---------+
| 3320 | Bangkok | THA | Bangkok | 6320174 |
| 3321 | Nonthaburi | THA | Nonthaburi | 292100 |
| 3323 | Chiang Mai | THA | Chiang Mai | 171100 |
+------+------------+-------+-------------+---------+
Selecting single columns
A query is a request for data from a database table
Note

• Keywords are not case-sensitive which means you can write it upper
case or lower case
• Preferred use keyword in upper case to distinguish them from other
parts of your query like column and table names

Ex: SELECT name FROM people

• A query included a semicolon at the end (;)


• Case insensitive:
• Same: SELECT Select select
• Same: Product product
• Different: ‘Seattle’ ‘seattle’
What can SQL do?

• SQL can retrieve data from a database


• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
Simple SQL Query
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT *
FROM Product
WHERE category=‘Gadgets’

PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks

“selection” Powergizmo $29.99 Gadgets GizmoWorks


SQL data types
Ways to Use SQL

• console command

• GUI interfaces are often available

You might also like