Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Oracle Database wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Concurrency control wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Functional Database Model wikipedia , lookup
Clusterpoint wikipedia , lookup
Databases & SQL By: Julia Tartakovsky CS 340, Fall 2004 Acknowledgement (for help with the J# code connecting to an Oracle DB): Yosef Lifshits 1 Database Management System (DBMS) A DBMS is: – A collection of interrelated data (a database), and – A set of programs to access the data DBMS provides an environment that is both convenient and efficient to use. Database Applications: – – – – – – Banking: all transactions Airlines: reservations, schedules Universities: registration, grades Sales: customers, products, purchases Manufacturing: production, inventory, orders, supply chain Human resources: employee records, salaries, tax deductions 2 Purpose of Database System In the early days, database applications were built on top of file systems – We are managing data in this way even today – E.g. using Windows Explorer to find your files Drawbacks of using file systems to store data: – Data redundancy and inconsistency Multiple file formats, duplication of information in different files – Difficulty in accessing data Need to write a new program to carry out each new task – Data isolation — multiple files and formats – Integrity problems Integrity constraints (e.g. account balance > 0) become part of program code Hard to add new constraints or change existing ones 3 Purpose of Database Systems (Cont.) Drawbacks of using file systems (cont.) – Atomicity of updates Failures may leave database in an inconsistent state with partial updates carried out E.g. transfer of funds from one account to another should either complete or not happen at all – Concurrent access by multiple users Concurrent accessed needed for performance Uncontrolled concurrent accesses can lead to inconsistencies – E.g. two people reading a balance and updating it at the same time – Security problems Database systems offer solutions to all the above problems 4 Application Architectures Two-tier architecture: E.g. client programs using ODBC/JDBC to communicate with a database Three-tier architecture: E.g. web-based applications, and applications built using “middleware” 5 Levels of Abstraction Physical level describes how a record (e.g., customer) is stored. Logical level: describes data stored in database, and the relationships among the data. type customer = record name : string; street : string; city : integer; end; View level: application programs hide details of data types. Views can also hide information (e.g., salary) for security purposes. 6 View of Data An architecture for a database system 7 Schemas and Instances Analogous to types and variables in programming languages Schema – the logical structure of the database – e.g., the database consists of information about a set of customers and accounts and the relationship between them – Analogous to type information of a variable in a program – Physical schema: database design at the physical level – Logical schema: database design at the logical level Instance – the actual content of the database at a particular point in time – Analogous to the value of a variable Physical Data Independence – the ability to modify the physical schema without changing the logical schema – Applications depend on the logical schema – In general, the interfaces between various levels and components should be well defined so that changes in some parts do not seriously influence others. 8 Data Models A collection of conceptual tools for describing – – – – data data relationships data semantics data constraints Entity-Relationship model Relational model Other models: – object-oriented model – Object-relational model – semi-structured data models, XML 9 Entity-Relationship Model Example of schema in the entity-relationship model: 10 Entity Relationship Model (Cont.) E-R model of real world – Entities (objects) E.g. customers, accounts – Relationships between entities E.g. Account A-101 is held by customer Johnson Relationship set depositor associates customers with accounts Widely used for database design – Database design in E-R model usually converted to design in the relational model (coming up next) which is used for storage and processing 11 Relational Model A collection of tables Example of tabular data in the relational model Customerid customername 192-83-7465 Johnson 019-28-3746 Smith 192-83-7465 Johnson 321-12-3123 Jones 019-28-3746 Smith customerstreet customercity Attributes accountnumber Alma Palo Alto A-101 North Rye A-215 Alma Palo Alto A-201 Main Harrison A-217 North Rye A-201 12 A Sample Relational Database 13 What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database. SQL statements are used to perform tasks such as: – update data on a database (an action query) – retrieve data from a database (a select query) 14 What is SQL? (cont.) Relational database management systems that use SQL: – – – – – Oracle Sybase Microsoft SQL Server Access Ingres Standard SQL commands: – – – – – – "Select“ "Insert" "Update“ "Delete“ "Create“ "Drop" 15 SQL SQL: widely used non-procedural language – E.g. find the name of the customer with customer-id 192-83-7465 select customer.customer-name from customer where customer.customer-id = ‘192-83-7465’ – E.g. find the balances of all accounts held by the customer with customer-id 192-83-7465 select account.balance from depositor, account where depositor.customer-id = ‘192-83-7465’ and depositor.account-number = account.account-number 16 SELECT For more examples and an interactive environment, go to: http://sqlcourse.com/select.html SELECT FROM WHERE customer_id, name CUSTOMER birth_year>=1975 CUSTOMER name birth year address 1 John 1975 Kowloon 2 Smith 1955 Kowloon 1 John 3 Joan 1980 Hong Kong 3 Joan 4 Bobby 1976 Hong Kong 4 Bobby 5 Shirley 1974 Hong Kong customer id RESULT customer id name 17 SELECT with “distinct” Eliminate identical records. RESULT SELECT FROM address CUSTOMER address Kowloon CUSTOMER Kowloon name birth year address Hong Kong 1 John 1975 Kowloon Hong Kong 2 Smith 1955 Kowloon Hong Kong 3 Joan 1980 Hong Kong 4 Bobby 1976 Hong Kong 5 Shirley 1974 Hong Kong customer id SELECT distinct address CUSTOMER FROM RESULT address Kowloon Hong Kong 18 Aggregate with distinct CUSTOMER name birth year address 1 John 1975 Kowloon 2 Smith 1955 Kowloon 3 Joan 1980 Hong Kong 4 Bobby 1976 Hong Kong 5 Shirley 1974 Hong Kong customer id For more examples and an interactive environment, go to: SELECT FROM count(address) CUSTOMER ANSWER: 5 SELECT FROM count(distinct address) CUSTOMER ANSWER: 2 http://sqlcourse2.com/agg_functions.html 19 Group-by Grouping tuples with identical attributes. RESULT - GROUP BY customer_id ACCOUNT account id customer id A1 A2 A3 A4 A5 type balance 1 saving 1 check 5k 2 saving 35k 3 3 saving check account id customer id balance A1 1 saving 20k A2 1 check 5k type balance saving 35k type balance 20k 100k type account id customer id A3 2 account id customer id 30k A4 3 saving 100k A5 3 check 30k For more examples and an interactive environment, go to: http://sqlcourse2.com/groupby.html 20 Summarized information by groups SELECT customer_id, COUNT(*) FROM ACCOUNT GROUP BY customer_id GROUP BY customer_id account id customer id 1 saving 20k A2 1 check 5k type balance saving 35k type balance A3 2 account id customer id customer id balance A1 account id customer id RESULT type A4 3 saving 100k A5 3 check 30k COUNT 1 2 2 1 3 2 21 Join ACCOUNT CUSTOMER customer id SELECT FROM WHERE * CUSTOMER, ACCOUNT CUSTOMER.customer_id = ACCOUNT.customer_id The result table removes meaningless tuples from the cartesian product. name 1 John 2 Smith 3 Joan account id customer id balance A1 1 20k A2 1 5k A3 2 35k A4 3 100k JOIN RESULT ACCOUNT CUSTOMER customer id name account id customer id balance 1 John A1 1 20k 1 John A2 1 5k 2 Smith A3 2 35k 3 Joan A4 3 23 100k Join with group-by and having JOIN RESULT SELECT FROM WHERE * CUSTOMER, ACCOUNT CUSTOMER.customer_id = ACCOUNT.customer_id ACCOUNT CUSTOMER customer id CUSTOMER.name, sum(ACCOUNT.balance) FROM CUSTOMER, ACCOUNT WHERE CUSTOMER.customer_id = ACCOUNT.customer_id GROUP BY CUSTOMER.name HAVING COUNT(*)=2 SELECT QUESTION: What is the meaning of this query? name account id customer id balance 1 John A1 1 20k 1 John A2 1 5k 2 Smith A3 2 35k 3 Joan A4 3 100k CUSTOMER ACCOUNT name balance John 20k John 5k JOIN RESULT name balance Smith 35k name balance Joan 100k name SUM John 25k 24 CREATE Table CREATE TABLE table-name (attr1 attr-type PRIMARYKEY, attr2 attr-type,…,attrN attr-type); Adds a new table with the specified attributes (and types) to the database. For more examples and an interactive environment, go to: http://sqlcourse.com/create.html 25 Access Data Types Numeric (1, 2, 4, 8 bytes, fixed or float) Text (255 max) Memo (64000 max) Date/Time (8 bytes) Currency (8 bytes, 15 digits + 4 digits decimal) Autonumber (4 bytes) Yes/No (1 bit) OLE (limited only by disk space) Hyperlinks (up to 64000 chars) 26 Access Numeric types Byte – Stores numbers from 0 to 255 (no fractions). 1 byte Integer – Stores numbers from –32,768 to 32,767 (no fractions) 2 bytes Long Integer (Default) – Stores numbers from –2,147,483,648 to 2,147,483,647 (no fractions). 4 bytes Single – Stores numbers from -3.402823E38 to –1.401298E–45 for negative values and from 1.401298E–45 to 3.402823E38 for positive values. 4 bytes Double – Stores numbers from –1.79769313486231E308 to – 4.94065645841247E–324 for negative values and from 1.79769313486231E308 to 4.94065645841247E–324 for positive values. 15 8 bytes Replication ID – Globally unique identifier (GUID) N/A 16 bytes 27 Oracle Data Types CHAR (size) -- max 2000 VARCHAR2(size) -- up to 4000 DATE DECIMAL, FLOAT, INTEGER, INTEGER(s), SMALLINT, NUMBER, NUMBER(size,d) – All numbers internally in same format… LONG, LONG RAW, LONG VARCHAR – up to 2 Gb -- only one per table BLOB, CLOB, NCLOB -- up to 4 Gb BFILE -- file pointer to binary OS file 28 Creating a new table from existing tables Syntax: – SELECT [DISTINCT] attr1, attr2,…, attr3 INTO newtablename FROM rel1 r1, rel2 r2,… rel3 r3 WHERE condition1 {AND | OR} condition2 ORDER BY attr1 [DESC], attr3 [DESC] 29 ALTER Table ALTER TABLE table-name ADD COLUMN attr1 attr-type; … DROP COLUMN attr1; Adds a new column to an existing database table. 30 INSERT INSERT INTO table-name (attr1, attr4, attr5,…, attrK) VALUES (“val1”, val4, val5,…, “valK”); Adds a new row(s) to a table. INSERT INTO table-name (attr1, attr4, attr5,…, attrK) VALUES SELECT ... For more examples and an interactive environment, go to: http://sqlcourse.com/insert.html 31 DELETE DELETE FROM table-name WHERE <where clause>; Removes rows from a table. For more examples and an interactive environment, go to: http://sqlcourse.com/delete.html 32 UPDATE UPDATE tablename SET attr1=newval, attr2 = newval2 WHERE <where clause>; changes values in existing rows in a table (those that match the WHERE clause). For more examples and an interactive environment, go to: http://sqlcourse.com/update.html 33 DROP Table DROP TABLE tablename; Removes a table from the database. For more examples and an interactive environment, go to: http://sqlcourse.com/drop.html 34 J# Source Code Connecting to an Oracle DB package juliadb1; import System.Drawing.*; import System.Collections.*; import System.ComponentModel.*; import System.Windows.Forms.*; import System.Data.*; // ODP.NET Oracle managed provider /** * Summary description for Form1. */ public class Form1 extends System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; /** * Required designer variable. */ private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /** * Clean up any resources being used. */ protected void Dispose(boolean disposing) { if (disposing) { if (components != null) { components.Dispose(); } } super.Dispose(disposing); } #region Windows Form Designer generated code /** * Required method for Designer support - do not modify * the contents of this method with the code editor. */ private void InitializeComponent() { 35 Screenshot of J# Program Output 36 Questions? For a neat SQL tutorial and an interactive environment, go to: http://sqlcourse2.com/intro2.html 37