Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Data Model Implementation Dan Ames Hydroinformatics Fall 2013 This work was funded by National Science Foundation Grants EPS 1135482 and EPS.

Similar presentations


Presentation on theme: "Lecture 7 Data Model Implementation Dan Ames Hydroinformatics Fall 2013 This work was funded by National Science Foundation Grants EPS 1135482 and EPS."— Presentation transcript:

1 Lecture 7 Data Model Implementation Dan Ames Hydroinformatics Fall 2013 This work was funded by National Science Foundation Grants EPS 1135482 and EPS 1208732

2

3 Objectives Understand common database software packages, limitations and capabilities Where are certain databases commonly used? Why and when would you use certain databases? Introduce MS Access Introduce MS SQL Server Management Studio

4 Who are the main contenders? Commercial software – Sybase Adaptive Server Enterprise – IBM DB2 – Oracle – Microsoft SQL Server – Teradata Free/GPL/Open Source: – MySQL – PostgreSQL

5 Things to consider when deciding on a DBMS Price Operating System Support Most important features – ACID (Atomicity, Consistency, Isolation, Durability) – Referential Integrity support – Transactions support Scalability and security Availability of support and software Potential longevity

6 DBMS’s sorted by release date

7

8 Operating System Support WindowsMac OS XLinuxBSDUNIX Adaptive Server EnterpriseYes DB2YesNoYesNoYes Microsoft SQL ServerYesNo MySQLYes OracleYes NoYes PostgreSQLYes TeradataYesNoYesNoYes

9 Let’s Explore more… Go to the wikipedia article “Comparison of relational database management systems” Considering the database you designed in the last homework exercise answer the question… Which two or three DBMSs best meet the needs of my data and potential data management workflow? Why?

10 SQL Server (Express) Express version free and redistributable, license to commercial version varies between $600 to $10000 Support for Windows only Express version limited to one CPU and single user thread – Limited features, but scales easily to Enterprise version – Comes with all standard SQL tools – Email support

11 Oracle Standard Edition Free download for non-profit, non-commercial use Approximately $15000/cpu for commercial use Support for most operating systems Very strong support and service Weak user interface, complex installation and maintenance process

12 MySQL No limit on # of CPUs, fully featured, support for several OSs $0 without Support, approx. $600-$6000 for subscription-based support Highly scalable, provides support for most types of indexes and storage methods

13 IBM DB2 Express Free for non-commercial use, $6000 per CPU unlimited users Express version limited to 2 CPUs. Support for Windows, Linux and Unix Strong features set, very good user interface Limited support but can be purchased for additional cost

14 How would it work in Access?

15 Basic Database Design Principles Avoid Duplication – Tables, fields, records Enforce Data Integrity – Data are referenced throughout the entire database so that changes made in one table affect other tables Ensure Data Accuracy – Formats (mm-dd-yyyy), pick lists, controlled vocabulary

16 Main Parts of Database Design Tables Fields Identifying Relationships Enforcing Integrity

17 Table: inven The ItemNo is the primary key for this table.

18 Table: invcust The Custid is the primary key for this table. Notice Indexed is Yes with no duplicates.

19 There is a one to many relationship between the order header table (orderz) and the order line item table (ordline). This means for every order there can be multiple line items, but for each line item there is only one order. I can find out the line items by going into the ordline table and retrieving all records that match the order number for a given order. Because there are multiple line items with the same order number, I have to use the combination or order number and item number to make a unique primary key. Tables: orderz & ordline Ordno is the primary key. Ordno and Itemno are the primary keys.

20 The primary key is Slsrep. Tables: Salesrep & Department The primary key is Dept.

21 Relationships

22 Query: Inven & Department SELECT Inven.ItemNo, Inven.ItemName, Inven.Dept, Department.Deptname, Department.Manager FROM Department INNER JOIN Inven ON Department.Dept = Inven.Dept; SELECT ItemNo, ItemName, Inven.Dept, Deptname, Manager FROM Inven, Department WHERE Department.Dept = Inven.Dept; Access SQL code. My SQL code. From inven. From department.

23 Queries: Orderz & Invcust SELECT Orderz.Ordno, Orderz.Custid, Invcust.Custname, Orderz.Ordate FROM Invcust INNER JOIN Orderz ON Invcust.Custid = Orderz.Custid; SELECT Ordno, Orderz.Custid, Custname, Ordate FROM Orderz, InvCust WHERE Invcust.Custid = Orderz.Custid; Access SQL code My SQL code Custname from Invcust table - the rest from Orderz table.

