CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
SQL server Section 4&5.
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Introduction to Structured Query Language (SQL)
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Using SQL to create tables Ways of using Databases.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Introduction to Structured Query Language (SQL)
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
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.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
Session 5: Working with MySQL iNET Academy Open Source Web Development.
The Relational Model These slides are based on the slides of your text book.
Copyright © Curt Hill Index Creation SQL.
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.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
SQL Server Indexes Indexes. Overview Indexes are used to help speed search results in a database. A careful use of indexes can greatly improve search.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL John Ortiz Cs.utsa.edu.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
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.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
Visual Programing SQL Overview Section 1.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
Session 1 Module 1: Introduction to Data Integrity
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Agenda for Class 2/20/2014 Introduce Microsoft’s SQL Server database management system. Use the lab to discuss how to CREATE, DROP and populate (INSERT)
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
In this session, you will learn to: Manage databases Manage tables Objectives.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Insert, Update and the rest…
SQL Creating and Managing Tables
Module 5: Implementing Data Integrity by Using Constraints
Referential Integrity
SQL Creating and Managing Tables
The Relational Model Relational Data Model
SQL Creating and Managing Tables
Oracle Data Definition Language (DDL)
Relational Database Design
Presentation transcript:

CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP

Agenda o Introduction to MS SQL Server and installation o Exercise o Assignment o Quiz

Syntax Conventions o See Table 1-5 on Page 14

Entity-Relationship (ER) o See Figure 1-1 on Page 19

SQL Server Management Studio o Windows Authentication o SQL Server Authentication o See Figure 2-1 and 2-2 on Page 26

Creating Database Objects o Two types of databases: o System Databases: master Tempdb Model msdb o User Databases: User created

Creating Database Objects (cont.) CREATE DATABASE db_name [ON [PRIMARY] file_spec1 {,file_spec2}…] [LOG ON file_spec3 {, file_spec4}…] [COLLATE collation_name] [FOR {ATTACH | ATTACH_REBUILD_LOG}] o db_name is database name. o LOG ON is to define where to keep the log. o COLLATE is to define the collation.

Creating Database Objects (cont.) o To create new database: USE master CREATE DATABASE cn2180 o To create a snapshot of the database: CREATE DATABASE database_name ON file_spec1 {, file_spec2}…] AS SNAPSHOT OF source_db_name

Creating Table CREATE TABLE tbl_name (col_name1 type1 [NOT NULL|NULL] [{, col_name2 type2 [NOT NULL|NULL]}…]) o By default all field are null able. o See Example 4.3 on P79. CREATE TABLE RMStudent(s_id integer NOT NULL, s_name char(20) NOT NULL, s_lname char(20) NOT NULL)

UNIQUE Clause o Candidate keys – all fields that has unique value and could be used as primary key. o Use UNIQUE clause with CREATE TABLE or ALTER TABLE statement. [CONSTRAINT c_name] UNIQUE [CLUSTERED | NONCLUSTERED] (col_name1 [{, col_name2} …]) o CONSTRAINT is to assign explicit name. o By default it is non clustered, which means the constraint will not be indexed.

PRIMARY KEY Clause o Use with CREATE TABLE or ALTER TABLE [CONSTRAINT c_name] PRIMARY KEY [CLUSTERED | NONCLUSTERED] (col_name1 [{,col_name2}…]) o Primary key must be NOT NULL USE cn2180 DROP TABLE RMStudent CREATE TABLE RMStudent(s_id integer NOT NULL, s_name char(20) NOT NULL, s_lname char(20) NOT NULL CONSTRAINT prim_sid PRIMARY KEY (s_id)))

FOREIGN KEY Clause [CONSTRAINT c_name] [[FOREIGN KEY] (col_name1 [{, col_name2}…]]) REFERENCES tbl_name (col_name3 [{, col_name4}…]) [ON DELETE {NO ACTION|CASCADE|SET NULL|SET DEFAULT}] [ON UPDATE {NO ACTION|CASCADE|SET NULL| SET DEFAULT}] o FOREIGN KEY must match the number and the data types of the columns in the REFERENCES clause.

FOREIGN KEY (Cont.) USE cn2180 CREATE TABLE ClassRm(c_id integer NOT NULL, c_loc char(20) NOT NULL, c_student char(20) NOT NULL, c_sid integer NOT NULL CONSTRAINT prim_cid PRIMARY KEY (c_id))) CONSTRAINT foreign_class FOREIGN KEY (c_sid) REFERENCES RMStudent (s_id))

Referential Constraints o Case 1: Add new data, new student, in foreign key field to ClassRm with data that is not exist in parent table (RmStudent). REJECT! o Case 2: Update the data in foreign key field with data that is not exist in parent table. REJECT! o Case 3: Update the primary key field in parent table to new value. The reference table has that key. REJECT! o Case 4: Delete the row of the parent table with the the primary key field that is referred from reference table. REJECT!

ON DELETE and ON UPDATE Options To work around referential constraints: o No Action – by default, you can modify only those value that is not referred to o Cascade – The data in parent table can be modified and will update the referencing table accordingly. o Set Null – All modification in parent table will affect the referencing table to be null if there is inconsistencies o Set Default – Same as Set Null, except the value are set to a default value.

ON DELETE and ON UPDATE Options USE cn2180 CREATE TABLE ClassRm(c_id integer NOT NULL, c_loc char(20) NOT NULL, c_student char(20) NOT NULL, c_sid integer NOT NULL CONSTRAINT prim_cid PRIMARY KEY (c_id))) CONSTRAINT foreign_class FOREIGN KEY (c_sid) REFERENCES RMStudent (s_id) ON DELETE CASCADE, CONSTRAINT foreign_class FOREIGN KEY (c_sid) REFERENCES RMStudent (s_id) ON UPDATE CASCADE)

CHECK Clause o Same as Validation Rule in MS Access. [CONSTRAINT c_name] CHECK [NOT FOR REPLICATIION] expression USE sample CREATE TABLE newtbl (cust_no INTEGER NOT NULL, cust_grp CHAR(3) NULL, CHECK (cust_grp IN (‘c1’,’c2’,’c10’)))

SQL Basic Objects o Comment /* */ o Reserved keywords o Data Type o Numeric – See page 53 o String – See page 54 o VARCHAR o Date and Time – See page 56 o User defined

SQL_VARIANT Data type o SQL_VARIANT can be used to store values of various data types at the same time.

Modifying database o Altering a table ALTER TABLE tbl_name ADD col_name type [NULL|IDENTITY] [{, col_name type [NULL|IDENTITY]}…] DROP COLUMN col_name [{, col_name} …] ALTER COLUMN col_name type {NULL|IDENTITY} [{, col_name type NULL|IDENTITY}…] ADD tbl_constraint DROP tbl_constraint

Assignment o Instruction: o Capture the screen for each question o Put question number under each screen o E.4.1 o E.4.6 – E.4.14 o E.4.16 – E.4.19