Introduction to SQL Server. nikos bikakis

Similar documents
Introduction to Relational Database Management Systems

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

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

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

Working with Databases and Java

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

Basic SQL. Basic SQL. Basic SQL

Relational Database Systems Part 01. Karine Reis Ferreira

Lab # 4. Data Definition Language (DDL)

DB tutorial using Database.NET dbforge Studio Express and SQLiteSpy

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

CS275 Intro to Databases

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

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Accessing databases in Java using JDBC

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

SQL Server 2008 Tutorial 3: Database Creation

Introduction to Databases

General Overview - rel. model. Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Reminder: our Mini-U db

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

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

Database Application Programs PL/SQL, Java and the Web

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

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

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

COSC344 Database Theory and Applications. σ a= c (P) Lecture 3 The Relational Data. Model. π A, COSC344 Lecture 3 1

Introduction to Database Systems CSE 414

Chapter 1 SQL and Data

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Chapter 4. The Relational Model

The Basic (Flat) Relational Model. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Physical Design of Relational Databases

Database Application Development

Full file at

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

Using Relational Databases for Digital Research

The Relational Model

SQL Server. Lecture3 Cascading referential integrity constraint

Introduction to Data Management CSE 344

The Web Application Developer s. Red Hat Database. View. October 30, Webcast. Patrick Macdonald, Fernando Nasser. Red Hat Database Engineering

The Relational Model

DB Programming. Database Systems

CS2300: File Structures and Introduction to Database Systems

Logging and Recovery. 444 Section, April 23, 2009

Database Management Systems,

Introduction to Microsoft Access

CS 377 Database Systems

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

The Relational Model of Data (ii)

First lecture of this chapter is in slides (PPT file)

Introduction to Microsoft Access 2010

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Oracle Create Table Foreign Key On Delete No

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 1 An introduction to relational databases and SQL

SQL DATA DEFINITION LANGUAGE

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Vendor: IBM. Exam Code: Exam Name: DB2 9 Application Developer. Version: Demo

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

Relational Data Model. Christopher Simpkins

SQL DATA DEFINITION LANGUAGE

CS2300: File Structures and Introduction to Database Systems

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

Integrity constraints, relationships. CS634 Lecture 2

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

Downloaded from

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Relational Data Model

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

The Relational Model. Chapter 3

Keys are fields in a table which participate in below activities in RDBMS systems:

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

JAVA AND DATABASES. Summer 2018

CSC 453 Database Technologies. Tanu Malik DePaul University

Introduction to Microsoft Access 2016

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

CPS510 Database System Design Primitive SYSTEM STRUCTURE

Referential Integrity and Other Table Constraints Ray Lockwood

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

Transcription:

Introduction to SQL Server nikos bikakis bikakis@dblab.ntua.gr dblab NTU Athens Dec 2011

Introduction MS SQL Server Database server Product of Microsoft Relational DB From: 1989 (SQL Server 1.0) To: July 2011 (SQL Server 2008 R2) Runs on: Windows 7, Vista, Server (03&08), XP, ME, 98 Platform: 32 & 64 2

Client / Server Database 5 Results are presented to the user 1 User Submits Query 2 Query is sent to the server 3 Query is executed on the server Database Client 4 Results are sent back to the client Server 3

SQL Database Objects Objects Tables Views Stored Procedures Functions Rules Defaults Cursors Triggers 4

SQL Server Data types Integer Float Text : Integer number : Real numbers : Strings Decimal : Real numbers Money Date Binary : Monetary data : Date and time : Images and other large objects Miscellaneous : Different types special to SQL Server 5

Operators Arithmetic : +, -, *, /, % Assignment : = Comparison : <, >, <=, >= <>, =,!=,!<,!> Logical : AND, OR, NOT, IN, LIKE, BETWEEN, ANY, ALL, EXISTS, SOME String : Concatenation (+) Unary : -, +, ~ Bitwise: &,, ^ 6

Operations (1/3) Database Level Changing a database Use <dbname> Creating a database Create database <dbname> Dropping a database Drop database <dbname> 7

Operations (2/3) Schema Level Create Table Drop Table Alter Table (Used to modify table structure) Add new column Change data type of existing column Delete a column Add or remove constraints like foreign key, primary key 8

