1 databases semester 2 week 1 lab 2 the delete, grant, revoke and select statements
Post on 21-Dec-2015
214 views
TRANSCRIPT
![Page 1: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/1.jpg)
1
Databases Semester 2Week 1 Lab 2
The Delete, grant, revoke and Select Statements
![Page 2: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/2.jpg)
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.
![Page 3: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/3.jpg)
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.
![Page 4: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/4.jpg)
4
Supplier table
SupplierId SName
1001 ‘Chunky Choc’
1002 ‘Juicy Jellies’
1003 ‘Crunchy crisps’
1004 ‘Fizzy drinks’
![Page 5: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/5.jpg)
5
Product tableProductCode
ProductName ProductPrice
SupplierId
TCHO Cheese&Onion €.80 1003
TSVN Salt&Vinegar €.80 1003
SOUR Juicy Sours €.75 1002
TBUF Buffalo Crisps €.80 1003
PSTS Fruit Pastilles 50g €.55 1002
COLA Cola flavoured €1.00 1004
LMON Lemonade €1.00 1004
CLAL Large Cola 1litre €1.50 1004
ORNG Orange flavoured €1.00 1004
PSTL Fruit Pastiles 150g €1.50 1002
![Page 6: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/6.jpg)
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 = <you decide>;
1 row deleted.
![Page 7: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/7.jpg)
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 1003TSVN Salt&Vinegar 0.8000000 1003TBUF Buffalo Crisps 0.8000000 1003
• As with DROP, you cannot delete a row if rows in other tables depend on it.
![Page 8: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/8.jpg)
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.
![Page 9: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/9.jpg)
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;
![Page 10: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/10.jpg)
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.
![Page 11: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/11.jpg)
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.
![Page 12: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/12.jpg)
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.
![Page 13: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/13.jpg)
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.
![Page 14: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/14.jpg)
14
Simplest select
• Select * from <table-name>• E.g. Select * from builder.corderline;
![Page 15: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/15.jpg)
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.
![Page 16: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/16.jpg)
16
Selection
• Selection picks individual columns from a table:
![Page 17: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/17.jpg)
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 501E101 Cavity blocks(500) 1000 500 172
200 501E141 Cavity blocks(200) 400 130 300
200 501SQL>
• Note that each row wraps around, making it difficult to read.
![Page 18: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/18.jpg)
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;
![Page 19: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/19.jpg)
19
Using likeSQL> select stock_code from stock where stock_code like
'_1%‘;STOCK-----A101A111B101B111C101C121D101D131E101E14110 rows selected.
![Page 20: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/20.jpg)
20
Using like and underscore
SQL> select stock_code from stock where stock_code like '_1__‘;
STOCK-----A101A111B101B111C101C121D101D131E101E141
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
![Page 21: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/21.jpg)
21
CatalogSQL> select * from cat;
TABLE_NAME TABLE_TYPE------------------------------ -----------BIN$diXeGjXrQFi1kU/+ypNLsQ==$0 TABLEBIN$6btY4yriRdic8z2ONHRQrg==$0 TABLEBIN$Suh8BiYvRGuAAEdTnfho0w==$0 TABLEBIN$4DwYo9kfTciwcX3xGgcz3A==$0 TABLECUSTOMER TABLECORDER TABLESUPPLIER TABLESTOCK TABLESORDERLINE TABLESTAFF TABLESORDER TABLE
TABLE_NAME TABLE_TYPE------------------------------ -----------CORDERLINE TABLEBIN$CW8kb+b3Q+WljFuHIBZosQ==$0 TABLEBIN$Jb8z6PBiTL6Hy8yXBB8W3g==$0 TABLEBIN$5AZXhMuMS4mosvy8TqkfqQ==$0 TABLEBIN$hkvkMrD9RrqOMe4W2key9g==$0 TABLEBIN$pFcvgvUlSDmjAyQPR5Gz7A==$0 TABLEBIN$Pk3/ceHkRvOH06D3q/6WeA==$0 TABLEBIN$kZs92llMTwKdMcBRyIwlbg==$0 TABLEBIN$L4P807SFTNm63jXRmPA7XA==$0 TABLEBIN$oV13pVZNQr+ow/hMlzjzww==$0 TABLEBIN$K4j8mchgSh6CseBlwvlJxw==$0 TABLE
TABLE_NAME TABLE_TYPE------------------------------ -----------BIN$w54kQhBiQciAp90csiFiWw==$0 TABLEBIN$E+DmYcaIQxiLOa0euxpTaA==$0 TABLEBIN$WtulD5u9T2CdH/eYXdC6PQ==$0 TABLEBIN$sUogRgsJT221fbL/tFGcCw==$0 TABLEBIN$gCyduUbEQrOk1DVLgPpr2Q==$0 TABLEBIN$/uY2loJiTGu3UFW+NjUBxQ==$0 TABLEBIN$lv9wTq5gRmGK2th8kl4hnw==$0 TABLE
29 rows selected.
• This is unclear and unhelpful. We don’t want to see the dropped tables. See following slide.
![Page 22: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/22.jpg)
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.
![Page 23: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/23.jpg)
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
![Page 24: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/24.jpg)
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.
![Page 25: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/25.jpg)
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.
![Page 26: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/26.jpg)
26
Select with orderingSQL> 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 E10110 rows selected.
![Page 27: 1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements](https://reader030.vdocuments.pub/reader030/viewer/2022032521/56649d575503460f94a365ae/html5/thumbnails/27.jpg)
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.