Lab 4: Tables and Constraints

Similar documents
Basis Data Terapan. Yoannita

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 1. Introduction to SQL Server Panayiotis Andreou

Code Centric: T-SQL Programming with Stored Procedures and Triggers

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Introduction to SQL Server 2005/2008 and Transact SQL

System Pages (Setup Admins Only)

Angela Henry. Data Types Do Matter

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

SAS OVERVIEW WHAT S NEW IN 9.4

Building Better. SQL Server Databases

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Exact Numeric Data Types

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

Database Connectivity with JDBC

New Features Bulletin Replication Server Options 15.6

5. SQL Query Syntax 1. Select Statement. 6. CPS: Database Schema

Project Database Rules

ColumnStore Indexes UNIQUE and NOT DULL

Building Better. SQL Server Databases

BEGINNING T-SQL. Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

Compression Users Guide. Adaptive Server Enterprise 15.7

Oracle SQL Developer. Supplementary Information for Microsoft SQL Server and Sybase Adaptive Server Migrations Release 2.

About System Setup Pages

ColdFusion Summit 2016

How to Migrate Microsoft SQL Server Connections from the OLE DB to the ODBC Provider Type

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Get Table Schema In Sql Server 2005 Modify. Column Datatype >>>CLICK HERE<<<

Object-Oriented Programming With SAS Via PROC DS2

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Compression Users Guide. SAP Adaptive Server Enterprise 16.0

MySQL Creating a Database Lecture 3

MTAT Introduction to Databases

Required Materials - SQL Server Management Studio - Your copy of the Northwind database

Basic SQL. Basic SQL. Basic SQL

Module 5: Implementing Data Integrity

ACHOO - THE FLU, SAS & YOU

Introduction to IBM DB2

Programming and Database Fundamentals for Data Scientists

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

RDBMS Basics: What Makes Up a SQL Server Database?

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

1RDBMS Basics: What Makes Up a SQL Server Database?

Every Byte Counts. Why Datatype Choices Matter. Andy Yun, Database Architect/Developer

Mirror Replication Agent

Microsoft SQL Server - Concepts 2 005

SQL Data Definition and Data Manipulation Languages (DDL and DML)

SQL Server 2014 Column Store Indexes. Vivek Sanil Microsoft Sr. Premier Field Engineer

Calgary SAS Users Group April 7, 2016 Peter Eberhardt Fernwood Consulting Group Inc. Xue Yao Winnipeg Regional Health Authority

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Documentation Accessibility. Access to Oracle Support. Supported Browsers

Full file at

SQL: Data Definition Language

New Features Bulletin Replication Server Options

Lab # 4. Data Definition Language (DDL)

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

COMP 430 Intro. to Database Systems

Database and table creation

Tasks for Installation / Introduction Concepts. 1. What are the Advantages of SQL Server? Data Integrity

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Data and Tables. Bok, Jong Soon

CST272 SQL Server, SQL and the SqlDataSource Page 1

The Top 20 Design Tips

Kaotii.

1SQL Server 2012 Architecture

CS634 Architecture of Database Systems Spring Elizabeth (Betty) O Neil University of Massachusetts at Boston

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Data types String data types Numeric data types Date, time, and timestamp data types XML data type Large object data types ROWID data type

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Outsourcer Administrator Guide Document Date

WHAT S NEW IN FOUNDATION SAS FOR 9.4

Principles of Data Management

IBM Database Conversion Workbench 3.5

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

SQL. Often times, in order for us to build the most functional website we can, we depend on a database to store information.

SQL Structured Query Language Introduction

In-Memory Tables and Natively Compiled T-SQL. Blazing Speed for OLTP and MOre

Chapter 8 How to work with data types

Introduction to Column Stores with Microsoft SQL Server 2016

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

Database Design on Construction Project Cost System Nannan Zhang1,a, Wenfeng Song2,b

EDB Postgres Hadoop Data Adapter Guide

Simple Quesries in SQL & Table Creation and Data Manipulation

user specifies what is wanted, not how to find it

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

From theory to practice. Designing Tables for an Oracle Database System. Sqlplus. Sqlplus. Technicalities. Add the following to your.

Designing Tables for an Oracle Database System. From theory to practice

Data Definition Language with mysql. By Kautsar

MySQL: an application

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Creating databases using SQL Server Management Studio Express

Informatica Cloud Spring Data Integration Hub Connector Guide

IMPLEMENTING LOGICAL DATA TYPES

Data Definition Language (DDL)

MTAT Introduction to Databases

INFORMATION TECHNOLOGY NOTES

SAS 9.2 Table Server. TSPL Language Reference

Transcription:

Lab : Tables and Constraints Objective You have had a brief introduction to tables and how to create them, but we want to have a more in-depth look at what goes into creating a table, making good choices about data types, and enforcing constraints to maintain the integrity of your data. In this lab you will create a new table that contains user-defined data types, as well as different sorts of relationships and constraints. Turn-in Instructions At the end of the lab you will create scripts to reproduce (1) the Login table, (2) the new UserProfiles table, and (3) your new user-defined data types. You ll do this by right clicking on the object, going to Script object As and selecting CREATE TO. You ll use the naming convention <name of object>_<your username>.sql. Assignment Details Task 1 SQL Server 2005 has the following data types available: Common Data Type Integer Exact Numeric Approximate numeric Monetary Date and time Character SQL Server system supplied data types int bigint smallint tinyint bit ( 0 or 1) decimal [(p[,s])] numeric [(p[,s])] digits before and after the decimal float [(n)] real money smallmoney datetime smalldatetime char[(n)] varchar[(n)] text, varchar(max) Number of bytes 2 1 1 bit 2 17 0-000 0-000 0-2 GB