Operations (3/3) Data Level Select Insert Update Update data to all/selected columns/rows Delete Delete all/selected rows from table 9

Integrity Constraints in RDBS Integrity constraints are used to ensure accuracy and consistency of data in a relational database. Types Entity integrity Domain Integrity Referential Integrity User Defined Integrity 10

Entity Integrity Every table must have a primary key Primary key should be unique and not null Used during Insertions and Updates SQL PRIMARY KEY UNIQUE (Candidate Keys) Primary keys Referenced by Foreign keys Indexes 11

Creating Unique Values in DBMS MS SQL Server Identity (seed, increment) Seed is the initial value Increment is the value by which we need to skip to fetch the next value Identity(1,2) will generate sequence numbers 1,3,5,7 MySQL AUTO_INCREMENT The starting value is 1, and it will increment by 1 for each new record. AUTO_INCREMENT = k (start from k value) 12

Domain Integrity Column Constraints NOT NULL CHECK (e.g., CHECK( age >= 0) ) Domain Constraints Use Column Constraints Similar to user-defined datatypes Reusability Programmer friendly (gives names) Used during Insertions and Updates 13

Domain Constraints Example Define Domain Constraint CREATE DOMAIN validage INT ( CONSTRAINT positive CHECK (VALUE >= 0), CONSTRAINT limit CHECK (VALUE < 150 ), CONSTRAINT not-null-value CHECK( VALUE NOT NULL)); Use Domain Constraint CREATE TABLE Employee (... age validage, ) 14

Referential Integrity The referential integrity constraint states that a tuple in one relation that refers to another relation must refer to an existing tuple in that relation. Foreign Key value Primary Key value SQL pk type PRIMARY KEY FOREIGN KEY fk REFERENCES pk Artist Table Primary Key Foreign Key Song Table artist_name artist_id artist_id song_id song_name Aaa 1 Bbb 2 Ccc 3 2 1 xxx 1 2 www 2 3 zzz 15

Referential Integrity Example Artist Table Primary Key Foreign Key Song Table artist_name artist_id artist_id song_id song_name Aaa 1 Bbb 2 Ccc 3 2 1 xxx 1 2 www 2 3 zzz Delete tuple (2, Bbb) Possible scenarios Reject Set Song.artist_id = null Delete Song tuples 16

Referential Integrity Constraints in SQL CREATE TABLE a (... FOREIGN KEY fk REFERENCES pk action... ) Where action is: nothing or NO ACTION (deletion/update rejected) ON DELETE SET NULL / ON UPDATE SET NULL ON DELETE CASCADE / ON UPDATE CASCADE 17

Access SQL Server Java Connection con = DriverManager.getConnection( jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=db1, "myuser", "mypassword"); Statement stmt = con.createstatement(); ResultSet rs = stmt.executequery( "SELECT Name, Salary FROM Employee"); while (rs.next()) { System.out.println(rs.getString("Name") ); System.out.println(rs.getFloat("Salary") ) ; } 18

Access SQL Server VB.NET Dim sqlconn As New SqlConnection( server=127.0.0.1;trusted_connection=no;database=db1; user=myuser; password=mypassword ) Dim sqlcomm = New SqlCommand("SELECT * FROM Films", sqlconn) Dim sqlreader = sqlcomm.executereader() Do While (sqlreader.read()) Console.Write(sqlReader("film_ID").ToString() + ") Console.Write(sqlReader("title").ToString() + " ") Console.Write(sqlReader("director").ToString() + " ") Console.WriteLine(sqlReader("year_produced").ToString()) Loop 19

Project Implementation Linux/7/Vista/Win2000/XP/2003/98/ME SQL Server 2000/2005/2008/postgres/mysql JAVA, VB.NET, PYTHON, C++. SQL Server 2005 ΒΙΒΛΙΟΘΗΚΗ ΗΛΕΚΤΡΟΛΟΓΩΝ 20

Project Requirements Database Design and implementation Use Integrity Constraints!!! Graphical User Interface Fully functional View DB Insert DB Query DB etc. User-friendly Drop-down list Radio button Check box etc. Define meaningful Queries, Views, etc. 21

< Thank You! /> :-) 22