In this session, you will learn to: Manage databases Manage tables Objectives.

Slides:



Advertisements
Similar presentations
Database Systems: Design, Implementation, and Management Tenth Edition
Advertisements

Accounting System Design
Working with SQL Server Database Objects
Introduction to Structured Query Language (SQL)
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Working with SQL and PL/SQL/ Session 1 / 1 of 27 SQL Server Architecture.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases.
Oracle Data Definition Language (DDL)
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Database Technical Session By: Prof. Adarsh Patel.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
SQL Server 7.0 Maintaining Referential Integrity.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
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.
Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.
Module 4: Implementing Data Integrity
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Session 11 Creating Tables and Using Data Types. RDBMS and Data Management/Session 11/2 of 40 Session Objectives Define the data types and list the categories.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
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
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
SQL Basics Review Reviewing what we’ve learned so far…….
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Advanced SQL - DDL Advanced Database Dr. AlaaEddin Almabhouh.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Getting started with Accurately Storing Data
Fundamental of Database Systems
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Module 5: Implementing Data Integrity by Using Constraints
Oracle Data Definition Language (DDL)
Database Management System
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

In this session, you will learn to: Manage databases Manage tables Objectives

SQL Server 2005 contains the following system databases: master: The master database that records all the server-specific configuration information. tempdb: The temporary database that holds all temporary tables and stored procedures. model: The model database that acts as a template or prototype for the new databases. msdb: The database that handles the task of scheduling, exception handling, etc. Resource: Read-only database that contains all the system objects included in the SQL Server Identifying the System Databases in SQL Server 2005

Just a minute What is the utility of the model database? Answer: The model database acts as a template or a prototype for the new databases.

User-defined database: Is stored as a set of files. These files include: Primary data file Secondary data file Transaction log file Syntax: CREATE DATABASE database_name [ ON [ PRIMARY ] [ ]] [ LOG ON { [,...n ] } ] ::= ( [ NAME = logical_file_name, ] FILENAME = 'os_file_name' [, SIZE = size ] [, MAXSIZE = { max_size | UNLIMITED } ] [, FILEGROWTH = growth_increment ] ) [,...n] Let’s see how… Creating a User-Defined Database

Just a minute Which statement is used to create a database? Answer: The CREATE DATABASE statement

Table: Is created to store data Is created by using the CREATE TABLE statement Syntax: CREATE TABLE [ database_name. [ schema_name ].] table_name ( { | } [ ] [,...n ] ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] [ { TEXTIMAGE_ON { filegroup | "default" } ] [ ; ] Creating a Table

Example: Create an EmployeeLeave table in the HumanResources schema with the following details: Creating a Table (Contd.) ColumnsData TypeChecks EmployeeIDintNOT NULL LeaveStartDatedateNOT NULL LeaveEndDatedateNOT NULL LeaveReasonvarchar(100)NOT NULL LeaveTypechar(2)NOT NULL Let’s see how…

Avoids data redundancy Ensures that the data in a database is accurate, consistent, and reliable Is broadly classified into the following categories: Entity integrity: Ensures that each row can be uniquely identified by an attribute called the primary key Domain integrity: Ensures that only a valid range of values is allowed to be stored in a column Referential integrity: Ensures that the values of the foreign key match the value of the corresponding primary key User-defined integrity: Refers to a set of rules specified by a user, which do not belong to the entity, domain, and referential integrity categories Implementing Data Integrity

You can maintain data integrity by: Applying constraints Applying rules Using user-defined types Implementing Data Integrity (Contd.)

Constraints can be applied: By using CREATE TABLE statement By using ALTER TABLE statement Syntax: CREATE TABLE table_name ( column_name CONSTRAINT constraint_name constraint_type [,CONSTRAINT constraint_name constraint_type] ) Implementing Data Integrity (Contd.)

Constraints are of the following types: Primary key constraint Unique constraint Foreign key constraint Check constraint Default constraint Implementing Data Integrity (Contd.)

Primary key constraint: Is defined on a column or a set of columns whose values uniquely identify all the rows in a table Does not allow NULL values in the column Ensures entity integrity Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name PRIMARY KEY [CLUSTERED|NONCLUSTERED] col_name [, col_name [, col_name [, …]]] ) Implementing Data Integrity (Contd.)

Unique constraint: Is defined to enforce uniqueness in non-primary key columns Allows one NULL value in the column Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name UNIQUE [CLUSTERED | NONCLUSTERED] (col_name [, col_name [, col_name [, …]]]) col_name [, col_name [, col_name [, …]]] ) Implementing Data Integrity (Contd.)

Foreign key constraint: Is defined to remove the inconsistency in two tables when the data in one table depends on the data in another table Associates one or more columns (the foreign key) of a table with an identical set of columns (a primary key column) in another table Implementing Data Integrity (Contd.)

Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name FOREIGN KEY (col_name [, col_name [, …]]) REFERENCES table_name (column_name [, column_name [, …]])] (col_name [, col_name [, col_name [, …]]]) col_name [, col_name [, col_name [, …]]] ) Let’s see how… Implementing Data Integrity (Contd.)

Check constraint: Is defined to enforce domain integrity by restricting the values to be inserted in a column Can be defined on multiple columns Is specified by using the following keywords: IN LIKE BETWEEN Implementing Data Integrity (Contd.)

Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name] CHECK (expression) (col_name [, col_name [, …]]). ) Let’s see how… Implementing Data Integrity (Contd.)

