Databases and SQL Designing a database with tables and primary keys

Slides:



Advertisements
Similar presentations
Designing Tables in Microsoft Access By Ed Lance.
Advertisements

CC SQL Utilities.
MS-Access XP Lesson 1. Introduction to MS-Access Database Management System Software (DBMS) Store data in databases Database is a collection of table.
Access Lesson 2 Creating a Database
Access - Project 1 l What Is a Database? –A Collection of Data –Organized in a manner to allow: »Access »Retrieval »Use of That Data.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Creating And Maintaining A Database. 2 Learn the guidelines for designing databases When designing a database, first try to think of all the fields of.
Concepts of Database Management Sixth Edition
Microsoft Office Word 2013 Expert Microsoft Office Word 2013 Expert Courseware # 3251 Lesson 4: Working with Forms.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
ASP.NET Programming with C# and SQL Server First Edition
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 4 1 Microsoft Office FrontPage 2003 Tutorial 4 – Using Shared Borders and Themes.
Creating a Web Site to Gather Data and Conduct Research.
 A database is a collection of data that is organized so that its contents can easily be accessed, managed, and updated. What is Database?
XP New Perspectives on Microsoft Access 2002 Tutorial 21 Microsoft Access Tutorial 2 – Creating And Maintaining A Database.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
Database Systems Microsoft Access Practical #1 Creating Tables Nos 215.
WHAT IS A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
® Microsoft Office 2013 Access Creating a Database.
CSC 240 (Blum)1 Introduction to Access CSC 240 (Blum)2 Click on the Access desktop icon or go to Start/Programs/Microsoft Office/Microsoft Office.
CSC 240 (Blum)1 Introduction to Access CSC 240 (Blum)2 Click on the Access desktop icon or go to Start/All Programs/Microsoft Office/Microsoft Office.
Lesson 4.  After a table has been created, you may need to modify it. You can make many changes to a table—or other database object—using its property.
INTRODUCTION TO ACCESS. OBJECTIVES  Define the terms field, record, table, relational database, primary key, and foreign key  Create a blank database.
Ennis-Cole, AC 2.01, CECS Maintaining A Database By: Dr. Ennis-Cole.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 1 ® Database & Table.
SQL server Section 2&3. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.
Microsoft Access Prepared by the Academic Faculty Members of IT.
Access Module Implementing a Database with Microsoft Access A Great Module on Your CD.
Getting Started in Access 1 Using an existing Database Database Window Table Design View Parts of a Table Views in Access Navigating in Access Entering.
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
COMPUTER SKILLS MS-ACCESS. Introduction Access is a piece of software known as a database management system. At its most basic level, it can be used to.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
N5 Databases Notes Information Systems Design & Development: Structures and links.
3 A Guide to MySQL.
Access Tutorial 2 Building a Database and Defining Table Relationships
Tutorial 1 Creating a Database
Guidelines for Designing Databases
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Access Tutorial 1 Creating a Database
Practical Office 2007 Chapter 10
Forms and Reports 09.
Basic Database Concepts
About SharePoint Server 2007 My Sites
Introduction to Microsoft Access
Data Definition and Data Types
Attributes and Domains
Unit 8.2 How data can be used Accessing Data in Views
Access Creating a Database
Access Creating a Database
Access Lesson 1 Understanding Access Fundamentals
Access Tutorial 1 Creating a Database
Introduction to Access 2003
MODULE 7 Microsoft Access 2010
Tutorial 3 – Querying a Database
Access Lesson 2 Creating a Database
Access Lesson 2 Creating a Database
CIS16 Application Programming with Visual Basic
Microsoft Office Access 2003
Computer Science Projects Database Theory / Prototypes
Attributes and Domains
Access Tutorial 1 Creating a Database
ICT Database Lesson 2 Designing a Database.
Guidelines for Microsoft® Office 2013
Access Tutorial 1 Creating a Database
Introduction to MS ACCESS
Unit J: Creating a Database
Presentation transcript:

Databases and SQL Designing a database with tables and primary keys Exploring database design Designing a database with tables and primary keys Creating your own SQL Server tables Linking tables

Introduction VWD allows you to have ACCESS (.mdb) or SQL-Server (.mdf) files. SQL-Server provides better scalability and supports a greater number of simultaneous users. ACCESS has a front-end. SQL-Server does not. It is just a server DATA= collection of information in a structured way. Field (entity) Record (collection of fields)Table (collection of records)Database (Collection of files)

