Database CIS 340 LAB#2 I.Arwa Najdi a_najdi1988@yahoo.com
Objectives Create Table Insert data into table Retrieving Data Using the SQL SELECT Statement Lab exercise
SQL DML and DDL SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL). The query and update commands form the DML part of SQL: SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys), specifies links between tables. The most important DDL statements in SQL are: CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index.
Creating Table Using the SQL CREATE Statement
Creating Tables Tables are the basic structure where data is stored in the database You create tables with the SQL CREATE TABLE statement. When creating tables, you must provide: Table name Column name(s) Data types for each column CREATE TABLE table_name (column 1 data_type_for_column_1, column 2 data_type_for_column_2,... )
rules Table and column naming rules: Common data types:
Using CREATE Table CREATE TABLE student( Id number, fname VARCHAR(30), lname VARCHAR(30), address VARCHAR(30), birthdate VARCHAR(50))
Inserting Data Using the SQL INSERT INTO Statement
inserting data The SQL INSERT INTO syntax has 2 main forms and the result of either of them is adding a new row into the database table. The first syntax form of the INSERT INTO SQL clause doesn't specify the column names where the data will be inserted, but just their values: INSERT INTO Table1 VALUES (value1, value2, value3 ) The second form of the SQL INSERT INTO command, specifies both the columns and the values to be inserted in them: INSERT INTO Table1 (Column1, Column2, Column3 ) VALUES (Value1, Value2, Value3 )
inserting data the number of the columns in the second INSERT INTO syntax form must match the number of values into the SQL statement, otherwise you will get an error If you want to enter data for just a few of the table columns, you ll have to use the second syntax form of the SQL INSERT INTO clause, because the first form will produce an error if you haven t supplied values for all columns. INSERT INTO student VALUES ('345','amal','naser','alolya','22/2/1409') INSERT INTO student(id,fname,lname,address,birthdate) VALUES ('375','norah','naser','alolya','22/2/1409')
inserting data
Retrieving Data Using the SQL SELECT Statement
SELECT Statement Basics The SQL SELECT statement :queries data from tables in the database. The statement begins with the SELECT keyword. The basic SELECT statement has 3 clauses: SELECT FROM WHERE The SELECT clause: specifies the table columns that are retrieved. The FROM clause: specifies the tables accessed. The WHERE clause: specifies which table rows are used. The WHERE clause is optional; if missing, all table rows are used.
SQL SELECT Syntax SELECT column_name(s) FROM table_name WHERE column_name operator value SELECT identifies the columns to be displayed column names separated by commas FROM identifies the table containing those columns WHERE specifies which rows in the FROM tables to use (it is optional)
Departments table
Selecting All Columns To select all columns from table use * SELECT * FROM departments;
Selecting Specific Columns SELECT department_id, location_id FROM departments;
The SQL SELECT DISTINCT Statement In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table. The DISTINCT keyword can be used to return only distinct (different) values. SELECT DISTINCT column_name(s) FROM table_name WHERE column_name operator value DISTINCT specifies is optional. The default is retains duplicate rows.
Example of DISTINCT The default display of queries is all rows, including duplicate rows. SELECT department_id FROM employees; SELECT DISTINCT department_id FROM employees;
Defining a Column Alias The column names in the select statement can be qualified by the appropriate table name: SELECT employees.last_name, employees.salary FROM employees; A column alias: Renames a column heading Is useful with calculations Immediately follows the column name (There can also be the optional AS keyword between the column name and alias.) Requires double quotation marks if it contains spaces or special characters or if it is case-sensitive
Using Column Aliases
Concatenation Operator A concatenation operator: Links columns or character strings to other columns Is represented by two vertical bars ( ) Creates a resultant column that is a character expression SELECT last_name job_id AS "Employees FROM employees;
Literal Character Strings A literal is a character, a number, or a date that is included in the SELECT statement. Date and character literal values must be enclosed by single quotation marks. Each character string is output once for each row returned.
Using Literal Character Strings SELECT last_name ' is a ' job_id AS "Employee Details" FROM employees;
Limiting Rows Using a Selection (Restricting Data)
The WHERE Clause Restrict the rows that are returned by using the WHERE clause The WHERE clause follows the FROM clause. The WHERE clause is used to filter records. he WHERE clause is used to extract only those records that fulfill a specified criterion SELECT column_name(s) FROM table_name WHERE column_name operator value
WHERE Clause Example The "Persons" table: we want to select only the persons living in the city "Sandnes" from the table above. SELECT * FROM Persons WHERE City='Sandnes' The result will look like this:
Character Strings and Dates Character strings and date values are enclosed by single quotation marks. Character values are case-sensitive, and date values are format-sensitive. The default date format is DD-MON- RR.
Quotes Around Text Fields SQL uses single quotes around text values (most database systems will also accept double quotes). Although, numeric values should not be enclosed in quotes. For text values: This is correct: SELECT * FROM Persons WHERE FirstName='Tove This is wrong: SELECT * FROM Persons WHERE FirstName=Tove For numeric values: This is correct: SELECT * FROM Persons WHERE Year=1965 This is wrong: SELECT * FROM Persons WHERE Year='1965'
Operators Allowed in the WHERE Clause (Comparison Conditions )
EMPLOYEES
Using Comparison Conditions SELECT last_name, salary FROM employees WHERE salary <= 3000 ;
Using the BETWEEN Condition Use the BETWEEN condition to display rows based on a range of values: SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500 ;
Using the IN Condition Use the IN membership condition to test for values in a list: SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201) ;
Using the LIKE Condition Use the LIKE condition to perform wildcard searches of valid search string values. Search conditions can contain either literal characters or numbers: % denotes zero or many characters. _ denotes one character. SELECT first_name FROM employees WHERE first_name LIKE 'S%' ;
Using the LIKE Condition You can combine pattern-matching characters: SELECT last_name FROM employees WHERE last_name LIKE '_o%' ;
Logical Conditions
Using the AND Operator The AND operator displays a record if both the first condition and the second condition is true we want to select only the persons with the first name equal to "Tove" AND the last name equal to "Svendson": SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson'
Using the OR Operator The OR operator displays a record if either the first condition or the second condition is true. Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to "Ola": SELECT * FROM Persons WHERE FirstName='Tove' OR FirstName='Ola'
Combining AND & OR You can also combine AND and OR (use parenthesis to form complex expressions). we want to select only the persons with the last name equal to "Svendson" AND the first name equal to "Tove" OR to "Ola" SELECT * FROM Persons WHERE LastName='Svendson' AND (FirstName='Tove' OR FirstName='Ola')
Using the NOT Operator SELECT last_name, job_id FROM employees WHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
Lab exercise Create Patient table as shown below:
Insert the following records:
Do the following queries present patient age that his id is 567 Present name of patients Present id of patient that his age 17 and name amal Present id of patient that his name begin with n Present phone number of maha Present name of patient that his doctor number is 1 or his name is amal Present different patient name Present patients id and their names as one column named patient data Present patients phone number for nada and maha