24 SELECT Orderz.Ordno, Orderz.Custid, Orderz.Ordate, Ordline.Itemno, Ordline.Numord FROM Orderz INNER JOIN Ordline ON Orderz.Ordno = Ordline.Ordno; SELECT Orderz.Ordno, Custid, Ordate, Itemno, Numord FROM Orderz, Ordline WHERE Orderz.Ordno = Ordline.Ordno; Query: Orderz & Ordline Access SQL code My SQL code From Orderz From ordline

25 SELECT Orderz.Ordno, Ordline.Itemno, Inven.ItemName, Ordline.Numord FROM Inven INNER JOIN (Orderz INNER JOIN Ordline ON Orderz.Ordno = Ordline.Ordno) ON Inven.ItemNo = Ordline.Itemno; Query: Orderz, Ordline, Inven SELECT Orderz.Ordno, Ordline.Itemno, Inven.ItemName, Ordline.Numord FROM Orderz, Ordline, Inven WHERE Orderz.Ordno = Ordline.Ordno AND Inven.ItemNo = Ordline.Itemno; Access SQL My SQL

26 Query on 4 tables SELECT Orderz.Ordno, Orderz.Custid, Invcust.Custname, Orderz.Ordate, Ordline.Itemno, Inven.ItemName, Ordline.Numord FROM (Invcust INNER JOIN Orderz ON Invcust.Custid = Orderz.Custid) INNER JOIN (Inven INNER JOIN Ordline ON Inven.ItemNo = Ordline.Itemno) ON Orderz.Ordno = Ordline.Ordno; SELECT Orderz.Ordno, Orderz.Custid, Custname, Ordate, Ordline.Itemno, ItemName, Numord FROM Invcust, Orderz, Ordline, Inven WHERE Invcust.Custid = Orderz.Custid AND Inven.ItemNo = Ordline.Itemno AND Orderz.Ordno = Ordline.Ordno; Access SQL My SQL

27 The Access Window New Perspectives on Microsoft Access 2010 27

28 Databases and Relationships New Perspectives on Microsoft Access 2010 28

29 Entering Records New Perspectives on Microsoft Access 2010 29

30 How would it work in SQL Server Management Studio?

31 MS SQL Server 2008MS SQL Server 2008 Overview SQL Server Slides adapted from www.telerik.com

32 What is Microsoft SQL Server?What is Microsoft SQL Server? MS SQL Server is a Relational Database Management System (RDBMS) from Microsoft MS SQL Server is a Relational Database Management System (RDBMS) from Microsoft – The main language supported in SQL Server is Transact SQL (T-SQL), an extension of SQL – Powerful, trustworthy, easy-to-use DB server The most recent version is SQL Server 2008 The most recent version is SQL Server 2008 Works only on Windows systems Works only on Windows systems A free distribution exists (SQL Server Express) A free distribution exists (SQL Server Express) – http://www.microsoft.com/express/database/ http://www.microsoft.com/express/database/ 32

33 Connecting to SQL ServerConnecting to SQL Server Connecting to SQL Server requires Connecting to SQL Server requires – The name of the server machine / IP address – The name of the server instance – The name of the database – Username / password (if using SQL Server authentication) Types of authentication in SQL Server Types of authentication in SQL Server – Windows (by using a Windows user credentials) – Mixed (both Windows and SQL Server) 33

34 SQL Server Management Studio (SSMS)SQL Server Management Studio (SSMS) SQL Server Management Studio (SSMS) is a powerful graphical DB management tool SQL Server Management Studio (SSMS) is a powerful graphical DB management tool – Administrate databases (create, modify, backup / restore DB) – Create and modify E/R diagrams – View / modify table data and other DB objects – Execute SQL queries – Free and easy to use tool – Works with all SQL Server versions 34

35 SQL Server Management Studio – Screenshot 35

36 SQL Server DatabasesSQL Server Databases 36 System Databases User Databases model tempdb msdb distribution pubsNorthwind… master

37 SQL Server DatabasesSQL Server Databases Each SQL Server database consists of two files: Each SQL Server database consists of two files: file –.mdf file Contains the core data in the database Contains the core data in the database Schema, tables data, and other database objects Schema, tables data, and other database objects file –.ldf file Transaction log – keeps track of transactions Transaction log – keeps track of transactions You need both these files to use the database You need both these files to use the database You can move a database by SQL scripting, backup / restore, or copy the.mdf /.ldf files You can move a database by SQL scripting, backup / restore, or copy the.mdf /.ldf files 37