View of Database Tables Database Explorer All tables automatically created start with aspnet_ To see what the tables contain : Right click Show table data Don’t add or delete data manually to any of the aspnet_ files ALWAYS use the WEB SITE ADMINISTRATION TOOL to manage Membership Data

ASPNET TABLES

SQL-SERVER SQL-Server is an RDBMS (Data + Relationships among them) One-to-many, many-to-many Example : One user can buy many products and one product can be bought by many users QUESTION : How can I connect the two tables (i.e. Users and Items)? ANSWER : By creating a new table (i.e. Transactions) In real life we don’t draw lines between tables . We actually don’t even look at the tables. INSTEAD we create queries to manage them, update them, view them, insert records into them, delete records from them etc.

TABLE KEYS Any time you extract data from the tables (i.e. Users, Items, Transactions) your query must contain fields of all 3 tables The primary keys and the foreign keys that link the tables must be connected by join-lines in a query

Rules to many connections between tables Every table has a primary key The transaction table must contain at least 2 fields whose names and data types must be exactly the same as the fields in the tables Users and Items This is because we have to realize the one-to-many association

SQL Server Tables (USERS) Right Click on the tables ASPNET_USERS Choose Open Table Definition

SQL – DATA TYPES BASIC DATA TYPES Unicode Text (16-bit / character) Number (Scalar) DateTime Boolean Binary (Pictures, sounds etc) Other (Specialized Data Types : UniqueIdentifier, xml, timestamp, sql-variant) Unicode Text (16-bit / character) Non-Unicode Text (8-bit/character)

TEXT char(n): Fixed-length non-Unicode text where n defines the actual length of each string. nchar(n): Fixed-length Unicode text, where n defines the actual length of each string. varchar(n): Variable-length non-Unicode text where n defines the maximum length of a string stored in the field, up to 8,000 characters. nvarchar(n): Variable-length Unicode text, where n defines the maximum length of each string, up to 4,000 characters.

Number data types Integers: These are whole numbers with no decimal point. Floating-point numbers: These types of scalar values can contain a decimal point as in 0.10 or 123.45 or 12,345,678.987654321. There are also currency data types specifically used for storing dollar amounts like $99.99.

INTEGERS tinyint: From 0 to 255 smallint: From –32,768 to 32,767 bigint: From –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

MONEY smallmoney: From about –$214,748.00 to about $214,748.00.

