Nogle udpluk af mine ’SQL-favoritter’ af Dan Foldager

Slides:



Advertisements
Similar presentations
Data Definition Language (DDL)
Advertisements

MySQL. To start go to Login details: login: labuser password:macimd15 – There.
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
1 Chapter 2: Creating and Modifying Database Tables.
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.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
5 Chapter 5 Structured Query Language (SQL1) Revision.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Agenda Journalling More Embedded SQL. Journalling.
Irwin/McGraw-Hill Copyright © 2000 The McGraw-Hill Companies. All Rights reserved Whitten Bentley DittmanSYSTEMS ANALYSIS AND DESIGN METHODS5th Edition.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Introduction to SQL.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
© 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.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
GLOBEX INFOTEK Copyright © 2013 Dr. Emelda Ntinglet-DavisSYSTEMS ANALYSIS AND DESIGN METHODSINTRODUCTORY SESSION EFFECTIVE DATABASE DESIGN for BEGINNERS.
1 Chapter 2: Creating and Modifying Database Objects.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
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)
Chapter 3: Relational Databases
Databases Introduction - concepts. Concepts of Relational Databases.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
8 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Big Data Yuan Xue CS 292 Special topics on.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
CS 3630 Database Design and Implementation
TABLES AND INDEXES Ashima Wadhwa.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Module 5: Implementing Data Integrity by Using Constraints
Referential Integrity
Lecturer: Mukhtar Mohamed Ali “Hakaale”
DBS201: More on SQL Lecture 2.
SQL data definition using Oracle
2016, Fall Pusan National University Taehoon Kim
ORACLE I 2 Salim Phone : YM : talim_bansal.
Referential Integrity
אבטחת נתונים בסביבת SQL Data Security
SQL DATA CONSTRAINTS.
Data Model.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Creating and Managing Database Tables
Chapter 2: Creating And Modifying Database Tables
Data Definition Language
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Indexes and more Table Creation
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Database Instructor: Bei Kang.
Presentation transcript:

Nogle udpluk af mine ’SQL-favoritter’ af Dan Foldager 14. September 2010

AGENDA Database Nye ord og begreber og meget andet.. Data retrieval Pivot Hvorfor SQL ? Mere info.. Links

Referential integrity Journaling Materialized query table Database Nye ord Fra DDS til DDL XML Large objects Referential integrity Journaling Materialized query table CREATE TABLE AS (SELECT fra ’data-dictionary’) field-reference-file System i Navigator kan danne SQL udfra DDS

Database – nye ord Felt = kolonne (column) Record = række (row) Library = samling (collection), schema Index = index / view Datatypes: beløbsfelt = Money !!! Large objects: - clob (character large object) - blob (Binary large object) - XML Cascade (on delete) Referential constraint UDF = user defined function MQT = Materialized Query Table Null ? … Datatyper…. Lav liste (matrix m release-numre)

Database - fra DDS til DDL (1 af 3) System i Navigator Fra DDS til SQL via få klik… OBS: Editcode / editword mm.

Database - fra DDS til DDL (2 af 3) - SQL-fejl (editcode, VALUES mm) CREATE TABLE LABEL ON TABLE HER ER NOGET AT PRØVE.. (lille mand)

Database - fra DDS til DDL (3 af 3) CREATE TABLE mylib/myfile AS (SELECT RNO1, RNO2, RNAM, ..., RSTAC, REDAT, REUSR, RMDAT, RMUSR FROM *libl/AVDTADIC) WITH NO DATA Generelt: CREATE TABLE lib/table AS (SELECT field1, field2, field7, field22, … FROM lib/fieldref )

Hvordan ? Library -> files -> !!! Sourcecode (type = SQL) RUNSQLSTM executes the sourcemember RUNSQLSTM (ikke compile – forklar [interpretiv, dvs ’execute’))

Ny applikation.. (1 af 4) CRTLIB gsgruppen : CREATE SCHEMA gsgruppen or CREATE COLLECTION gsgruppen System i Navigator Skemaer – SCHEMA – COLLECTION !!! OBS: h.klik for at danne din liste i træet..

Ny applikation.. (2 af 4) …lad os ombryde til læsbart !! CRTFILE master : CREATE TABLE gsgruppen/master (medlnr INT NOT NULL WITH DEFAULT, navn VARCHAR (50) NOT NULL WITH DEFAULT, inddat DATE , uddat DATE, email VARCHAR ( 100) NOT NULL WITH DEFAULT, tekst VARCHAR (1024) NOT NULL WITH DEFAULT, firma VARCHAR (50) NOT NULL WITH DEFAULT, CONSTRAINT medlnr_pk PRIMARY KEY (medlnr),CONSTRAINT medlnr_value CHECK (medlnr > 0 )) CRTFILE trans : CREATE TABLE gsgruppen/trans (faktNR INT NOT NULL WITH DEFAULT, trndat DATE , medlnr INT NOT NULL WITH DEFAULT, trntxt VARCHAR (100) NOT NULL WITH DEFAULT, amount DECIMAL (9,2) NOT NULL WITH DEFAULT, CONSTRAINT faktnr_pk PRIMARY KEY (faktnr), CONSTRAINT faktnr_value CHECK (faktnr > 100000 ), CONSTRAINT medlnr_fk FOREIGN KEY(medlnr) REFERENCES gsgruppen/master(medlnr) ON DELETE CASCADE ON UPDATE RESTRICT) TODO… …lad os ombryde til læsbart !!

