This document provides an overview of SQL and database concepts. It discusses:
- The basic structure of tables, rows, columns, and data types
- The four main SQL languages: DDL, DML, TCL, and DCL and common commands like CREATE, INSERT, UPDATE, DELETE, etc.
- Database objects like tables, views, indexes, and how to query them
- Constraints like PRIMARY KEY, UNIQUE, NOT NULL and REFERENTIAL integrity
- Transactions with COMMIT, ROLLBACK, and SAVEPOINT
Data Definition Language (DDL), Data Definition Language (DDL), Data Manipulation Language (DML) , Transaction Control Language (TCL) , Data Control Language (DCL) - , SQL Constraints
Using and Creating SQL Functions with Ammar Hassan Brohi.
String Functions
Numeric Functions
String / Number Conversion Functions
Group Functions
Date and Time Functions
Date Conversion Functions
Data Definition Language (DDL), Data Definition Language (DDL), Data Manipulation Language (DML) , Transaction Control Language (TCL) , Data Control Language (DCL) - , SQL Constraints
Using and Creating SQL Functions with Ammar Hassan Brohi.
String Functions
Numeric Functions
String / Number Conversion Functions
Group Functions
Date and Time Functions
Date Conversion Functions
This tutorial will give you a quick start to SQL including examples. It covers most of the topics required for a basic understanding of SQL and to get a feel of how it works.
This presentation deals with the fundamentals of SQL, Installation and Database concepts. Presented by our team in Alphalogic Inc: https://www.alphalogicinc.com/
This tutorial will give you a quick start to SQL including examples. It covers most of the topics required for a basic understanding of SQL and to get a feel of how it works.
This presentation deals with the fundamentals of SQL, Installation and Database concepts. Presented by our team in Alphalogic Inc: https://www.alphalogicinc.com/
SQL: Unique IDs, Primary Keys and Archiving Inactive Rows Without Violating C...Marco M. Kiesewetter, MBA
We want to add a constraint to an ID column - to serve as a unique Primary Key. We also want to archive any change to the table, not by deleting or overwriting data but by keeping it, flagged as inactive. This leads to several rows with the same ID value, which the constraint tries to prevent. The constraint needs to function but only on active rows, not on archived (‘deleted’) rows. Here is how to achieve this.
After completing this lesson, you should be able to do the following:
Describe the main database objects
Create tables
Describe the datatypes that can be used when specifying column definition
Alter table definitions
Drop, rename, and truncate tables
Opendatabay - Open Data Marketplace.pptxOpendatabay
Opendatabay.com unlocks the power of data for everyone. Open Data Marketplace fosters a collaborative hub for data enthusiasts to explore, share, and contribute to a vast collection of datasets.
First ever open hub for data enthusiasts to collaborate and innovate. A platform to explore, share, and contribute to a vast collection of datasets. Through robust quality control and innovative technologies like blockchain verification, opendatabay ensures the authenticity and reliability of datasets, empowering users to make data-driven decisions with confidence. Leverage cutting-edge AI technologies to enhance the data exploration, analysis, and discovery experience.
From intelligent search and recommendations to automated data productisation and quotation, Opendatabay AI-driven features streamline the data workflow. Finding the data you need shouldn't be a complex. Opendatabay simplifies the data acquisition process with an intuitive interface and robust search tools. Effortlessly explore, discover, and access the data you need, allowing you to focus on extracting valuable insights. Opendatabay breaks new ground with a dedicated, AI-generated, synthetic datasets.
Leverage these privacy-preserving datasets for training and testing AI models without compromising sensitive information. Opendatabay prioritizes transparency by providing detailed metadata, provenance information, and usage guidelines for each dataset, ensuring users have a comprehensive understanding of the data they're working with. By leveraging a powerful combination of distributed ledger technology and rigorous third-party audits Opendatabay ensures the authenticity and reliability of every dataset. Security is at the core of Opendatabay. Marketplace implements stringent security measures, including encryption, access controls, and regular vulnerability assessments, to safeguard your data and protect your privacy.
Techniques to optimize the pagerank algorithm usually fall in two categories. One is to try reducing the work per iteration, and the other is to try reducing the number of iterations. These goals are often at odds with one another. Skipping computation on vertices which have already converged has the potential to save iteration time. Skipping in-identical vertices, with the same in-links, helps reduce duplicate computations and thus could help reduce iteration time. Road networks often have chains which can be short-circuited before pagerank computation to improve performance. Final ranks of chain nodes can be easily calculated. This could reduce both the iteration time, and the number of iterations. If a graph has no dangling nodes, pagerank of each strongly connected component can be computed in topological order. This could help reduce the iteration time, no. of iterations, and also enable multi-iteration concurrency in pagerank computation. The combination of all of the above methods is the STICD algorithm. [sticd] For dynamic graphs, unchanged components whose ranks are unaffected can be skipped altogether.
2. Table Definition
• A table is uniquely identified by its name and consists of rows that
contain the stored information,
• each row containing exactly one tuple (or record ). A table can have
one or more columns.
• A column is made up of a column name and a data type, and it
describes an attribute of the
• tuples. The structure of a table, also called relation schema, thus is
defined by its attributes.
• The type of information to be stored in a table is defined by the data
types of the attributes
• at table creation time.
3. SQL
• STRUCTURED QUERY LANGUAGE.
• Non-Procedural Language.
• Language for any RDBMS.
• We can't Execute more than one statement at a
time
• Very simple. Easily understandable.
5. DDL
• Data Definition Language.
• To Create Objects, To Alter Objects & To Drop Objects.
• Commands:
CREATE
ALTER
DROP
TRUNCATE
• DDL Statements are Auto Commit Statements.
6. DML
• Data Manipulation Language.
• To Maintain information the following Commands:
INSERT
UPDATE
DELETE
7. TCL
• Transaction Control Language.
• Transaction is Nothing But DML.
• Transaction : A Series of INSERTs, UPDATEs, DELETEs
etc.,
• Commands:
COMMIT
ROLLBACK
SAVEPOINT
8. DCL
• Data Control Language.
• To Deal with Privileges.
• Commands:
GRANT
REVOKE
9. DRL
• Data Retrieval Language.
• To retrieve data from the Database.
• Commands:
SELECT
11. CREATE (...CONTINUED)
RULES FOR NAMING OBJECT
• Should start with Alphabet.
Error Message : Invalid Table Name
• Object name should have Maximum Length of 30
characters.
Error Message : Identifier too Long
• No special symbols and spaces except '_'
Error Message : Missing / Invalid option
• Reserved Words cannot be used.
Error Message : Invalid table name
• Case insensitive.
The above rules also apply to column names
12. CREATE (...CONTINUED)
• Example : CREATE TABLE EMP
( EMPNO NUMBER(4),
ENAME VARCHAR2(15),
JOB VARCHAR2(10),
DOB DATE,
SAL NUMBER(7,2),
DEPT NUMBER(7,2) );
• On completion of creation of the Table " Table created " Message will be
displayed.
• Every SQL Statement is terminated by semicolon ';' .
• When ever SQL Statement is terminated it will be placed in " afiedt.buf ".
Later if there are no errors Transaction will be carried out. Otherwise error
message will be displayed.
13. INSERT
• DML Command.
• To insert rows in to Table.
• Syntax 1 : INSERT INTO < TABLE_NAME > VALUES (VAL1,VAL2,
...........,Val n);
Example : INSERT INTO EMP VALUES (1001,'BABU','MANAGER','20-
JAN-2004',8000,20);
To insert Multiple rows in to a Table with this Syntax we have type the
Command repeatedly.
• Syntax 2 : INSERT INTO < TABLE_NAME > VALUES (&VAL1,&VAL2,
...........,&VALn);
Example : INSERT INTO EMP VALUES
(&ENO,'&ENAME','&JOB','&HDATE',&SAL,&DEPT);
To insert Multiple rows in to a Table with this Syntax we need not type the
Command repeatedly, instead we have type / symbol repeatedly.
14. INSERT(…CONTINUED)
• Syntax 3 : INSERT INTO < TABLE_NAME >
(COL1,COL2,..........,COLn) VALUES (&VAL1,&VAL2,
...........,&VALn);
Example : INSERT INTO EMP (EMPNO,ENAME,JOB)
VALUES (&ENO,'&ENAME','&JOB');
To insert information regarding only few columns of the
table.
• In any Syntax CHAR and DATE data types must be
specified with in single Quotes.
15. SELECT
• DRL Command.
• To retrieve information from the Tables.
• Syntax : SELECT * FROM < TABLE_NAME >;
Example : SELECT * FROM EMP;
16. ALTER
• DDL Command.
• To Modify any Existing Tables.
• Syntax : ALTER TABLE < TABLE_NAME >
[ADD | MODIFY | DROP | DISABLE | ENABLE]
(COL_NAME1 DEFINITION,
COL_NAME2 DEFINITION,
........
COL_NAME n DEFINITION )
17. ALTER (...CONTINUED)
ADD
• Adding Column to Table by ADD Command.
• Example : ALTER TABLE EMP ADD (COMM
NUMBER(7,2), DOB DATE);
18. ALTER (...CONTINUED)
MODIFY
• Change Data type and Increase / Decrease width by
MODIFY Command.
• We can’t Modify the column when there is already some
information in the Column.
Error Message : Column to be Modified must be empty to
Decrease precision / scale.
Error Message : Column to be Modified must be empty to
Change Data type.
• We cannot Modify Table Name using Alter Command.
• Example : ALTER TABLE EMP MODIFY (JOB
VARCHAR2(25), SAL NUMBER(9,2));
19. ALTER (...CONTINUED)
DROP
• Drop the Column by DROP Command.
• Example : ALTER TABLE EMP DROP COLUMN
DOB;
• ORACLE 9 provides the feature of dropping Multiple
Column.
Example : ALTER TABLE EMP SET UNUSED DOB
COLUMN;
Example : ALTER TABLE EMP DROP UNUSED
COLUMNS;
Drops all columns which are set to UNUSED.
20. ALTER (...CONTINUED) ENABLE /
DISABLE
• Enable and Disable Constraints by ENABLE and
DISABLE Commands respectively.
• Example : ALTER TABLE EMP ENABLE
CONSTRAINT PK1;
• Example : ALTER TABLE EMP DISABLE
CONSTRAINT PK1;
21. DROP
• DDL Command.
• To remove any object from schema.
• Drops both Information and Structure of the Table.
• Syntax : DROP < OBJECT_TYPE > <
OBJECT_NAME >;
• Example : DROP TABLE EMP;
22. TRUNC
• DDL Command.
• Removes rows in the Table Permanently.
• Syntax : TRUNC TABLE < TABLE_NAME >;
• Example : TRUNC TABLE EMP;
• WHERE Clause cannot be used with TRUNC.
• Only information deleted, Not Structure.
• Auto-Commit Command.
• EXIT and QUIT are Implicit Commit statements.
23. UPDATE
• DML Command.
• To Modify any existing information in the table.
• Syntax : UPDATE < TABLE_NAME > SET <
COLUMN_NAME > = < VALUE > [ WHERE
<CONDITION> ];
• Example : UPDATE EMP SET SAL = 9000 WHERE
EMPNO=1002;
• To UPDATE multiple columns with in a row separate
them by Commas.
• Example: UPDATE EMP SET SAL =
9000,COMM=1000 WHERE EMPNO=1002;
24. DELETE
• DML Command.
• To Delete rows from the Table.
• Syntax : DELETE FROM < TABLE_NAME > [ WHERE
<CONDITION> ];
• If Condition not specified all rows of the table will be
deleted.
• Example : DELETE FROM EMP;
• If Condition specified all rows of the table satisfying the
Condition will be deleted.
• Example : DELETE FROM EMP WHERE JOB =
'MANAGER‘;
25. COMMIT & ROLL BACK
• TCL Commands.
• Whenever we update data it will be first placed in o buffer.
• If we COMMIT the buffer information will be stored
permanently in physical location.
• If we ROLLBACK we get previous values.
• DDL Commands are AUTO-COMMIT statements.
• If DDL Commands are used after Transactions all those
transactions will Automatically Commit.
26. SAVEPOINT
• Suppose there are 'N' transactions after COMMIT Point.
• If 'Nth' transaction is to be ROLLED BACK all previous
N-1 transactions will also get ROLLED BACK.
• To avoid this we have SAVEPOINT.
• If we have SAVEPOINT in between Transactions, If we
ROLLBACK only transactions after specified
SAVEPOINT will be ROLLED BACK.
• On using COMMIT there will be no more SAVEPOINTs.
27. SAVEPOINT (...CONTINUED)
• Example :
• UPDATE EMP SET SAL = 9000 WHERE EMPNO=1002;
SAVEPOINT A;
UPDATE EMP SET SAL = 8000 WHERE EMPNO=1002;
SAVEPOINT B;
UPDATE EMP SET SAL = 7000 WHERE EMPNO=1002;
SAVEPOINT C;
UPDATE EMP SET SAL = 6000 WHERE EMPNO=1002;
SAVEPOINT D;
• If Error occurred in Third UPDATE statement we can ROLLBACK to c
• Example : ROLLBACK TO B;
28. GRANT
• DCL Command.
• To Grant Permission to user to any Table.
• The Data Dictionary Tables related to Privileges:
• USER_TAB_PRIVS_MADE : What privileges are
given?
• USER_TAB_PRIVS_RECD : Who gave the privileges?
29. GRANT (...CONTINUED)
• To give all the privileges to user.
Syntax : GRANT ALL ON < TABLE_NAME > TO <USER_NAME
>;
ALL means INSERT, DELETE, SELECT, ALTER & INDEX.
• To give single privilege to user.
Syntax : GRANT SELECT ON < TABLE_NAME > TO
<USER_NAME >;
Read only permission Granted.
• To give privilege of GRANTing privileges to user by WITH GRANT
OPTION.
Syntax : GRANT ALL ON < TABLE_NAME > TO <USER_NAME
> WITH GRANT OPTION;
Example : GRANT ALL ON EMP TO USER1 WITH GRANT
OPTION;
30. GRANT (...CONTINUED)
• If 'X' is the Owner of a particular table 'TABLE', all users who
received privileges can access 'TABLE' by X.TABLE.
• Example : SELECT * FROM SCOTT.EMP;
SCOTT is Owner.
• OWNER: GRANT ALL ON EMP TO USER1 WITH GRANT
OPTION;
USER1 : SELECT * FROM SCOTT.EMP;
GRANT ALL ON EMP TO USER2;
USER2 : SELECT * FROM SCOTT.EMP;
31. REVOKE
• DCL Command.
• To take back the privileges GRANTED.
• Syntax : REVOKE ALL ON < TABLE_NAME >
FROM < USER_NAME >;
• Example : REVOKE ALL ON EMP FROM USER1;
• Example : REVOKE INSERT ON EMP FROM
USER1;
• CASCADING REVOKE.
32. Database Objects
Description
Basic unit of storage; composed of rows
and columns
Logically represents subsets of data from
one or more tables
Generates primary key values
Improves the performance of some queries
Alternative name for an object
Object
Table
View
Sequence
Index
Synonym
33. Querying a View
Oracle Server
USER_VIEWS
EMPVU30
SELECT EMPNO,
ENAME, SAL
FROM EMP
WHERE DEPTNO=30;
iSQL*Plus
SELECT *
FROM EMPVU30;
EMP
34. What is an Index?
An index:
• Is a schema object
• Is used by the Oracle server to speed up the retrieval of
rows by using a pointer
• Can reduce disk I/O by using a rapid path access method to
locate data quickly
• Is independent of the table it indexes
• Is used and maintained automatically by the Oracle server
35. How Are Indexes Created?
• Automatically: A unique index is created automatically when
you define a PRIMARY KEY or UNIQUE constraint in a
table definition.
• Manually: Users can create nonunique indexes on columns to
speed up access to the rows.
37. LEVELS OF CONSTRAINTS
• COLUMN LEVEL
SYNTAX: CONSTRAINT <CONSTRAINT_NAME>
<CONSTRAINT_TYPE>
• TABLE LEVEL
SYNTAX: CONSTRAINT <CONSTRAINT_NAME>
<CONSTRAINT_TYPE> (COLUMN_NAME)
Functionally no difference between Column level and
Table level constraints.
38. CONSTRAINTS(…Contd)
• NOT NULL is Column level constraint.
• To add NOT NULL constraint after table creation use
MODIFY. For others we use ADD.
• If constraint name is not given, the system will provide its
own name.
• The constraints information will be stored under
USER_CONSTRAINTS ,USER_CONS_COLUMN.
• Only one Primary Key is allowed per table.
• On a single column four types of constraints can be
enforced at a time
• Two or more check constraints can be enforced on a
column at a time.
39. COLUMN LEVEL CONSTRAINT
• CREATE TABLE EMP
(EMPNO NUMBER(4) CONSTRAINT PK1 PRIMARY KEY,
ENAME VARCHAR2(15) CONSTRAINT N1 NOTNULL,
JOB VARCHAR2(15) CONSTRAINT N2 NOTNULL,
DOJ DATE,
SAL NUMBER(7,2) CONSTRAINT C1 CHECK(SAL>2000),
COMM NUMBER(7,2),
PHONENO NUMBER(10) CONSTRAINT U1 UNIQUE,
DEPTNO NUMBER(2));
NOT NULL IS THE ONLY COLUMN LEVELCONSTRAINT.
41. CONSTRAINTS (…Contd)
• After creation of table if we want to enforce constraints
– If it is NOT NULL use MODIFY
– If other constraints use ADD
• Example:
ALTER TABLE EMP MODIFY
( ENAME VARCHAR2(15) CONSTRAINT N1 NOT NULL);
ALTER TABLE EMP ADD
(CONSTRAINT K1 PRIMARY KEY(EMPNO),
CONSTRAINT C1 CHECK(SAL>2000),
CONSTRAINT U1 UNIQUE(PHONENO));
42. COMPOSITE PRIMARY KEY
• If two or more attributes together form Primary Key then it is
called Composite Key.
• The combination of columns values should be unique.
• Composite Primary Key is always a Table level constraint. We
cannot go for column level.
• Example:
CREATE TABLE PRODUCT_DETAILS
( PRODUCT_NO NUMBER(5),
PRODUCT_NAME VARCHAR2(25),
PRODUCT_PRICE NUMBER(11,2),
CONSTRAINT PK1 PRIMARY KEY(PRODUCT_NO,
PRODUCT_NAME, PRODUCT_PRICE));
43. DROPING CONSTRAINTS
• First find constraint name from USER_CONSTRAINTS.
• SELECTCONSTRAINT_NAME FROM USER_CONSTRAINTS;
• If name is given by us we can identify it easily.
• If it is system generated we have to know Table name from
USER_CONSTRAINTS and column details from
USER_CONS_COLUMNS.
• Then DROP that constraint by constraint name.
• Example : ALTER TABLE EMP DROP PRIMARY KEY;
• Example : ALTER TABLE EMP DROP CONSTRAINT U1;
44. ENABLE & DISABLE
• ALTER TABLE EMP DISABLE CONSTRAINT C1;
• ALTER TABLE EMP ENABLE CONSTRAINT C1;
• By Default constraint will be in enabled form.
45. REFERENTIAL INTEGRITY
• To create relationships between tables and avoiding redundancy.
• Example:
CREATE TABLE DEPT
(DEPTNO NUMBER(2),
DNAME VARCHAR2(20),
LOC VARCHAR2(25),
CONSTRAINT PK2 PRIMARY KEY(DEPTNO));
46. REFERENTIAL INTEGRITY
(…Contd)
• CREATE TABLE EMP
(EMPNO NUMBER(4) CONSTRAINT PK1 PRIMARY KEY,
ENAME VARCHAR2(15) CONSTRAINT N1 NOTNULL,
JOB VARCHAR2(15) CONSTRAINT N2 NOTNULL,
DOJ DATE,
SAL NUMBER(7,2) CONSTRAINT C1 CHECK(SAL>2000),
COMM NUMBER(7,2),
DNO NUMBER(2),
CONSTRAINT R1 REFERENCES DEPT(DEPTNO));
DNO,DEPTNO SHOULD BE OF SAME DATATYPE AND
WIDTH.
47. REFERENTIAL INTEGRITY
(…Contd)
• To enforce Referential Integrity constraint after table creation
• We cannot delete the master record without deleting the corresponding
Detail records.
• ALTER TABLE EMP CONSTRAINT R1 FOREIGN KEY(DNO)
REFERENCES DEPT(DEPTNO);
• If we delete the master record the corresponding dependent records
should also be deleted.
• ALTER TABLE EMP CONSTRAINT R1 FOREIGN KEY(DNO)
REFERENCES DEPT(DEPTNO) ON DELETE CASCADE;