Default constraint: Is defined to assign a constant value to a column Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name] DEFAULT (constant_expression | NULL) (col_name [, col_name [, …]]). ) Let’s see how… Implementing Data Integrity (Contd.)

Just a minute Which keyword is used to specify a check constraint? Answer: A check constraint can be specified by using the LIKE, IN, and BETWEEN keywords.

Rules: Help in enforcing domain integrity for columns or user-defined data types Can be specified to the column or the user-defined data type before an INSERT or UPDATE statement is issued Are used to implement business-related restrictions or limitations Can be created by using the CREATE RULE statement Syntax: CREATE RULE rule_name AS conditional_expression Can be bound by using the sp_bindrule stored procedure Let’s see how… Implementing Data Integrity (Contd.)

User-defined data types: Are custom data types defined by the users with a custom name Can be created by using the CREATE TYPE statement Syntax: CREATE TYPE [ schema_name. ] type_name { FROM base_type [ ( precision [, scale ] ) ] [ NULL | NOT NULL ] } [ ; ] Let’s see how… Implementing Data Integrity (Contd.)

Just a minute You want to create a rule, rule1, which allows the user to enter any of the four values: Tea, Coffee, Soup, or Miranda in a column. Which command should you execute? Answer: CREATE RULE rule1 IN ('Tea', 'Coffee', 'Soup', 'Miranda')

Flash presentation: Partitioning TablesPartitioning Table Partitioned table: Is created when the table contains voluminous data Is created to separate data into multiple physical locations based on a range of values for a specific column Helps in improving query performance Can be created by: Creating a partition function Creating a partition scheme Creating a table by using the partition scheme Creating a Partitioned Table

Modify a table: When there is a requirement to add or remove columns and constraints Using the ALTER TABLE statement Syntax: ALTER TABLE [ database_name. [ schema_name ]. | schema_name. ] table_name { ALTER COLUMN column_name { [ NULL | NOT NULL ] } | [ WITH { CHECK | NOCHECK } ] ADD COLUMN { ADD CONSTRAINT constraint_name constraint_type Let’s see how… Modifying a Table

Just a minute You are managing a large table. You want to improve the performance of the table and want the table to be more manageable. Which strategy can you use? Answer: Partition the table

Drop a table: When a table is not required Using the DROP TABLE statement Syntax: DROP TABLE [ database_name. [ schema_name ]. ] table_name Let’s see how… Dropping a Table

Problem Statement: The management of AdventureWorks, Inc. has decided to provide travel and medical reimbursements to the employees. They want to store the details of these reimbursements in the database. For this, you need to create a database table, EmployeeReimbursements. The details of the tables are shown in the following table. Demo: Managing Tables ColumnsData Type and SizeConstraints RimIDintPrimary key EmployeeIDintForeign Key references the EmployeeID of the Employee Table, NOT NULL AmountmoneyAmount>0 RimTypevarchar(20)RimType should be Medical, Cash, or Local Pending_withvarchar(30)NOT NULL How will you create the table?

Solution: To solve the preceding problem, you need to perform the following tasks: 1. Write the query to create a table. 2. Execute the statement to verify the result. Demo: Managing Tables (Contd.)

In this session, you learned that: A database is a repository of information that contains data in an organized way. The master database records all the server-specific configuration information, including authorized users, databases, system configuration settings, and remote servers. The tempdb database is a temporary database that holds all temporary tables and stored procedures. The model database acts as a template or a prototype for the new databases. The msdb database supports the SQL Server Agent. The SQL Server Agent includes features that schedule periodic activities of the SQL Server. The Resource database is a read-only database that contains all the system objects that are included with SQL Server Summary

The user-defined databases are created by the users to store data for client-server applications. A database consists of the following types of files: Primary data file Secondary data file Transaction log file A database must consist of a primary data file and one transaction log file. The CREATE DATABASE statement is used to create a database, which also includes determining the name of the database, the size of the database, and the files used to store data in the database. Tables are used to store data. Summary (Contd.)

Tables are used to store data. The CREATE TABLE statement is used to create a table. Data integrity is enforced to keep the data in a database accurate, consistent, and reliable. It is broadly classified into the following categories: Entity integrity: Ensures that each row can be uniquely identified by an attribute called the primary key. Domain integrity: Ensures that only a valid range of values is allowed to be stored in a column. Referential integrity: Ensures that the values of the foreign key match the value of the corresponding primary key. User-defined integrity: Refers to a set of rules specified by a user, which do not belong to the entity, domain, and referential integrity categories. Summary (Contd.)

Constraints define rules that must be followed to maintain consistency and correctness of data. A primary key constraint is defined on a column or a set of columns whose values uniquely identify rows in a table. The unique constraint is used to enforce uniqueness on non- primary key columns. A foreign key constraint associates one or more columns of a table (the foreign key) with an identical set of columns on which a primary key constraint has been defined (a primary key column in another table). A check constraint enforces domain integrity by restricting the values to be inserted in a column. The IN, LIKE, and BETWEEN keywords are used to define the check constraint. Summary (Contd.)

A default constraint can be used to assign a constant value to a column, and the user need not insert values for such a column. A rule provides a mechanism for enforcing domain integrity for columns or user-defined data types. User-defined data types are custom data types defined by the users with a custom name. A partitioned table is created to manage the data and improve the query performance. The ALTER TABLE statement is used to modify a table. The DROP TABLE statement is used to delete a table. Summary (Contd.)