1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements.

Slides:



Advertisements
Similar presentations
Virtual training week 4 structured query language (SQL)
Advertisements

Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Structured Query Language and its components. SQL SQL stands for Structured Query Language. There is a standard SQL called the American National Standards.
Building a database. Implementation of desirable features Integrity –A field’s validation can be declared when the field is declared. If this validation.
Functions and string manipulation Lab 2 Week 2. Date functions Commonly used date functions are: –sysdate –next_day –add_months –last_day –months_between.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Populating and Querying tables Insert and mostly View (DML)
Some relationship types Using the Builder2 schema.
A Guide to Oracle9i1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Starting out The RDBMS we will be using is Oracle. It is held on the FERDIA server. The Oracle client is installed in all of the labs in the COMP domain.
Databases Week 1, lab 2 Simple selects. About the environment We are using SQL Server for the moment. The server we are using is: –Cian.student.comp.dit.ie.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.
Introduction to Structured Query Language (SQL)
Week 2 Lecture 2 Structure of a database. External Schema Conceptual Schema Internal Schema Physical Schema.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
Guide to Oracle10G1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
SQL Training SQL Statements – Part 1. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Explain the role of SQL.
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Oracle’s take on joins Where it differs from ANSI standard.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
ADO.NET Data Access. Page  2 SQL  When we interact with the datasource through ADO.NET we use the SQL language to retrieve,modify,update information.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
Agenda for Class 2/20/2014 Introduce Microsoft’s SQL Server database management system. Use the lab to discuss how to CREATE, DROP and populate (INSERT)
IS6146 Databases for Management Information Systems Lecture 1: Introduction to IS6146 Rob Gleasure robgleasure.com.
Populating and Querying tables Insert, Update, Delete and View (DML)
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Fundamentals of DBMS Notes-1.
SQL Query Getting to the data ……..
Chapter 5 Introduction to SQL.
Connect to SQL Server and run select statements
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
CIS16 Application Programming with Visual Basic
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
HAVING,INDEX,COMMIT & ROLLBACK
Lab 2: Retrieving Data from the Database
Presentation transcript:

1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements

2 Firstly… Finish lab 1. –You will now have two joined tables. VERY IMPORTANT! –If you get an error, SAVE IT in an ERROR.TXT file. When you find out what it means, type the explanation below it in the ERROR.TXT file. This way, if you come across it again, you’ll know what it means.

3 Next… Populate the tables. The following two slides give a sample of what could be in the tables. You need not use these values, but you must keep the values appropriate to the subject.

4 Supplier table SupplierIdSName 1001‘Chunky Choc’ 1002‘Juicy Jellies’ 1003‘Crunchy crisps’ 1004‘Fizzy drinks’

5 Product table Product Code ProductNameProduct Price SupplierId TCHOCheese&Onion€ TSVNSalt&Vinegar€ SOURJuicy Sours€ TBUFBuffalo Crisps€ PSTSFruit Pastilles 50g€ COLACola flavoured€ LMONLemonade€ CLALLarge Cola 1litre€ ORNGOrange flavoured€ PSTLFruit Pastiles 150g€

6 Deleting a row First try one that has no dependants. Look at the example. If you are using this, then one of the suppliers has no products. SQL> delete from supplier where supplierId = ; 1 row deleted.

7 Try to delete a row with dependants Attempt to delete the supplier row with supplierID 1003 SQL> delete from supplier where supplierId = 1003 *ERROR at line 1: ORA-02292: integrity constraint (POBYRNE.SUPPLIER) violated - child record found Because, there is one or more products depending on it: SQL> select * from product where supplierId = 1003; PRODUCTCODE PRODUCTNAME PRODUCTPRICE SUPPLIERID TCHO Cheese&Onion TSVN Salt&Vinegar TBUF Buffalo Crisps As with DROP, you cannot delete a row if rows in other tables depend on it.

8 Updating The Supplier called ‘Juicy Drinks’ has raised the price of all products by 10%. Update the price for the appropriate rows in the table. –See lecture week 1 semester 2.

9 Granting / revoke access To grant access to another user: –Connect to your schema I connected to builder –Enter a ‘grant’ for each object. I granted each of you access to the tables in the builder schema. For a student with a schema called jbloggs in the ft228 database, I entered the following: grant select on customer to jbloggs; grant select on supplier to jbloggs; grant select on staff to jbloggs; grant select on corder to jbloggs; etc. To reverse this: –Revoke select on customer from jbloggs;

10 Using a table from a different schema Any student who has been granted access to the table stock in the schema builder: –Select * from builder.stock; –The builder. Prefix tells the DBMS that you are looking outside your own schema. –SEE EXERCISE SLIDE AT THE END.

11 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO new_table_name] FROM table_list [WHERE search_conditions] [GROUP BY group_by_list] [HAVING search_conditions] [ORDER BY order_list [ASC | DESC] ] In this lesson, we will address only those clauses in black.

12 Select list Describes the columns of the result set. It is a comma-separated list of expressions. Each expression defines both –the format (data type and size) and –the source of the data for the result set column. Each select list expression is usually a reference to a column in the source table or view the data is coming from, but can be any other expression, such as a constant or a Transact- SQL function. Using the * expression in a select list specifies that all columns in the source table are returned.