Ny applikation.. (2 af 4) CRTFILE master : CRTFILE trans : TODO… CREATE TABLE gsgruppen/master ( medlnr INT NOT NULL WITH DEFAULT, navn VARCHAR (50) NOT NULL WITH DEFAULT, inddat DATE, uddat DATE, email VARCHAR ( 100) NOT NULL WITH DEFAULT, tekst VARCHAR (1024) NOT NULL WITH DEFAULT, firma VARCHAR (50) NOT NULL WITH DEFAULT, CONSTRAINT medlnr_pk PRIMARY KEY (medlnr), CONSTRAINT medlnr_value CHECK (medlnr > 0 )) CRTFILE trans : CREATE TABLE gsgruppen/trans ( faktNR INT NOT NULL WITH DEFAULT, trndat DATE , medlnr INT NOT NULL WITH DEFAULT, trntxt VARCHAR (100) NOT NULL WITH DEFAULT, amount DECIMAL (9,2) NOT NULL WITH DEFAULT, CONSTRAINT faktnr_pk PRIMARY KEY (faktnr), CONSTRAINT faktnr_value CHECK (faktnr > 100000 ), CONSTRAINT medlnr_fk FOREIGN KEY(medlnr) REFERENCES gsgruppen/master(medlnr) ON DELETE CASCADE ON UPDATE RESTRICT ) TODO…

Ny applikation.. (3 af 4) TODO…

Ny applikation.. (4 af 4) Data… : TODO… INSERT INTO gsgruppen/master VALUES(NULL, ’Dan’, ’Foldager’,….) INSERT INTO gsgruppen/trans ….????????? TODO…

Nogle tips .. Ret stavefejl i feltoverskrift Dokumentation… LABEL ON COLUMN PALPRIS.PRP1UD IS 'Pris 1/1 paller - ud –’ Dokumentation… COMMENT ON COLUMN … [mulighed for 2000 char dokumentation] SQL function Labels : - virker også på DDS-beskrevne filer - COMMENT ON COLUMN FLDREF ( EntryDate IS ‘Date of record creation. Date is set automatically by the syst em.’, EntryUser IS ‘User-ID for person who entered this record. Rows may exist whe re special values starting with an asterisk (”*”) are used, fx ”*CONVERT” an d ”*IMPORT”.’, UpdateDate IS ‘Date of latest record update. Date is set automatically by the system (trigger-pgm). Field is null at record entry to the table.’, STATUS IS ‘Record status field. Defaults to ”A” (Active) when record is created. May be set to ”I” (Inactive), ”D” (Deleted) or ”*” (may be erase d.’ ); SQL i CL ?

Data retrieval SELECT…. Pivot SELECT (CURRENT DATE - 3 DAYS), davno from avadfo_9/avdta

Nogle tips .. Pivot !! (1 af 5) OPGAVE: Vis avance pr varegruppe over tid. SELECT ssvagr, ssdato, ssbelb - sskost AS avance FROM qqq/saltrans

Nogle tips .. Pivot !! (2 af 5) SELECT ssvagr, ssdato, ssbelb - sskost AS avance FROM qqq/saltrans SELECT ssvagr, year(ssdato) as YEAR, ssbelb - sskost AS avance FROM qqq/saltrans QUARTER – WEEKNO – YEAR fra dato-felt

Nogle tips .. Pivot !! (3 af 5) SELECT ssvagr, ssdato, ssbelb - sskost AS avance FROM qqq/saltrans SELECT ssvagr, year(ssdato) as YEAR, ssbelb - sskost AS avance FROM qqq/saltrans SELECT ssvagr, year(ssdato) as YEAR, sum(ssbelb - sskost) AS avance FROM qqq/saltrans GROUP BY ssvagr, year(ssdato) ORDER BY ssvagr, year(ssdato)

Nogle tips .. Pivot !! (4 af 5) SELECT ssvagr, year(ssdato) as YEAR, sum(ssbelb - sskost) AS avance FROM qqq/saltrans GROUP BY ssvagr, year(ssdato) ORDER BY ssvagr, year(ssdato) select ssvagr, sum(case when year(ssdato) = 2006 then (ssbelb - sskost) end) as "2006", sum(case when year(ssdato) = 2007 then (ssbelb - sskost) end) as "2007", sum(case when year(ssdato) = 2008 then (ssbelb - sskost) end) as "2008", sum(case when year(ssdato) = 2009 then (ssbelb - sskost) end) as "2009" from qqq/saltrans where year(ssdato) >= 2006 GROUP BY ssvagr ORDER BY ssvagr

Nogle tips .. Pivot (5 af 5) Use System i Navigator select ssvagr, sum(case when year(ssdato) = 2006 then (ssbelb - sskost) end) as "2006", sum(case when year(ssdato) = 2007 then (ssbelb - sskost) end) as "2007", sum(case when year(ssdato) = 2008 then (ssbelb - sskost) end) as "2008", sum(case when year(ssdato) = 2009 then (ssbelb - sskost) end) as "2009" from qqq/saltrans where year(ssdato) >= 2006 GROUP BY ssvagr ORDER BY ssvagr Use System i Navigator

Hvorfor SQL ? Move databaselogic to database level Lob og xml data ikke muligt i DDS Token !!!!<<<<<<<obs Date calculation Cascading (..on delete) Pivot Cross platform impact… Use STRQM i stedet for WRKQRY !!! …

Some tips .. SQL i CL C/free Exec sql Select …. INTO :aaaaa FROM where :abcde = inputknr; (e løsning hos DFS) /end-free

Mere info.. Links: http://en.wikipedia.org/wiki/SQL#Language_elements http://en.wikipedia.org/wiki/Pivot_table http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=/sqlp/rbafyorderop.htm http://en.wikipedia.org/wiki/SQL http://www-03.ibm.com/systems/i/software/navigator/perftips.html http://itknowledgeexchange.techtarget.com/itanswers/documenting-column-in-sql-table-in-qadbifld-as400/

SLUT dan@adfo.dk www.adfo.dk