SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
Database Systems: Design, Implementation, and Management Tenth Edition
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
1 Introduction to Web Application Introduction to Data Base.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Stored Procedures A stored procedure is a named collection of SQL statements language. You can create stored procedures for commonly used functions and.
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.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
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.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
Visual Programing SQL Overview Section 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Databases Introduction - concepts. Concepts of Relational Databases.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Big Data Yuan Xue CS 292 Special topics on.
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
SQL, the Structured Query Language
Fundamentals of DBMS Notes-1.
SQL Query Getting to the data ……..
More SQL: Complex Queries,
Introduction to SQL Karolina muszyńska
The Basics of Data Manipulation
Chapter Name By Adrienne Watt
Structured Query Language
The Basics of Data Manipulation
Database systems Lecture 3 – SQL + CRUD
C1. SQL BAsic.
Contents Preface I Introduction Lesson Objectives I-2
Indexes and more Table Creation
Presentation transcript:

SQL Basics

SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management system SQL Instrunctions can be grouped into different classes:  Date defininition language (DDL): create, alter, drop  Data manipulation language (DML):  Insert – insert new data into a database  Update – updates data in a database  Delete – deletes data from a database  Select – extracts data from a database 2

Data definition language (DDL) CREATE TABLE Studenti( nrMatricol int NOT NULL, nume varchar(20) NULL, initialaTatalui varchar(3) NULL, prenume varchar(30) NULL, facultate varchar(10) NULL, specializare varchar(10) NULL, grupa int NULL ) 3

DDL (Cont) CONSTRAINTS NOT NULL - specifies that the column does not accept NULL values PRIMARY KEY - constraints identify the column or set of columns that have values that uniquely identify a row in a table FOREIGN KEY - constraints identify and enforce the relationships between tables. You cannot insert a row with a foreign key value, except NULL, if there is no candidate key with that value.  The ON DELETE clause controls what actions are taken when you try to delete a row to which existing foreign keys point. The ON DELETE clause has the following options: NO ACTION specifies that the deletion fails with an error; CASCADE specifies that all the rows with foreign keys pointing to the deleted row are also deleted; SET NULL specifies that all rows with foreign keys pointing to the deleted row are set to NULL; SET DEFAULT specifies that all rows with foreign keys pointing to the deleted row are set to their default value  The ON UPDATE clause defines the actions that are taken if you try to update a candidate key value to which existing foreign keys point. This clause also supports the NO ACTION, CASCADE, SET NULL and SET DEFAULT options. UNIQUE - constraints enforce the uniqueness of the values in a set of columns CHECK (P), where P is a predicate - constraints enforce domain integrity by limiting the values that can be put in a column 4

DDL (Cont) ALTER TABLE Studenti ADD nationalitate varchar(30) ALTER TABLE Studenti ADD CONSTRAINT PK_nrMatricol primary key (nrMatricol) ALTER TABLE Studenti ALTER COLUMN initialaTatalui varchar(5) 5

DDL (Cont) Drop table table_name Drop view schema_name.view_name Drop function schema_name.function_name Drop index index_name on object_name Drop trigger trigger_name on {database | all server} Drop database database_name Drop schema schema_name Drop role role_name Drop user user_name 6

Data manipulation language (DML) INSERT INTO Studenti (nrMatricol,nume,initialaTatalui,prenume,facultate,specializare,grupa,nationalitate) VALUES (12546,'Georgescu','A','Maria','MI','info-ro',122,'romana') insert into Grupe select grupa, specializare from Studenti 7

DML (Cont) update Studenti set nrMatricol = nrMatricol+1 update Studenti set nume = 'Ionnica' where nrMatricol =

DML (Cont) DELETE FROM Studenti WHERE nrmatricol < DELETE FROM Studenti DELETE * FROM Studenti TRUNCATE TABLE Studenti The difference between truncate and delete removes all rows from a table the operation cannot be rolled back and no triggers will be fired is faster and doesn't use as much undo space as a DELETE. that the truncate resets auto increment column. 9

DML (Cont) SELECT nrMatricol,nume,initialaTatalui,prenume,facultate,specializare,grupa,nationalitate FROM Studenti WHERE nrmatricol = WHERE grupa = 123 WHERE nationalitate is null WHERE prenume like 'V%' WHERE specializare like '%ro' 10

DML (Cont) IS NULL / IS NOT NULL – checks if the column is null or not BETWEEN – selects a range of data between two values. The values can be numbers, text, or dates [val1, val2) EXISTS / NOT EXISTS – returns the value true if the argument subquery is nonempty IN / NOT IN – operator allows you to specify multiple values in a WHERE clause. LIKE / NOT LIKE – operator is used in a WHERE clause to search for a specified pattern in a column; ‘%’ - matches any substring, ‘_’ - matches any character. 11

Aggregate Functions Avg() – returns the average values Min() – returns the min value Max() – returns the max value Sum() – returns the average values Count() – returns the number of rows 12

Stored Procedures CREATE PROC [ EDURE ] [ owner. ] procedure_name [ ; number ] AS sql_statement [...n ] CREATE PROCEDURE au_info_all AS SELECT au_lname, au_fname, title, pub_name FROM authors a INNER JOIN titleauthor ta ON a.au_id = ta.au_id INNER JOIN titles t ON t.title_id = ta.title_id INNER JOIN publishers p ON t.pu EXEC au_info_allb_id = p.pub_id GO 13

Cursor DECLARE cursor_name CURSOR FOR select_statement OPEN cursor_name FETCH NEXT FROM curtor_name WHILE = 0 BEGIN … sql_statement FETCH NEXT FROM curtor_name END CLOSE cursor_name ; DEALLOCATE cursor_name; 14