38 Data Types in SQL Server Numeric – bit (1-bit), integer (32-bit), bigint (64-bit) – float, real, numeric(scale, precision) – money – for money (precise) operations Strings – char(size) – fixed size string – varchar(size) – variable size string – nvarchar(size) – Unicode variable size string – text / ntext – text data block (unlimited size)

39 Data Types in SQL Server (2) Binary data – varbinary(size) – a sequence of bits – image – a binary block up to 1 GB Date and time – datetime – date and time starting from 1.1.1753 to 31.12.9999, a precision of 1/300 sec. – smalldatetime – date and time (1-minute precision)

40 Data Types in SQL Server (3) Other types – timestamp – automatically generated number whenever a change is made to the data row – uniqueidentifier – GUID identifier – xml – data in XML format

41 Data Types in SQL Server (4) Nullable and NOT NULL types – All types in SQL Server may or may not allow NULL values Primary key columns – Define the primary key Identity columns – Automatically increased values when a new row is inserted (auto-increment values) – Used in combination with primary key

42 Database Modeling with SQL Server Management Studio Creating a Database

43 Connecting to SQL Server When starting SSMS a window pops up Usually it is enough to just click the "Connect" button without changing anything

44 Working with Object Explorer Object Explorer is the main tool to use when working with the database and its objects Enables us: – To create a new database – To create objects in the database (tables, stored procedures, relationships and others) – To change the properties of objects – To enter records into the tables 44

45 Creating a New Database In Object Explorer we go to the "Databases" and choose "New Database…" from the context menu 45

46 Creating a New Database (2) In the "New Database" window enter the name of the new database and click [OK] 46

47 Database Modeling with SQL Server Management Studio Creating E/R Diagrams 47

48 Creating an E/R diagram In the "Database Diagrams" menu choose the "New Database Diagram" We can choose from the existing tables, which we want to add to the diagram 48

49 Database Modeling with SQL Server Management Studio Creating Tables

50 If the database doesn't show immediately in Object Explorer perform "Refresh" [F 5 ] Creating new table: 50

51 Creating Tables (2) Enter table name and define the table columns (name and type): 51 Enter the name of the column here Choose the data type of the column here Choose whether NULLs are allowed

52 Creating Tables (3) Defining a primary key 52 Right click on the column start and select "Set Primary Key"

53 Creating Tables (4) Defining an identity columns – Identity means that the values in a certain column are auto generated (for int columns) – These values cannot be assigned manually – Identity Seed – the starting number from which the values in the column begin to increase. – Identity Increment – by how much each consecutive value is increased 53

54 Creating Tables (5) Setting an identity through the "Column Properties" window 54

55 Creating Tables (6) It is a good practice to set the name of the table at the time it is created – Use the "Properties" window – If it's not visible use "View"  "Properties Window" or press [F4] 55 Tablen ame

56 Creating Tables (7) When closing the window for the table, SSMS asks whether to save the table – You can do it manually by choosing “Save Table” from the “File” menu or by pressing Ctrl + S 56

57 Database Modeling with SQL Server Management Studio Creating Relationships between Tables

58 Creating Relationships To create one-to-many relationship drag the foreign key column onto the other table – Drag from the child table to the parent table 58

59 Self-Relationships Self-relationship can be created by dragging a foreign key onto the same table 59

60 Database Modeling with SQL Server Management Studio Naming Conventions

61 Tables – Each word is capitalized (Pascal Case) – In English, plural – Examples: Users, PhotoAlbums, Countries Columns – In English, singular – Each word is capitalized (Pascal Case) – Avoid reserved words (e.g. key, int, date ) – Examples: FirstName, OrderDate, Price 61

62 Naming Conventions (2) Primary key – Use " Id " or name_of_the_table + " Id " – Example: in the Users table the PK column should be be called Id or UserId Foreign key – Use the name of the referenced table + " Id " – Example: in the Users table the foreign key column that references the Groups table should be named GroupId 62

63 Naming Conventions (3) Relationship names (constraints) – In English, Pascal Case – " FK_ " + table1 + " _ " + table2 – For example: FK_Users_Groups Index names – " IX_ " + table + column – For example: IX_Users_UserName 63

64 Naming Conventions (4) Unique key constraints names – " UK_ " + table + column – For instance: UK_Users_UserName Views names – V_ + name – Example: V_BGCompanies Stored procedures names – usp_ + name – Example: usp_InsertCustomer(@name) 64


Download ppt "Lecture 7 Data Model Implementation Dan Ames Hydroinformatics Fall 2013 This work was funded by National Science Foundation Grants EPS 1135482 and EPS."

Similar presentations


Ads by Google