Structured Query
Language (SQL)
1Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh
2
Rushdi Shams,
Dept of CSE,
KUET
Basics of Table
 Table is a bucket where you pour data.
 Data in a specific table is a...
3Rushdi Shams, Dept of CSE, KUET
Basics of Table
 In the table, the column goes on in a horizontal
fashion.
 ISBN, AUTHO...
4Rushdi Shams, Dept of CSE, KUET
Basics of Table
 In the table, the row goes on in a vertical fashion.
 Every row in thi...
5Rushdi Shams, Dept of CSE, KUET
Basic of Table
 Books contain relatively disorganized data
 Organize information using ...
6
Rushdi Shams,
Dept of CSE,
KUET
Relation / Table
 In relational data model, the table is also
called relation. There ar...
7
Rushdi Shams,
Dept of CSE,
KUET
Relation / Table (continued)
5. Duplicate rows are not allowed in a
relation
6. The orde...
8
Rushdi Shams,
Dept of CSE,
KUET
Terminology
 The number of rows in a table is called
Cardinality
 The number of column...
9
Rushdi Shams,
Dept of CSE,
KUET
Languages in Database
Systems
 Data Definition Language (DDL)
 Used to create and modi...
10
Rushdi Shams,
Dept of CSE,
KUET
Languages in Database
Systems
 Data Modification Language (DML)
 Used to insert, view...
11
Rushdi Shams,
Dept of CSE,
KUET
Languages in Database
Systems
 Data Control Language (DCL)
Privileges, Access Control,...
Data Definition Language (DDL)Data Definition Language (DDL)
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 12
Creating a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 13
Creating a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 14
Dropping a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 15
 DROP TABLE table_name;
 DROP TABLE cars;
 DR...
Adding a column to a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 16
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 17
Adding columns to a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 18
Modifying a single column on a
table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 19
Modifying columns on a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 20
Dropping columns on a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 21
Renaming columns on a table
Data Modification LanguageData Modification Language
(DML)(DML)
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 22
Inserting data into table
 REMEMBER- when you are inserting values that are varchar, char
or other string types (if any) ...
Inserting data into table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 24
Showing data in a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 25
Updating data in a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 26
Deleting row from a table
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 27
If you do not put WHERE clause, while u...
Data Control Language (DCL)Data Control Language (DCL)
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 28
29
Rushdi Shams,
Dept of CSE,
KUET
Types of Database Privileges
 System Privileges
Control the operations that the user ...
30
Rushdi Shams,
Dept of CSE,
KUET
Database Objects
 An Oracle database consists of multiple user
accounts
 Each user ac...
31
Rushdi Shams,
Dept of CSE,
KUET
Oracle Naming Standard
 Oracle database objects must adhere to the
Oracle Naming Stand...
32Rushdi Shams, Dept of CSE, KUET
Creating New User Accounts
 Done by DBA
 Syntax:
CREATE USER username IDENTIFIED BY
pa...
33Rushdi Shams, Dept of CSE, KUET
Trying to access the database
 As we have created a new user and assigned his
password,...
34
Rushdi Shams,
Dept of CSE,
KUET
The Answer
35Rushdi Shams, Dept of CSE, KUET
Granting System Privileges
 Done by DBA
 Syntax:
GRANT privilege1, privilege2, … TO us...
36
Rushdi Shams,
Dept of CSE,
KUET
 So, now can you log in?
 Yes, you can!
37Rushdi Shams, Dept of CSE, KUET
Example Oracle System Privileges
PrivilegePrivilege LevelLevel PurposePurpose
CREATE SES...
38
Rushdi Shams,
Dept of CSE,
KUET
Granting another privilege
39
Rushdi Shams,
Dept of CSE,
KUET
Can you create table yet?
40Rushdi Shams, Dept of CSE, KUET
The Reason
 You have granted your user to create session (log in)
and to create table. ...
41
Rushdi Shams,
Dept of CSE,
KUET
… & The Solution
42
Rushdi Shams,
Dept of CSE,
KUET
Database Roles
 Role is a database object that can be
assigned system privileges
 Rol...
43Rushdi Shams, Dept of CSE, KUET
Creating a Role
 Syntax:
