Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements."— Presentation transcript:

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

2 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 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 4 Supplier table SupplierIdSName 1001‘Chunky Choc’ 1002‘Juicy Jellies’ 1003‘Crunchy crisps’ 1004‘Fizzy drinks’

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

6 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 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 0.8000000 1003 TSVN Salt&Vinegar 0.8000000 1003 TBUF Buffalo Crisps 0.8000000 1003 As with DROP, you cannot delete a row if rows in other tables depend on it.

8 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 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 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 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 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 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 14 Simplest select Select * from E.g. Select * from builder.corderline;

15 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 16 Selection Selection picks individual columns from a table:

17 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) 200 500 300 100 501 E101 Cavity blocks(500) 1000 500 172 200 501 E141 Cavity blocks(200) 400 130 300 200 501 SQL> Note that each row wraps around, making it difficult to read.

18 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 19 Using like SQL> select stock_code from stock where stock_code like '_1%‘; STOCK ----- A101 A111 B101 B111 C101 C121 D101 D131 E101 E141 10 rows selected.

20 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 E141 10 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 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 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 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 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 25 Select without ordering SQL> select * from corderline; QUANTITYREQUIRED CORDERNO STOCK ---------------- ---------- ----- 20 101 A101 47 103 B111 1 103 C101 3 103 C121 1 105 D101 2 107 D131 2 103 E101 5 108 E101 200 101 BRK11 20 106 A111 10 rows selected.

26 26 Select with ordering SQL> select * FROM CORDERLINE ORDER BY STOCK_CODE; QUANTITYREQUIRED CORDERNO STOCK ---------------- ---------- ----- 20 101 A101 20 106 A111 47 103 B111 200 101 BRK11 1 103 C101 3 103 C121 1 105 D101 2 107 D131 2 103 E101 5 108 E101 10 rows selected.

27 27 Reversing the order: SQL> select * from corderline order by stock_code desc; QUANTITYREQUIRED CORDERNO STOCK ---------------- ---------- ----- 2 103 E101 5 108 E101 2 107 D131 1 105 D101 3 103 C121 1 103 C101 200 101 BRK11 47 103 B111 20 106 A111 20 101 A101 10 rows selected.


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

Similar presentations


Ads by Google