Database CIS 340 LAB#2. I.Arwa Najdi

Similar documents
Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Learning MySQL! Angola Africa SELECT name, gdp/population FROM world WHERE area > !

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

SQL. Short introduction

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

A basic create statement for a simple student table would look like the following.

A table is a collection of related data entries and it consists of columns and rows.

Oracle Database 10g: Introduction to SQL

Information Systems SQL. Nikolaj Popov

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Oracle SQL. Course Summary. Duration. Objectives

Appendix A Practices and Solutions

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Displaying Data from Multiple Tables

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Database Query 1: SQL Basics

Oracle Database 12c: Introduction to SQL Ed 1.1

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals

Where? Originating Table Employees Departments

CHAPTER 12. SQL Joins. Exam Objectives

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Using AND in a Query: Step 1: Open Query Design

Oracle Database 10g Express

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

Introduction to Microsoft Jet SQL

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

GET DATA FROM MULTIPLE TABLES QUESTIONS

CSC 443 Data Base Management Systems. Basic SQL

How To Create A Table In Sql (Ahem)

A Brief Introduction to MySQL

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

Structured Query Language (SQL)

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Advanced Query for Query Developers

Microsoft Access 3: Understanding and Creating Queries

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Microsoft Access Lesson 5: Structured Query Language (SQL)

Teach Yourself InterBase

Using SQL Queries in Crystal Reports

Oracle 10g PL/SQL Training

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Access Queries (Office 2003)

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

Darshan Institute of Engineering & Technology PL_SQL

3.GETTING STARTED WITH ORACLE8i

Netezza SQL Class Outline

Oracle For Beginners Page : 1

Programming with SQL

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

2. Which of the following declarations is invalid? Mark for Review (1) Points

Oracle Database: Introduction to SQL

5. CHANGING STRUCTURE AND DATA

Oracle Database: SQL and PL/SQL Fundamentals NEW

Concepts Design Basics Command-line MySQL Security Loophole

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

SQL Injection. The ability to inject SQL commands into the database engine through an existing application

DbSchema Tutorial with Introduction in SQL Databases

Relational Database: Additional Operations on Relations; SQL

Multimedia im Netz Online Multimedia Winter semester 2015/16

Oracle Database 11g SQL

Chapter 1. Writing Basic. SQL Statements

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Admin Guide Product version: Product date: November, Technical Administration Guide. General

MOC 20461C: Querying Microsoft SQL Server. Course Overview

SQL Programming. Student Workbook

What is the value of SQL%ISOPEN immediately after the SELECT statement is executed? Error. That attribute does not apply for implicit cursors.

SQL SELECT Query: Intermediate

FileMaker 13. SQL Reference

Database Programming with PL/SQL: Learning Objectives

SQL Introduction Chapter 7, sections 1 & 4. Introduction to SQL. Introduction to SQL. Introduction to SQL

Database Administration with MySQL

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Database Migration from MySQL to RDM Server

Paper An Introduction to SAS PROC SQL Timothy J Harrington, Venturi Partners Consulting, Waukegan, Illinois

Performing Queries Using PROC SQL (1)

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

How Strings are Stored. Searching Text. Setting. ANSI_PADDING Setting

How to Improve Database Connectivity With the Data Tools Platform. John Graham (Sybase Data Tooling) Brian Payton (IBM Information Management)

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

MS Access Lab 2. Topic: Tables

T-SQL STANDARD ELEMENTS

ATTACHMENT 6 SQL Server 2012 Programming Standards

Advance DBMS. Structured Query Language (SQL)

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Transcription:

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