FLOATING POINT NUMBERS Decimal Precision (total # of numeric digits)- Maximum Precision is 38-digits Scale (total # of digits after the decimal point) Float Numeric Real

Summary of SQL-DATA TYPES

Summary of SQL-DATA TYPES

Summary of SQL-DATA TYPES

Summary of SQL-DATA TYPES

UniqueIdentifier Defines a field that can store a GUID (Globaly Unique ID) Example : 8a116cb2-4503-af83-870e-4f450bee923a Watch the ASPNET_USERS Table The ApplicationID is an ID that defines the whole WebSite The UserID is created automatically by the CreateUserControl To automatically assign new values Go to the property Default Value or Binding  it is set to newid()

CREATE YOUR OWN TABLES You don’t touch the aspnet_tables But you want to create your own tables Database ExplorerRight Click on the Tables FolderChoose Add New Table  Define the fields Give each field a name Choose the data type of the field Define the primary key SOS  You can let the SQL-Server to manage the primary key for you and keep it hidden from everyone

How to Set Primary Keys Right Click on the field name Choose  Set Primary Key or Click the field nameChoose Table Designer  Set Primary Key from the menu To have the records automatically numbered-- PROPERTIES Click the + sign to the Identity Specification Set the (Is Identity) to YES Set The Identity Increment to 1 Set the Identity Seed to (example 10000)

CREATING THE ITEMS TABLE ItemID (7 char fixed length) 3 char-3 digit ItemName (varchar) ItemDescription (varchar) ItemPrice (don’t include $ sign when insert records)

THE TRANSACTIONS TABLE Create the table SOS  Foreign Keys ( these are primary keys of other tables) Insert the UserID field  Don’t make it to get newid() Let the property empty Insert the ItemID  Don’t make it auto-numbered i.e. The Identity Specification property must be set to NO Insert the DateOfTransaction field Insert the SellingPrice field (to keep track of old prices) Insert the IsPaid field Insert the TransactionID (for the transaction code) Do it automatically numbered i.e. IsIdentity YES, Increment  (1), Seed  (20000) SOS the table does not need a PRIMARY KEY

POPULATING TABLES When you first create a table, it will be empty. So when you right-click the table name and choose Show Table Data, you’ll see an empty record with the word NULL in each field. For example, right-clicking an empty Items table and choosing Show Table Data displays that table’s contents. But because the table is brand new and empty, its contents consist of a single empty record that has the word NULL (which means, in effect, blank) in each field, as shown

POPULATING TABLES--Continue You don’t want to manually enter records to a table TIPS if you do insert them manually When you enter data into a record that contains an automaticallygenerated primary key (for example, the ItemId field in the Items table), you must leave that field empty (NULL). The field gets filled with a value automatically, after you’ve filled in other fields in the record. When you’re typing data into table fields, do not press Enter when you complete your entry. Instead, when you’ve finished filling one field and want to move on to the next, press the Tab key. Or just click the next field in which you want to enter or edit data. The little pencil that appears after you fill in a field isn’t part of the data in the field. It’s only there to tell you the field has been edited since the table was first opened. When typing dollar amounts, don’t type the dollar sign. There’s no need to bother typing commas, either; they’ll just be removed. For example, if you want to enter $1,234.56 into a money field, type 1234.56. In the Design surface, all dollar amounts have four digits to the right of the decimal point and no dollar sign. Don’t let that bother you. As long as you’re working in Database Explorer, how the data looks isn’t important.

TIPS CONTINUE If you don’t have data to fill a field yet, you can leave the field empty (NULL), provided you allowed nulls in the design of the table. If you didn’t, and you find that the table really needs to allow nulls in a field, go back to the table’s definition and select the Allow Nulls check box for that field. After you’ve filled in all the fields for a record, you can press Tab or Enter to complete that record. SQL Server validates the data and saves the record. If there are any problems, the record won’t be saved. Instead, you’ll see an error message (stating No row was updated) with a description of the problem. You have to click OK in that message box and fix the problem. This fix often involves clicking the faulty value and then pressing Escape to “undo” the entry currently in the column.

Linking Tables Although the tables in a database store data, the way you get exactly the data you need, and only the data you need, when you need it, is through Structured Query Language, abbreviated SQL Examples SELECT * FROM Items SELECT UserId, UserName FROM aspnet_Users SELECT * FROM Items WHERE ItemId = 10002 SELECT ItemName, ItemDescription FROM ItemsTable WHERE ItemId = 10002

Don’t Worry about SQL You don’t actually have to write the SQL statements yourself. And that’s a good thing — SQL statements can be far more complex than the examples shown here. To avoid all that tedious and error-prone typing, you can use the Query Builder to create your complex SQL statements. The Query Builder lets you pick and choose what you want to extract from your tables with ease — and with a more intuitive graphical user interface. Choose options, the Query Builder then writes the appropriate SQL statement for you. The Query Builder appears automatically whenever you perform some action that requires getting data from a database. For now, we use the Query Builder to link tables together into a view.

Creating a view In SQL Server, a view is a saved query. When you set up your site for Membership, VWD automatically created several views. They are listed under Views in Database Explorer. The name of each automatically-created view starts with vw_aspnet_ as shown in Figure

Creating a view - Continue

Creating a view - Continue Never delete, change, or rename any view whose name starts with vw_aspnet_. Those views are created by the membership system, for the membership system, and they really don’t like being monkeyed around with. To illustrate using the Query Builder to link users, transactions, and items, Let’s create a view named UsersAndItemsView. You create a view as you would any other object: Right-click the Views folder in Database Explorer and choose Add New View. A dialog box named Add Table opens. The first step in building the query is to choose the tables from which the query will get data. If there’s a many-to-many relationship among selected tables, the view must also include the Transactions table that provides the link between tables. To add a table to the query, click its name in the Add Table dialog box, and then click OK. (For this example, add the aspnet_Users, Transactions, and Items tables to the view.) Click the Close button in the Add Tables dialog box after choosing your tables.

Creating a view - Continue

Creating a view - Continue Choose which fields you want to see from these tables.You choose which fields you want the view to display by clicking the check box next to each desired field name. As you do so, each field name you check appears in the Criterion pane, just below the Diagram pane. Below the Criterion pane is the Show SQL pane, which shows the actual SQL statement that the Query Builder creates as you go.

Testing a Query While you’re designing a query, you can test it out at any time using any of these methods: Right-click some empty space in the Design surface and choose Execute SQL. Press Ctrl+R. Click the Execute Query button (red exclamation mark) in the toolbar. Choose Query Designer➪Execute Query from the menu bar. The results of executing the query appear in the Results pane at the bottom of everything else.