13 FROM table_list Contains a list of the tables from which the result set data is retrieved. These sources can be: –Base tables or views in your own schema. –Base tables or views in schemae to which you have select access.

14 Simplest select Select * from E.g. Select * from builder.corderline;

15 WHERE search_conditions The WHERE clause is a filter that defines the conditions each row in the source tables must meet to qualify for the SELECT. Only rows that meet the conditions contribute data to the result set. Data from rows that do not meet the conditions are not used.

16 Selection Selection picks individual columns from a table:

17 Projection Projection picks only rows that satisfy a condition : SQL> select * from stock where stock_description like '%block%'; STOCK STOCK_DESCRIPTION UNIT_PRICE UNITCOSTPRICE STOCK_LEVEL REORDER_LEVEL SUPPLIER_ID A101 Cavity blocks(100) E101 Cavity blocks(500) E141 Cavity blocks(200) SQL> Note that each row wraps around, making it difficult to read.

18 Conditions And, or not Equal, not equal, greater than, less than. Like, using % and – –% is a wildcard string of any length –- is a single wildcard character;

19 Using like SQL> select stock_code from stock where stock_code like '_1%‘; STOCK A101 A111 B101 B111 C101 C121 D101 D131 E101 E rows selected.

20 Using like and underscore SQL> select stock_code from stock where stock_code like '_1__‘; STOCK A101 A111 B101 B111 C101 C121 D101 D131 E101 E rows selected. Note the change in position of the ‘1’: SQL> select stock_code from stock where stock_code like '__1_‘; STOCK A111 B111

21 Catalog SQL> select * from cat; TABLE_NAME TABLE_TYPE BIN$diXeGjXrQFi1kU/+ypNLsQ==$0 TABLE BIN$6btY4yriRdic8z2ONHRQrg==$0 TABLE BIN$Suh8BiYvRGuAAEdTnfho0w==$0 TABLE BIN$4DwYo9kfTciwcX3xGgcz3A==$0 TABLE CUSTOMER TABLE CORDER TABLE SUPPLIER TABLE STOCK TABLE SORDERLINE TABLE STAFF TABLE SORDER TABLE TABLE_NAME TABLE_TYPE CORDERLINE TABLE BIN$CW8kb+b3Q+WljFuHIBZosQ==$0 TABLE BIN$Jb8z6PBiTL6Hy8yXBB8W3g==$0 TABLE BIN$5AZXhMuMS4mosvy8TqkfqQ==$0 TABLE BIN$hkvkMrD9RrqOMe4W2key9g==$0 TABLE BIN$pFcvgvUlSDmjAyQPR5Gz7A==$0 TABLE BIN$Pk3/ceHkRvOH06D3q/6WeA==$0 TABLE BIN$kZs92llMTwKdMcBRyIwlbg==$0 TABLE BIN$L4P807SFTNm63jXRmPA7XA==$0 TABLE BIN$oV13pVZNQr+ow/hMlzjzww==$0 TABLE BIN$K4j8mchgSh6CseBlwvlJxw==$0 TABLE TABLE_NAME TABLE_TYPE BIN$w54kQhBiQciAp90csiFiWw==$0 TABLE BIN$E+DmYcaIQxiLOa0euxpTaA==$0 TABLE BIN$WtulD5u9T2CdH/eYXdC6PQ==$0 TABLE BIN$sUogRgsJT221fbL/tFGcCw==$0 TABLE BIN$gCyduUbEQrOk1DVLgPpr2Q==$0 TABLE BIN$/uY2loJiTGu3UFW+NjUBxQ==$0 TABLE BIN$lv9wTq5gRmGK2th8kl4hnw==$0 TABLE 29 rows selected. This is unclear and unhelpful. We don’t want to see the dropped tables. See following slide.

22 Only get the current tables: SQL> select * from cat where table_name not like 'BIN$%‘; TABLE_NAME TABLE_TYPE CUSTOMER TABLE CORDER TABLE SUPPLIER TABLE STOCK TABLE SORDERLINE TABLE STAFF TABLE SORDER TABLE CORDERLINE TABLE 8 rows selected.

23 Miscellaneous To denote a string, use single quotes. Double quotes are not recognised. If you want to embed a single quote (i.e. an apostrophe!) in a string, precede it with another single quote (see slide 3 for example). –Note: If you copy single quotes from word, they don’t work, but they work from Notepad. You do not need to use quotes for numeric fields: –Select * from Product where UnitPrice <5

24 Sorting The order by clause will allow you to sort the fields. The default order is ascending. To change it to descending, use the keyword desc.

25 Select without ordering SQL> select * from corderline; QUANTITYREQUIRED CORDERNO STOCK A B C C D D E E BRK A rows selected.

26 Select with ordering SQL> select * FROM CORDERLINE ORDER BY STOCK_CODE; QUANTITYREQUIRED CORDERNO STOCK A A B BRK C C D D E E rows selected.

27 Reversing the order: SQL> select * from corderline order by stock_code desc; QUANTITYREQUIRED CORDERNO STOCK E E D D C C BRK B A A rows selected.