Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.

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

MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
Module 12: Auditing SQL Server Environments
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Module 2 Designing a Logical Database Model. Module Overview Guidelines for Building a Logical Database Model Planning for OLTP Activity Evaluating Logical.
Module 6 Implementing Table Structures in SQL Server ®2008 R2.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Concepts of Database Management Sixth Edition
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Module 17 Storing XML Data in SQL Server® 2008 R2.
Module 18 Monitoring SQL Server 2008 R2. Module Overview Monitoring Activity Capturing and Managing Performance Data Analyzing Collected Performance Data.
Database Lecture # 1 By Ubaid Ullah.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Database Technical Session By: Prof. Adarsh Patel.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Introduction to SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 UNIT 6: Chapter 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Module 9 Designing and Implementing Stored Procedures.
Module 9 Authenticating and Authorizing Users. Module Overview Authenticating Connections to SQL Server Authorizing Logins to Access Databases Authorization.
SQL Server 7.0 Maintaining Referential Integrity.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
HSCI 709 SQL Data Definition Language. SQL Standard SQL-92 was developed by the INCITS Technical Committee H2 on Databases. SQL-92 was designed to be.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Module 14 Configuring Security for SQL Server Agent.
DAY 12: DATABASE CONCEPT Tazin Afrin September 26,
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
Module 10 Assigning Server and Database Roles. Module Overview Working with Server Roles Working with Fixed Database Roles Creating User-defined Database.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
Module 3: Creating Data Types and Tables. Overview Working with Data Types Working with Tables Generating Column Values Generating Scripts.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Module 4: Implementing Data Integrity
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Visual Programing SQL Overview Section 1.
Module 11 Authorizing Users to Access Resources. Module Overview Authorizing User Access to Objects Authorizing Users to Execute Code Configuring Permissions.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Basics Review Reviewing what we’ve learned so far…….
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
In this session, you will learn to: Manage databases Manage tables Objectives.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Chapter Seven: SQL for Database Construction and Application Processing.
Getting started with Accurately Storing Data
Controlling User Access
TABLES AND INDEXES Ashima Wadhwa.
Controlling User Access
Module 5: Implementing Data Integrity by Using Constraints
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Normalization Referential Integrity
Relational Database Design
Responding to Data Manipulation Via Triggers
Presentation transcript:

Module 3 Designing and Implementing Tables

Module Overview Designing Tables Working with Schemas Creating and Altering Tables

Lesson 1: Designing Tables What is a Table? Normalizing Data Common Normalization Forms Demonstration 1A: Normalization Primary Keys Foreign Keys Working with System Tables

What is a Table? Relational databases store data in tables (relation)  Defined by a collection of columns (identified by name)  Contain zero or more rows Tables typically represent a type of object or entity  Employees, PurchaseOrders, Customers, SalesOrders  Consistent naming convention for tables is important  Tables are a security boundary Each row usually represents a single object or entity  One customer, one purchase order, one employee  Rows of Tables have no order

Normalizing Data Normalization is a process  Ensures that database structures are appropriate  Ensures that poor design characteristics are avoided Edgar F. Codd invented the relational model  Introduced the concept of normalization  Referred the degrees of normalization as "forms" Database designs should start normalized  Denormalization might be applied later to improve performance or to make analysis of data easier

Common Normalization Forms 1 st Form  Eliminate repeating groups in individual tables.  Create a separate table for each set of related data.  Identify each set of related data with a primary key. 2 nd Form  Create separate tables for sets of values that apply to multiple records.  Relate these tables with a foreign key. 3 rd Form  Eliminate fields that do not depend on the key.

Demonstration 1A: Normalization In this demonstration, you will see the common normalization errors.

Primary Keys Candidate key can be used to uniquely identify a row  Must be unique and cannot be unknown  Can involve multiple columns  Should not change  Primary key is one candidate key  Most tables will only have a single candidate key Substantial ongoing debate surrounding natural vs. surrogate keys  Natural key – formed from data related to the entity  Surrogate key – usually codes or numbers

Foreign Keys Foreign keys are references between tables  Foreign key in one table holds a candidate key for another table  Usually the primary key for consistency  Self-references are permitted such as an employee table that references a manager who is also an employee Rows cannot be inserted in a referencing table that do not exist in the referenced table  CustomerOrders cannot be entered for non-existent customers  A referenced key cannot be deleted or updated Multiple foreign keys might exist in a table  These can even reference the same table

Working with System Tables SQL Server provides a set of system tables  Should not be directly modified In SQL Server 2005, most were replaced by a set of permission-based system views Some system tables in the msdb database are still useful  dbo.backupset  dbo.restorehistory  dbo.sysjobhistory

Lesson 2: Working with Schemas What is a Schema? Object Name Resolution Creating Schemas Demonstration 2A: Schemas

What is a Schema? Schemas are containers for objects  Tables  Stored Procedures  Functions  Types  Views Schemas are security boundaries  Permissions can be granted at the schema level to apply to all objects within a schema  Simplifies security configuration

Object Name Resolution If the schema name is omitted, rules apply to how the name will be resolved  Each user has a default schema (does not apply to Windows groups)  Users with no defined default schema will have dbo as their default schema  First search is in the user's default schema  If not found, the dbo schema is searched also Whenever referencing an object in a statement, users should specify both the schema and the object name  SELECT ProductID FROM Production.Product

