* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download 11. Relational Databases and SQL
Relational algebra wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Clusterpoint wikipedia , lookup
Microsoft Access wikipedia , lookup
Team Foundation Server wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Lecture 10: Relational Databases & Structured Query Language (SQL) Objectives “The large majority of today's business applications revolve around relational databases and the SQL programming language (Structured Query Language). Few businesses could function without these technologies…” • Relational databases • Database management systems • SQL Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-2 Part 1 • Relational databases… Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-3 Relational databases • Most of today's databases are relational: – database contains 1 or more tables – table contains 1 or more records – record contains 1 or more fields – fields contain the data DB • So why is it called "relational"? – tables are related (joined) based on common fields Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-4 Example • Here's a simple database schema for tracking sales – 3 tables, related by primary keys (CID, OID, PID) – primary keys (in boldface) are unique record identifiers – customer may place order for one product at a time… Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-5 Customers… • Here's some data for the Customers table – ignore last row, it's a MS Access mechanism for adding rows… Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-6 Products… • Here's some data for the Products table – yes, we're selling animals :-) Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-7 Orders… • Here's some data for the Orders table – how do you read this? – e.g. order #9906 states Kathie O'Dahl purchased 600 Ants – must join tables together to figure that out... Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-8 Part 2 • Database management systems… Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-9 Database management systems • A DBMS consists of 2 main pieces: – the data – the DB engine request DB Engine Data – the data is typically stored in one or more files Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-10 Common types of DBMS • Two most common types of DBMS are: – Local – Server Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-11 Local DBMS • A local DBMS is where DB engine runs as part of application DB Engine Data application • Example? – MS Access – underlying DB engine is JET ("Joint Engine Technology") Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-12 Server DBMS • A server DBMS is where DB engine runs as a separate process – typically on a different machine (i.e. server) network? DB Engine Data server application – Examples? • MS SQL Server, Oracle, DB2, MySQL Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-13 Databases & Visual Studio .NET • Most databases ship with tools for opening / manipulating DB – MS Access database: use the MS Access Office product – SQL Server database: use Query Analyzer • But you can also use Visual Studio .NET! – – – – View menu, Server Explorer right-click on Data Connections Add Connection... MS Access database: • Provider: JET 4.0 OLE DB • Connection: browse… • Connection: test... – Drill-down to Tables – Double-click on a table – "Show SQL Pane" via toolbar Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-14 Part 3 • Structured Query Language… Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-15 Structured Query Language • SQL is the standard programming language of relational DBs – SQL is written in the form of queries – action queries insert, update & delete data – select queries retrieve data from DB • SQL is a great example of a declarative programming language – your declare what you want, DB engine figures out how… Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-16 Select queries • Select queries retrieve data w/o changing DB – you specify what data – you specify which table(s) to pull from – you specify under what conditions to retrieve data (if any) – you specify how to present data (if any) • Key concept: – what you get back is another (temporary) table! Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-17 Example • Select all customers, in no particular order: Select * From Customers; Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-18 Example #2 • Select all customers with a balance, in sorted order: Select * From Customers Where Balance > 0.0 Order By LastName Asc, FirstName Asc; Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-19 Computed fields • SQL is a programming language, so it can do computation • Example: – compute # of customers, average balance, and max balance Select count(*) as COUNT, avg(Balance) as AVG, max(Balance) as MAX From Customers; Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-20 Joins • You have to "join" tables when you want to combine data • Example: – what's the name of the customer that placed order #12351? – we need to join Customers table with Orders table… Select FirstName, LastName From Customers Inner Join Orders On Customers.CID = Orders.CID Where Orders.OID = 12351; Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-21 Nested select queries • • Often you need to nest queries Example: – what products have been ordered by "Jim Bag"? – requires 2 steps: 1. TEMP = join Orders with Customers to get Jim's orders 2. then join Products with TEMP to get product names Select Distinct Name From Products Inner Join (Select PID From Orders Inner Join Customers On Orders.CID = Customers.CID Where FirstName = 'Jim' and LastName = 'Bag') as TEMP On Products.PID = TEMP.PID Order By Name Asc; Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-22 Action queries • Action queries update a database • Main actions are insert, update, and delete – as with Select queries, date / string values require delimiters – bad things happen if you forget them… Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-23 Examples • Here's an example of each type of action query… – once again, note the delimiters around text & date values Insert Into Orders(OID, CID, PID, Quantity, DateOfPurchase) Values(33411, 14, 1, 1, '01-Jun-2004'); Update Customers Set CreditLimit = 40000000000.0, Balance = 0.0 Where LastName = 'Gates' and FirstName = 'Bill'; Delete From Customers Where CID = 666; Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-24 Summary • Databases are a critical component of most business apps • SQL is the standard programming language for databases • Databases are a huge subject area in Computer Science – as well as in business applications Introducing Microsoft J# in Visual Studio CS using .NET .NET 10-25