Introduction To SQL
Introduction To SQL
Introduction To SQL
COURSE OBJECTIVES
At the end of the training, the participants should be able to Understand structured query language Understand database management system Understand oracle SQL Extract and analyze data with SQL Interrogate data using SQL Audit and query the database Use real online monitoring of database.
TABLE OF CONTENTS
Introduction SQL The Query Language statements The Data Manipulation Language (DML) Statements. The Data Control Language (DCL) statements.
Introduction - SQL
What is SQL? SQL is a database special-purpose language used to: Retrieve data from a database e.g. Select statement. Manipulate data that is stored in a database e.g. Insert, update, delete statements. Define objects that will be stored in the database e.g. create table, create view statements. Control access to data in a database e.g. create user, grant <privileges> statements.
CONTD
Views- filters the data available in a table so that the user only sees what they need to see or what they are allowed to see. An index is a database structure designed to reduce the amount of time necessary to retrieve one or more rows from a table. Data Dictionary: Contains information about the entire database. Data type A type of allowed data. Every table column has an associated data type that restricts (or allows) specific data in that column
Example 2:
Select empno, ename, job from emp where job = Auditor order by empno desc;
Function
AVG(Column_name) COUNT(*), COUNT(Column_name) MAX(Column_name)
Description
Returns a column's average value Returns the number of rows in a table. Returns the number of rows in a column ignoring null values. Returns a column's highest value
MIN(Column_name)
SUM(Column_name)
Using Joins
A join query extracts information from two or more tables or views. A join query differs from a regular query in at least the following two ways: The FROM clause of a join query refers to two or more tables or views. A condition is specified in the join query (known as join condition) that relates the rows of one table to the rows of another table. The following example illustrates a simple join query: SELECT EMPNO, JOB, DEPTNO FROM EMP DEPT
INSERT CONTD
You can also use this syntax Insert into DEPT values ( 59, NULL, JOS) Then a row will be inserted as you view with Select * from dept;
A user can be created for a database with the following syntax: CREATE USER userid IDENTIFIED BY password; eg create user bayo identified by bayo; A users password can be altered for a database with the following syntax: ALTER USER userid IDENTIFIED BY NEW password; eg Alter user bayo identified by mark; A user can be dropped from a database with the following syntax: DROP USER userid cascade; The cascade keyword is used to ensure that all the objects already in the users schema are dropped as well. Without this only the user will be dropped.
Privileges.
Privileges
The right to perform a specific action in the database, granted by the DBA or other database users. Privileges are rights to execute specific SQL statements. The DBA grants privileges to user accounts to control what users can do in the database. Not all relational databases support the use of privileges e.g. MS Access. However Oracle supports two kinds of privileges : system privileges and object privileges.
System Privileges (Oracle) They allow users to perform a specific action on one or more database objects or users in the database. There are more than 160 system privileges available in the Oracle 10g database. Typically, system privileges will fall into two general categories: DBA privileges and user privileges. There is no distinction at the database level between these two types of system privileges. In general, system privileges that can affect the database as a whole are considered to be DBA privileges while the ones that can affect only a users schema are user privileges. System privileges are granted with the GRANT command, which has the following syntax: GRANT create user, audit any to mami with admin option;
System Privileges
System Privilege
CREATE USER
Description
Create a new database user
Remove a database user Create a new table in any schema Turn on or turn off database auditing
System Privilege
CREATE SESSION
Description
Establish a connection to the database Create a table in the users schema Create a stored function or procedure
CREATE TABLE
CREATE PROCEDURE
Object Privileges
Object Privileges Privileges that allow users to manipulate the contents of database objects in other users schemas. Object privileges are granted on schema objects such as tables and stored procedures. They are granted to a username in a different schema. In other words, the owner of an object in a schema has all privileges on the object and can grant privileges on the object to another user.
Object Privilege
SELECT UPDATE DELETE INSERT
Description
Read (query) access on a table Update (change) rows in a table or view Delete rows from a table or view Add rows to a table or view
INDEX
EXECUTE
View
DBA_SYS_PRIVS SESSION_PRIVS DBA_TAB_PRIVS
Description
lists system privileges granted to users and roles lists the privileges that are currently available to the user lists all grants on all objects in the database
DBA_COL_PRIVS
Security Monitoring
Processes are in place to regularly monitor security on the system.
Discuss with the DBAs their processes for monitoring key database functions and security-related events to determine if system activity is regularly monitored. Obtain from the DBAs any reports or queries that are used to monitor the system. Discuss with the DBAs the level of auditing that is performed on users actions. Review the setting for the AUDIT_TRAIL parameter in the init<SID>.ora file to determine if auditing is enabled. Review the retention policy on audit trails and logs. Discuss with the DBAs procedures for monitoring sensitive accounts and privileges. Review the output of the following query to determine if updates made by the DBAs account are monitored: SELECT * FROM SYS.DBA_STMT_AUDIT_OPTS;
Security Monitoring
Review the output of the following query to determine auditing in place for all system-level privileges: SELECT * FROM DBA_PRIV_AUDIT_OPTS; Review the output of the following query to determine if statement-level auditing is enabled: SELECT * FROM DBA_STMT_AUDIT_OPTS; Review the output of the following query to determine auditing in place for database objects: SELECT * FROM DBA_OBJ_AUDIT_OPTS; Discuss with the DBA where audit trails are stored and how that location is secured from tampering.
Security Monitoring
Discuss with the DBA the process for monitoring errors in the alert log and the process for monitoring the creation of trace files. Determine the procedures used for reviewing inactive profiles. Verify the process by reviewing the last login dates of a user list to determine if any accounts have been inactive for more than 60 days or the maximum required by corporate policy. Review the process for monitoring unexpected database start ups and shutdowns. Obtain a list of triggers in the database and discuss with the DBA how they are used to monitor the database.
Logical Security
Appropriate account and password controls in place
Discuss with the DBA procedures used to log onto the system. Ensure that DBA does not use the CONNECT INTERNAL option to connect to the database. Ensure that each DBA uses a unique account to log on and administer the database. Obtain a list of users by executing the following command: SELECT * FROM DBA_USERS; Obtain the settings for the default profile (obtain settings for customized profiles if they are used): SELECT * FROM DBA_PROFILES; Review the list of users to ensure that generic accounts are not used (e.g., test, guest or shared accounts).
Logical Security
Review the list to ensure that default accounts and passwords are not used. Verify this by attempting to log onto the database using the default accounts and passwords. Review the list of users to ensure that profiles are appropriately assigned to accounts. Discuss with the DBA the process for establishing an initial password. Ensure that generic or passwords that can be easily guessed are not used. Review the following profile settings to ensure that password controls and resource limits are in place:
Logical Security
COMPOSITE_LIMIT SESSIONS_PER_USER CPU_PER_SESSION CPU_PER_CALL LOGICAL_READS_PER_SESSION LOGICAL_READS_PER_CALL IDLE_TIME PRIVATE_SGA CONNECT_TIME FAILED_LOGIN_ATTEMPTS PASSWORD_LIFE_TIME PASSWORD_REUSE_TIME PASSWORD_REUSE_MAX PASSWORD_VERIFY_FUNCTION PASSWORD_LOCK_TIME PASSWORD_GRACE_TIME