Lists, Views, Describing tables Kris Pepper. Where are we Finishing up integrity, sets and lists, and now adding in views and describe. Finishing homework.

Slides:



Advertisements
Similar presentations
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Virtual training week 4 structured query language (SQL)
CSC443 Integrity Set operations List operations. Review Commands: Create Drop Alter Insert Select.
Alter an existing Table Note: (a) Primary key: SID + Course_code.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
More sql Session 4 – subselects, union, aggregate functions.
SQL’s Data Definition Language (DDL) n DDL statements define, modify and remove objects from data dictionary tables maintained by the DBMS n Whenever you.
11 Copyright © 2004, Oracle. All rights reserved. Managing Objects with Data Dictionary Views.
Managing Objects with Data Dictionary Views
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Sections 10 – Constraints
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Oracle Data Definition Language (DDL)
The Relational Model These slides are based on the slides of your text book.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL: DDL John Ortiz Cs.utsa.edu.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Programming Sections 9 – Constraints. Marge Hohly2 CONSTRAINT TYPES  NOT NULL Constraints  UNIQUE Constraints  PRIMARY KEY Constraints  FOREIGN.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Oracle 11g: SQL Chapter 4 Constraints.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
More on Primary and Foreign Keys Please see speaker notes for additional information!
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
11 Copyright © 2006, Oracle. All rights reserved. Managing Objects with Data Dictionary Views.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
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.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Oracle Developer. Create Table SQL> create table catalog ( 2 cno varchar2(7), 3 ctitle varchar2(50), 4 primary key (cno)); Table created. SQL> describe.
Database Programming Sections 9 & 10 – DDL Data Definition Language,
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Including Constraints
Insert, Update and the rest…
Managing Objects with Data Dictionary Views
Managing Objects with Data Dictionary Views
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Oracle Data Definition Language (DDL)
Database Management System
Instructor: Samia arshad
Presentation transcript:

Lists, Views, Describing tables Kris Pepper

Where are we Finishing up integrity, sets and lists, and now adding in views and describe. Finishing homework 3. Starting final project.

LIST Compare two lists of similar attributes IN >ANY >SOME >ALL

List Comparison - in Compare to values in a list - in or not in List student first names of sid 1111 and 2222 Select distinct fname from students where SID in (1111, 2222); Result: Nandita Sydney SID SIDName 1111Nandita 2222Sydney 3333Susan 4444Naveen 5555Elad 6666Lincoln 7777new

List Comparison - in Can use a subquery to create the list: List student first names who are enrolled in term f96 Select distinct SID from enrolls where term = ‘f96’); Select fname from students where sid in (select distinct sid from enrolls where term = ‘f96’); RESULT: FNAME Nandita Sydney Susan Naveen SID SIDName 1111Nandita 2222Sydney 3333Susan 4444Naveen 5555Elad 6666Lincoln 7777new

Try One List first names of students in course 1030, 1031 or 1035 Steps: 1: write the base query on students ending with IN (a list of SIDS in the course). 2: write a query of enrolls listing sids in these courses.

Answer List List first names of students in course 1030, 1031 or 1035 Steps: 1: write the base query on students ending with IN (a list of SIDS in the course). Select fname from students where sid in (a list of SIDS in the course) 2: write a query of enrolls listing sids in these courses. Select distinct sid from enrolls where lineno in (1030, 1031, 1035) 3. Put it together: Select fname from students where sid in (Select distinct sid from enrolls where lineno in (1030, 1031, 1035));

List Comparison - any, all, some >,,=, >=, <= Select student ids with scores greater than all in the list: Select distinct sid from scores where points >= all (90, 340, 70) Substitute a query listing all points

List Comparison - any, all, some Select scores greater than all in the list: Select distinct sid from scores where points >= all (select points from scores); 1111 You try: List student ids with scores less than all in the list

List Comparison - any, all, some Answer: Select distinct sid from scores where points <= all (select points from scores); 2222 Try one more: List student ids with scores which are greater than the average score

List Comparison - any, all, some Not a list answer Answer: Select distinct sid from scores where points < (select avg(points) from scores);

Sub-Query using outer Sub query acts for every row in the main (outer) query Use the fields from the outer query in the inner query List of courses in which all individual components grades are higher than B: Select term, lineno, cno from courses where b <all (select points from scores where scores.lineno = courses.lineno and scores.term = courses.term);

Exists If subquery has any row in result, then exists is TRUE If no row results, then FALSE Select all courses in which 1111 is enrolled: select distinct cno from courses where exists (select 'a' from enrolls where courses.lineno = enrolls.lineno and sid = ‘1111’); You try: list all the fields from the student table where any score is greater than 90. (use exists)

