* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Slide 1
Microsoft Jet Database Engine wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Oracle Database wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Clusterpoint wikipedia , lookup
Relational algebra wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Database model wikipedia , lookup
Open Database Connectivity wikipedia , lookup
What does DDL stand for? • Data Definition Language Valid or invalid? INVALID CREATE TABLE p_order_line ( ORDER_NUM char(5) NOT NULL PRIMARY KEY, PART_NUM char(4) NOT NULL PRIMARY KEY, NUM_ORDERED decimal(3,0) NULL, QUOTED_PRICE decimal(6,2) NULL ) The MySQL DBMS requires a concatenated primary key to be specified at the end of the table definition CREATE TABLE p_order_line ( ORDER_NUM char(5) NOT NULL, PART_NUM char(4) NOT NULL, NUM_ORDERED decimal(3,0) NULL, QUOTED_PRICE decimal(6,2) NULL, PRIMARY KEY (ORDER_NUM,PART_NUM) ) Valid or Invalid? INVALID H_BOOK Field BOOK_CODE TITLE PUBLISHER_CODE TYPE PRICE PAPERBACK Type char(4) char(40) char(3) char(3) decimal(4,2) char(1) Null NO YES YES YES YES YES Key PRI Default Extra insert into H_BOOK values ('32AB', 'The Audacity of Hope', 'ABY') If you do not specify the columns you are inserting data into SQL expects there to be a value for each column that exists in the table insert into H_BOOK (BOOK_CODE, TITLE, PUBLISHER_CODE) values ('32AB', 'The Audacity of Hope', 'ABY') Which clause changes the characteristics of an existing column? • MODIFY clause of ALTER TABLE command changes characteristics of existing columns What does SQL stand for? • Structured Query Language What does DBMS stand for? • Database Management System Name an example of a DBMS besides MySQL • • • • Oracle Microsoft SQL Server Access DB2 What are some benefits of views? What is a database? • Database: structure containing categories of information and relationships between these categories 10 Normalization • minimize duplication of data • help safeguard against data anomalies • improve performance for certain activities 11 What is the name of this type of notation Crow’s Foot Notation 12 Coalesce() • Returns the first non-NULL value in the list, or NULL if there are no non-NULL values. Example: select coalesce(null, null, 'Kris') Student_Names Student_Phones select sn.first_name, sn.last_name, coalesce(busn.phone_number, cell.phone_number, home.phone_number) as "phone" from student_names sn left outer join student_phones home on home.student_id = sn.student_id and home.phone_type = 'home' left outer join student_phones cell on cell.student_id = sn.student_id and cell.phone_type = 'cell' left outer join student_phones busn on busn.student_id = sn.student_id and busn.phone_type = 'busn' Results Oracle Platform Note • In MySQL and Microsoft SQL Server it is possible to write/run a query that does not reference a table Example: select 'kris' Oracle Platform Note • In Oracle the query must reference a table. We can use the “dummy” table DUAL, which is provided in Oracle, as our referenced table. Example: select 'kris‘ From Dual SQL Challenge #1 • Return a list of just city names from the table Locations • Note: the format of all values in the Locations table is as follows: City, State abbreviation – hints • left() • locate() • http://dev.mysql.com/doc/refman/5.5/en/ SQL Challenge #1 select left(city_state, locate(',', city_state) - 1) from locations SQL Challenge #2 • Return a distinct list of first & last names of boat owners who pay a rental fee that is less than the maximum paid rental fee and greater than the minimum rental fee paid – Hints • Use a select statement • 2 subqueries SQL Challenge #2 select distinct first_name, last_name from a_owner o join a_marina_slip ms on ms.owner_num = o.owner_num and ms.rental_fee < (select max(rental_fee) from a_marina_slip) and ms.rental_fee > (select min(rental_fee) from a_marina_slip) SQL Challenge #2 select distinct first_name, last_name from a_owner o join a_marina_slip ms on ms.owner_num = o.owner_num where ms.rental_fee < (select max(rental_fee) from a_marina_slip) and ms.rental_fee > (select min(rental_fee) from a_marina_slip) SQL Challenge #3 • Alexamara is considering applying the following discounts to it’s rental fees – rental fee > 3000 gets a 10% discount – rental fee > 2000 & < 3000 gets a 5% discount – all other rental fees receive no discount • Before applying the discounts they want to know how much money each marina would bring in prior to the discount and after the discount • Write a single query that will show them the before discount total and after discount total by marina name SQL Challenge #3 • Hints – query will be returning 2 different sums – can place a case statement within a sum() function – in phpmyadmin the opening parenthesis needs to be on same line as the word sum SQL Challenge #3 • Pseudocode select name, sum(non-discounted rental fees), sum(discounted rental fees) from marina slip join marina on matching marina nums group by name SQL Challenge #3 • Pseudocode select name, sum(non-discounted rental fees), sum(discounted rental fees) from marina slip join marina on matching marina nums group by name case statement goes here SQL Challenge #3 select name, sum(rental_fee) as 'before', sum( case when rental_fee > 3000 then rental_fee * .9 when rental_fee > 2000 then rental_fee * .95 else rental_fee end) as 'after' from a_marina_slip ms join a_marina m on m.marina_num = ms.marina_num group by name User Defined Stored Procedures and Functions • Sometimes referred to as routines • Created by users • Not to be confused with system procedures and functions – DBMS has its own set of procedures/functions used to manage the database Procedures/functions are similar to procedures/functions in other programming languages in that they can: • Utilize input and output parameters • Call other procedures and functions. • Allow for modular programming Example Function delimiter // create function calculate_annual_salary ( weekly_hrs decimal(10,2), pay_rt decimal(10,2) ) returns decimal(16,2) begin declare annual_salary decimal(16,2); set annual_salary = weekly_hrs * pay_rt * 52; return annual_salary; end What will the following query return? select calculate_annual_salary(40.0, 10.00) as 'annual salary' Function Definition delimiter // create function calculate_annual_salary ( weekly_hrs decimal(10,2), pay_rt decimal(10,2) ) returns decimal(16,2) begin declare annual_salary decimal(16,2); set annual_salary = weekly_hrs * pay_rt * 52; return annual_salary; end Results annual salary 20800.00 What will the following query return? select calculate_annual_salary(40.0, 20.00) as 'annual salary' Function Definition delimiter // create function calculate_annual_salary ( weekly_hrs decimal(10,2), pay_rt decimal(10,2) ) returns decimal(16,2) begin declare annual_salary decimal(16,2); set annual_salary = weekly_hrs * pay_rt * 52; return annual_salary; end Results annual salary 41600.00 System Catalog • Set of database objects (tables, views, procedures, etc.) used by the DBMS to store and utilize information about databases • Contains metadata – metadata is data about data • System catalog contains very useful information select concat('select * from ', TABLE_NAME) from INFORMATION_SCHEMA.TABLES order by table_schema desc, table_name What is Dynamic SQL? • SQL statements constructed at runtime and passed to the database management system for execution; generating SQL statements on the fly • Can be useful • Can make you very vulnerable to security threats if not used properly