Unicode Character Binary nchar[(n)] nvarchar[(n)] ntext, nvarchar(max) Binary[(n)] Varbinary[(n)] 0-000 (up to 000 characters) 0-000 (up to 000 characters) 0-2 GB 0-000 Image image 0-2 GB Global Identifier Special uniqueidentifier 16 Cusor Timestamp Sysname Table Sql_variant 0-256 256 0-016 In addition to all these built in types, SQL Server also allows you to create your own unique data types. A user-defined datatype is defined for a specific database. If the new type is defined in the master database, it is automatically included in all databases that are subsequently created. The definition of these datatypes are stored in the systypes table. User-defined data types are useful when there is a common data type element amongst many tables in the database. You have decided that you need more information stored about users than what the Login table (which you created in lab 1 in your copy of the Northwind database) currently specifies. One additional thing that you need to store is users first and last names. Since these are such common elements in a database, you ll want to create a datatype for first names and one for last names. Open SQL Server Management Studio and select New Query. You will be using a command called CREATE TYPE which creates user-defined data types (see example below). Name your user defined data-types in some appropriate and descriptive manner. Choose an appropriate system-defined data type. (There are quite a few design considerations to keep in mind: variable or fixed length data, maximum possible length for a name, disk space, whether or not null is a valid entry.) Don t forget to include an appropriate USE database command! Example CREATE TYPE command: CREATE TYPE UID FROM varchar(10) NOT NULL; Refer to the Books Online documentation in the Help menu for an explanation of the parameters for CREATE TYPE. When done with the task, in the Object Explorer look under Programmability Types to check your work.

Task 2 In your Login table, you defined LoginID as a PRIMARY KEY. This will be used as a lookup column in a new table containing additional user data, linking the tables together with a PRIMARY KEY / FOREIGN KEY relationship. Go to your original copy of Northwind. This should have your Login table in it. a. Create a new table called UserProfiles. b. Create a FOREIGN KEY column with an appropriate name. To make relationships between tables, click on the button which allows you to manage relationships. This dialog allows you to add PRIMARY KEY / FOREIGN KEY relationships by pushing the Add button. Your primary key is in your Login table, and your foreign key is what you just created in the UserProfiles table. Data type and properties must match between keys. Be sure to give your relationship an appropriate name. Also, set appropriate Delete and Update policies; see the INSERT and UPDATE section of the dialog. c. Create columns for the first name and last names (using your user-defined data types) and name them appropriately. d. Because you want to know about the age and location of the people accessing your system, create columns to store their birthday and the state where they live. Choose appropriate names and data types. Task 3 SQL Server allows you to put constraints on your data to verify its integrity. CHECK constraints can be added using either a query or regular management view. The Transact-SQL to add a CHECK constraint looks like this: USE <database name> ALTER TABLE dbo.<table name> ADD CONSTRAINT <constraint name> CHECK ( <conditional statement> ) Among other things, conditional statements can use =, >, <, >=, or <= and can be connected using the AND and OR keywords. To add a CHECK in management view, click on the table where you want to add the constraint, right click and select modify. In the table design, click manage check constraints, and then click the Add button in the resulting Check Constraints dialog. Add a constraint to the column storing the birthday of the user. Specify that it must be after 01-01-1900 and before today s date, which can be generated with a predefined system function. (Hint: Look up current date and time in the Online Books index.) After adding the constraint, test it by adding data to the UserProfiles table. (You learned how to do this in Lab 1.)

Task SQL Server also allows you to specify properties of columns. It occurs to you that if usernames are not unique, you will have some problems with logging into your system. Open a new query. Use an ALTER TABLE query to specify that the username column in the Login table must be unique. Example: USE Northwind -- Substitute the name of your copy of Northwind here ALTER TABLE dbo.suppliers ADD CONSTRAINT U_CompanyName UNIQUE NONCLUSTERED (CompanyName) Look in the Object Explorer for this new constraint. (Hint: Try looking under Keys.) Task 5 Assume you only want to have people from certain states allowed to access your database. Create a check on the column where you stored which state they were from. The check should only allow this column to contain the values ( IN, IA, IL, MO ) or any combination of your favorite states. (Hint: Search help for the keyword IN.) Edit the data in UserProfiles to test your new check. Turn-in Instructions Open Management Studios. Create a scripts to reproduce (1) the Login table, (2) the UserProfiles table, and (3) Your user-defined data types Do this by right clicking on the object, going to Script object As and selecting CREATE TO then File. Use the naming convention <name of object>_<your username>.sql. Review the scripts and verify that the generated code satisfies the requirements of the lab. Create a.zip or.rar archive of these.sql files. Submit the archive to the drop box on ANGEL. Revision History Dec. 13, 2006: Dec. 7, 2006: Jan. 12, 2006: Dec. 12, 2005 Cleaned up by Curt Clifton and Eliza Brock Updated for SQL Server 2005 by Andrew Foltz Clarified turn-in instructions, Curt Clifton. Updated by Curt Clifton and Steve Chenoweth

Dec. 15, 200: Dec. 13, 200: Reviewed and edited by Andy Cooper. Created by Jennifer Ford.