Exists Answer: select * from students where exists (select ‘a’ from scores where students.sid = scores.sid and points > 90);

Important Stuff Database checks integrity Subqueries help make lists for where conditions In, All, Some, Any Exists

Views Like creating a new table, but only doesn’t physically hold data. Can use in other queries. Format: CREATE VIEW Viewname AS Select statement

VIEWS - SAMPLE SQL> create view sids_in_course as select distinct sid from enrolls where lineno in (1030, 1031, 1035); View created. SQL> select * from sids_in_course; SID Use it in the query from before: Select fname from students where sid in (select * from sids_in_course);

VIEWS Try one: Create a new view called course_with_name listing all the courses with the course number and name (linking courses and catalog) When you are done, format the columns: column ctitle format a10 column lineno format 9999 Select * from course_with_name; Select * from cat; Create view course_with_name as select courses.*, ctitle from courses, catalog where courses.cno = catalog.cno; Select * from course_with_name

Describe tables Define with CREATE, ALTER, DROP Describe command shows basic info All definitions stored in system tables TableContents catTable names user_objectsTable level info (last changed date, etc) Also, all other types of ojects, like triggers and views user_tablesTable level info (Space and statistics) colsField definition information user_viewsSee complete view definition user_constraintsConstraints (primary key, foreign key and check) User_cons_columnsFields that are constrained

Some commands to describe select * from cat; select view_name, text from user_views;

cols SQL> column table_name format a10 SQL> column column_name format a10 SQL> column data_type format a10 SQL> column data_default format a10 select table_name, column_name, data_type, data_length, data_precision, data_default, nullable from cols TABLE_NAME COLUMN_NAM DATA_TYPE DATA_LENGTH DATA_PRECISION DATA_DEFAU N ODETAILS QTY NUMBER 22 Y ORDERS ONO NUMBER 22 5 N ORDERS CNO NUMBER 22 5 Y

User_cons_columns select column_name, constraint_name, position from user_cons_columns where table_name = 'ORDERS'; COLUMN_NAM CONSTRAINT_NAME POSITION ONO SYS_C ONO SYS_C CNO SYS_C ENO SYS_C

user_constraints Table_name = table that has the constraint R_constraint_name = foreign key constraint Constraint_type = P = primary key; R = foreign key

Query on user_constraints select constraint_name, constraint_type, r_constraint_name, search_condition from user_constraints where table_name = 'COURSES‘ CONSTRAINT_NAME CONSTRAINT R_CONSTRAI SEARCH_CON SYS_C C "TERM" IS NOT NULL SYS_C C "LINENO" IS NOT NULL SYS_C C "CNO" IS NOT NULL SYS_C C a > 0 SYS_C C b > 0 SYS_C C c > 0 SYS_C C d > 0 SYS_C P SYS_C R SYS_C00113

Showing primary keys select f.table_name, f.constraint_name, f.column_name from user_constraints k, user_cons_columns f where k.table_name = f.table_name and k.constraint_name = f.constraint_name and k.constraint_type = 'P' ; TABLE_NAME CONSTRAINT_NAME COLUMN_NAM COMPONENTS SYS_C TERM COMPONENTS SYS_C LINENO COMPONENTS SYS_C COMPNAME ENROLLS SYS_C SID ENROLLS SYS_C TERM ENROLLS SYS_C LINENO

Foreign keys TABLE_NAME CONSTRAINT_NAME COLUMN_NAM COMPONENTS SYS_C LINENO COMPONENTS SYS_C COMPNAME ENROLLS SYS_C SID ENROLLS SYS_C TERM ENROLLS SYS_C LINENO TEST9 SYS_C FIELD1 select f.table_name, f.constraint_name, f.column_name from user_constraints k, user_cons_columns f where k.r_constraint_name = f.constraint_name and k.constraint_type = 'R' ;

Checks select f.table_name, f.constraint_name, f.column_name, k.search_condition from user_constraints k, user_cons_columns f where k.table_name = f.table_name and k.constraint_name = f.constraint_name and k.constraint_type = 'C' ; TABLE_NAME CONSTRAINT_NAME COLUMN_NAM SEARCH_CON STUDENTS SYS_C LNAME "LNAME" IS NOT NULL COURSES SYS_C TERM "TERM" IS NOT NULL COURSES SYS_C LINENO "LINENO" IS NOT NULL

Important Stuff Database checks integrity Subqueries help make lists for where conditions In, All, Some, Any Exists Views remain Data dictionary can be queried