CREATE ROLE role_name;
44Rushdi Shams, Dept of CSE, KUET
Granting Privilege to Role
GRANT privilege1, privilege2, …
TO role_name;
45Rushdi Shams, Dept of CSE, KUET
Assigning Role to the User
 Syntax:
GRANT role_name TO user_name;
46Rushdi Shams, Dept of CSE, KUET
Revoking System Privileges
 Syntax:
REVOKE privilege1, privilege2, …
FROM username;
47
Rushdi Shams,
Dept of CSE,
KUET
What Happens Next?
48
Rushdi Shams,
Dept of CSE,
KUET
Oracle Data Types
 Data type: specifies type of data stored in a
field
 Date, charact...
49
Rushdi Shams,
Dept of CSE,
KUET
Character Type Data Type
 VARCHAR2
 Variable-length character strings
 Maximum of 4,...
50
Rushdi Shams,
Dept of CSE,
KUET
Character Type Data Type
 CHAR
 Fixed-length character data
 Maximum size 2000 chara...
51
Rushdi Shams,
Dept of CSE,
KUET
Character Type Data Type
 NCHAR
 Used for alternate alphabets
52
Rushdi Shams,
Dept of CSE,
KUET
Number Data Type
 NUMBER
 stores values between 10-130
and 10126
 General declaratio...
53
Rushdi Shams,
Dept of CSE,
KUET
Number Data Type
 Number type (integer, fixed point, floating
point) specified by prec...
54
Rushdi Shams,
Dept of CSE,
KUET
Number Data Type
 Whole number with no digits to right of
decimal point
 Precision is...
55
Rushdi Shams,
Dept of CSE,
KUET
Fixed Point Numbers
 Contain a specific number of decimal places
 Precision is maximu...
56
Rushdi Shams,
Dept of CSE,
KUET
Floating Point Numbers
 Contain a variable number of decimal places
 Precision and sc...
57
Rushdi Shams,
Dept of CSE,
KUET
Date Data Type
 DATE
 Stores dates from 1/1/4712 BC to 12/31/4712
AD
 Stores both a ...
SELECT command
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 58
SELECT command
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 59
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 60
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 61
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 62
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 63
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 64
SELECT Command
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 65
Upcoming SlideShare
Loading in …5
×

Sql