Creating Schemas Schemas are created using the CREATE SCHEMA command Schemas have owners  Objects contained within schemas also have owners Schema objects and permissions can be created in the same statement as the creation of the schema CREATE SCHEMA Reporting AUTHORIZATION Terry; CREATE SCHEMA KnowledgeBase AUTHORIZATION Paul CREATE TABLE Article (ArticleID int IDENTITY(1,1) PRIMARY KEY, ArticleContents xml) GRANT SELECT TO Salespeople; CREATE SCHEMA Reporting AUTHORIZATION Terry; CREATE SCHEMA KnowledgeBase AUTHORIZATION Paul CREATE TABLE Article (ArticleID int IDENTITY(1,1) PRIMARY KEY, ArticleContents xml) GRANT SELECT TO Salespeople;

Demonstration 2A: Schemas In this demonstration you will see how to:  Create a schema  Create a schema with an included object  Drop a schema

Lesson 3: Creating and Altering Tables Creating Tables Dropping Tables Altering Tables Demonstration 3A: Working with Tables Temporary Tables Demonstration 3B: Temporary Tables Computed Columns Demonstration 3C: Computed Columns

Creating Tables Tables are created with the CREATE TABLE statement Columns need to be specified Nullability should be specified Primary Key should be identified CREATE TABLE PetStore.Owner ( OwnerID int IDENTITY(1,1) PRIMARY KEY, OwnerName nvarchar(30) NOT NULL, HairColor nvarchar(10) NULL ); CREATE TABLE PetStore.Owner ( OwnerID int IDENTITY(1,1) PRIMARY KEY, OwnerName nvarchar(30) NOT NULL, HairColor nvarchar(10) NULL );

Dropping Tables Tables are removed by the DROP TABLE statement Referenced tables (via foreign keys) cannot be dropped All permissions, constraints, indexes, and triggers are also dropped Code that references the table is not dropped  Stored procedures  Functions DROP TABLE PetStore.Owner; GO DROP TABLE PetStore.Owner; GO

Altering Tables Tables are modified using the ALTER TABLE statement ALTER TABLE retains permissions on the table ALTER TABLE retains the data in the table Add/Drop columns and constraints Enable/Disable constraints and triggers ALTER TABLE PetStore.Owner ADD PreferredName nvarchar(30) NULL; GO ALTER TABLE PetStore.Owner DROP COLUMN PreferredName; GO ALTER TABLE PetStore.Owner ADD PreferredName nvarchar(30) NULL; GO ALTER TABLE PetStore.Owner DROP COLUMN PreferredName; GO

Demonstration 3A: Working with Tables In this demonstration, you will see how to:  Create tables  Alter tables  Drop tables

Temporary Tables Session temporary tables are only visible to their creators in the same session and same scope or sub-scope  Created with a # prefix  Dropped when the user disconnects or when out of scope  Should be deleted in code rather than depending on automatic drop  Are often created using SELECT INTO statements Global temporary tables are visible to all users  Created with a ## prefix  Deleted when all users referencing the table disconnect CREATE TABLE #Squares ( Number int PRIMARY KEY, NumberSquared int ); GO CREATE TABLE #Squares ( Number int PRIMARY KEY, NumberSquared int ); GO

Demonstration 3B: Temporary Tables In this demonstration, you will see how to work with temporary tables

Computed Columns Computed columns are derived from other columns or from functions Computed columns are often used to provide easier access to data without denormalizing it Persisted computed columns improve SELECT performance of computed columns in some situations CREATE TABLE PetStore.Pet (PetID int IDENTITY(1,1) PRIMARY KEY, PetName nvarchar(30) NOT NULL, DateOfBirth date NOT NULL, YearOfBirth AS DATEPART(year,DateOfBirth) PERSISTED ); GO CREATE TABLE PetStore.Pet (PetID int IDENTITY(1,1) PRIMARY KEY, PetName nvarchar(30) NOT NULL, DateOfBirth date NOT NULL, YearOfBirth AS DATEPART(year,DateOfBirth) PERSISTED ); GO

Demonstration 3C: Computed Columns In this demonstration, you will see how to work with computed columns

Lab 3: Designing and Implementing Tables Exercise 1: Improve the Design of Tables Exercise 2: Create a Schema Challenge Exercise 3: Create the Tables (Only if time permits) Logon information Estimated time: 45 minutes

Lab Scenario A business analyst from your organization has provided you with a first-pass at a schema design for some new tables being added to the MarketDev database. You need to provide an improved schema design based on good design practices and an appropriate level of normalization. The business analyst was also confused about when data should be nullable. You need to decide about nullability for each column in your improved design. The new tables need to be isolated in their own schema. You need to create the required schema DirectMarketing. The owner of the schema should be dbo. When the schema has been created, if you have available time, you need to create the tables that have been designed.

Lab Review When should a table be declared as nullable? Could columns such as AddressLine1, AddressLine2, AddressLine3 be reasonable in a normalized design? How would this differ from fields called PhoneNumber1, PhoneNumber2, PhoneNumber3?

Module Review and Takeaways Review Questions Best Practices