364 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
364
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql

  1. 1. Structured Query Language (SQL) 1Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh
  2. 2. 2 Rushdi Shams, Dept of CSE, KUET Basics of Table  Table is a bucket where you pour data.  Data in a specific table is associated with all other items in that table.  In a table, there are basically 3 things- 1. Rows / Records / Tuples 2. Columns / Attributes / Fields 3. Data
  3. 3. 3Rushdi Shams, Dept of CSE, KUET Basics of Table  In the table, the column goes on in a horizontal fashion.  ISBN, AUTHOR, PUBLISHER, TITLE, GENRE, PRINTED are the column names for this table
  4. 4. 4Rushdi Shams, Dept of CSE, KUET Basics of Table  In the table, the row goes on in a vertical fashion.  Every row in this table has data for ISBN, AUTHOR, PUBLISHER, TITLE, GENRE, PRINTED
  5. 5. 5Rushdi Shams, Dept of CSE, KUET Basic of Table  Books contain relatively disorganized data  Organize information using model  Resulting in a neatly structured set of rows and columns
  6. 6. 6 Rushdi Shams, Dept of CSE, KUET Relation / Table  In relational data model, the table is also called relation. There are set of rules that are applied on the relations. You must have to know them. 1. A database contains many relations. Every relations in a database must have distinct names 2. Every column in a relation must have distinct names 3. Every entries in a column must be in the same domain 4. The ordering of columns in a relation is insignificant
  7. 7. 7 Rushdi Shams, Dept of CSE, KUET Relation / Table (continued) 5. Duplicate rows are not allowed in a relation 6. The ordering of rows is insignificant 7. Multiple values are not allowed in the cells of a relation 8. 2 rows in a relation may contain the same value for 1 columns but not in all (deviation of 5)
  8. 8. 8 Rushdi Shams, Dept of CSE, KUET Terminology  The number of rows in a table is called Cardinality  The number of columns in a table is called Degree
  9. 9. 9 Rushdi Shams, Dept of CSE, KUET Languages in Database Systems  Data Definition Language (DDL)  Used to create and modify the structure of database objects  CREATE  ALTER  DROP  DDL commands execute as soon as they are issued, and do not need to be explicitly saved
  10. 10. 10 Rushdi Shams, Dept of CSE, KUET Languages in Database Systems  Data Modification Language (DML)  Used to insert, view, and modify database data  INSERT  UPDATE  DELETE  SELECT  DML commands need to be explicitly saved or rolled back  COMMIT  ROLLBACK
  11. 11. 11 Rushdi Shams, Dept of CSE, KUET Languages in Database Systems  Data Control Language (DCL) Privileges, Access Control, Administrative Rights
  12. 12. Data Definition Language (DDL)Data Definition Language (DDL) Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 12
  13. 13. Creating a table Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 13
  14. 14. Creating a table Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 14
  15. 15. Dropping a table Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 15  DROP TABLE table_name;  DROP TABLE cars;  DROP TABLE specs;  DROP TABLE stock;
  16. 16. Adding a column to a table Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 16
  17. 17. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 17 Adding columns to a table
  18. 18. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 18 Modifying a single column on a table
  19. 19. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 19 Modifying columns on a table
  20. 20. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 20 Dropping columns on a table
  21. 21. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 21 Renaming columns on a table
  22. 22. Data Modification LanguageData Modification Language (DML)(DML) Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 22
  23. 23. Inserting data into table  REMEMBER- when you are inserting values that are varchar, char or other string types (if any) then you will have to put ‘’ around that value (e.g. ‘value_for_column_X’). Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 23
  24. 24. Inserting data into table Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 24
  25. 25. Showing data in a table Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 25
  26. 26. Updating data in a table Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 26
  27. 27. Deleting row from a table Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 27 If you do not put WHERE clause, while using it with DELETE statement then all the data in that table will be deleted. SO, BE CAREFUL!
  28. 28. Data Control Language (DCL)Data Control Language (DCL) Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 28
  29. 29. 29 Rushdi Shams, Dept of CSE, KUET Types of Database Privileges  System Privileges Control the operations that the user can perform within the database ○ Connecting to the database, creating new tables, shutting down the database, etc.  Object Privileges Granted on individual database objects Controls operations that a user can perform on a specific object (insert data, delete data, etc.) When you create an object in your user schema, you can then grant object privileges on that object to other database users
  30. 30. 30 Rushdi Shams, Dept of CSE, KUET Database Objects  An Oracle database consists of multiple user accounts  Each user account owns database objects Tables Views Stored programs
  31. 31. 31 Rushdi Shams, Dept of CSE, KUET Oracle Naming Standard  Oracle database objects must adhere to the Oracle Naming Standard 1 to 30 characters long Must begin with a character Can contain characters, numbers, and the symbols $, _ , and #
  32. 32. 32Rushdi Shams, Dept of CSE, KUET Creating New User Accounts  Done by DBA  Syntax: CREATE USER username IDENTIFIED BY password;
  33. 33. 33Rushdi Shams, Dept of CSE, KUET Trying to access the database  As we have created a new user and assigned his password, should we be able to log into the database with the username and password?
  34. 34. 34 Rushdi Shams, Dept of CSE, KUET The Answer
  35. 35. 35Rushdi Shams, Dept of CSE, KUET Granting System Privileges  Done by DBA  Syntax: GRANT privilege1, privilege2, … TO username;
  36. 36. 36 Rushdi Shams, Dept of CSE, KUET  So, now can you log in?  Yes, you can!
  37. 37. 37Rushdi Shams, Dept of CSE, KUET Example Oracle System Privileges PrivilegePrivilege LevelLevel PurposePurpose CREATE SESSIONCREATE SESSION UserUser Connecting to databaseConnecting to database CREATE TABLECREATE TABLE UserUser Creating tables in current user schemaCreating tables in current user schema UNLIMITEDUNLIMITED TABLESPACETABLESPACE UserUser Allows user to create schema objectsAllows user to create schema objects using as much space as neededusing as much space as needed CREATE USERCREATE USER DBADBA Creating new usersCreating new users GRANT ANY PRIVILEGEGRANT ANY PRIVILEGE DBADBA Granting system privileges to usersGranting system privileges to users CREATE ANY TABLECREATE ANY TABLE DBADBA Creating tables in any user schemaCreating tables in any user schema DROP ANY TABLEDROP ANY TABLE DBADBA Dropping tables in any user schemaDropping tables in any user schema
  38. 38. 38 Rushdi Shams, Dept of CSE, KUET Granting another privilege
  39. 39. 39 Rushdi Shams, Dept of CSE, KUET Can you create table yet?
  40. 40. 40Rushdi Shams, Dept of CSE, KUET The Reason  You have granted your user to create session (log in) and to create table. But, it is to create on your default SYSTEM database. Therefore, you will have to grant another privilege to your user.
  41. 41. 41 Rushdi Shams, Dept of CSE, KUET … & The Solution
  42. 42. 42 Rushdi Shams, Dept of CSE, KUET Database Roles  Role is a database object that can be assigned system privileges  Role is then assigned to a user, and the user inherits the role’s privileges  Used to easily assign groups of related privileges to users
  43. 43. 43Rushdi Shams, Dept of CSE, KUET Creating a Role  Syntax: CREATE ROLE role_name;
  44. 44. 44Rushdi Shams, Dept of CSE, KUET Granting Privilege to Role GRANT privilege1, privilege2, … TO role_name;
  45. 45. 45Rushdi Shams, Dept of CSE, KUET Assigning Role to the User  Syntax: GRANT role_name TO user_name;
  46. 46. 46Rushdi Shams, Dept of CSE, KUET Revoking System Privileges  Syntax: REVOKE privilege1, privilege2, … FROM username;
  47. 47. 47 Rushdi Shams, Dept of CSE, KUET What Happens Next?
  48. 48. 48 Rushdi Shams, Dept of CSE, KUET Oracle Data Types  Data type: specifies type of data stored in a field  Date, character, number, etc.
  49. 49. 49 Rushdi Shams, Dept of CSE, KUET Character Type Data Type  VARCHAR2  Variable-length character strings  Maximum of 4,000 characters  Must specify maximum width allowed  Example declaration: student_name VARCHAR2(30)
  50. 50. 50 Rushdi Shams, Dept of CSE, KUET Character Type Data Type  CHAR  Fixed-length character data  Maximum size 2000 characters  Must specify maximum width allowed  Example declaration: student_gender CHAR(1)
  51. 51. 51 Rushdi Shams, Dept of CSE, KUET Character Type Data Type  NCHAR  Used for alternate alphabets
  52. 52. 52 Rushdi Shams, Dept of CSE, KUET Number Data Type  NUMBER  stores values between 10-130 and 10126  General declaration format: variable_name NUMBER(precision, scale)
  53. 53. 53 Rushdi Shams, Dept of CSE, KUET Number Data Type  Number type (integer, fixed point, floating point) specified by precision and scale  Precision: total number of digits on either side of the decimal point  Scale: number of digits to right of decimal point
  54. 54. 54 Rushdi Shams, Dept of CSE, KUET Number Data Type  Whole number with no digits to right of decimal point  Precision is maximum width  Scale is omitted  Sample declaration: s_age NUMBER (2)
  55. 55. 55 Rushdi Shams, Dept of CSE, KUET Fixed Point Numbers  Contain a specific number of decimal places  Precision is maximum width  Scale is number of decimal places  Sample declaration: item_price NUMBER(5, 2)
  56. 56. 56 Rushdi Shams, Dept of CSE, KUET Floating Point Numbers  Contain a variable number of decimal places  Precision and scale are omitted  Sample declaration: s_GPA NUMBER
  57. 57. 57 Rushdi Shams, Dept of CSE, KUET Date Data Type  DATE  Stores dates from 1/1/4712 BC to 12/31/4712 AD  Stores both a date and time component  Default date format: DD-MON-YY example: 05-JUN-03  Sample declaration: s_dob DATE
  58. 58. SELECT command Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 58
  59. 59. SELECT command Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 59
  60. 60. SELECT Command Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 60
  61. 61. SELECT Command Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 61
  62. 62. SELECT Command Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 62
  63. 63. SELECT Command Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 63
  64. 64. SELECT Command Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 64
  65. 65. SELECT Command Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 65

×