Podcast
Questions and Answers
Which database object describes any component of a database, regardless of the user who created it?
Which database object describes any component of a database, regardless of the user who created it?
- View
- Schema
- Catalog (correct)
- Table
What is the primary function of Data Definition Language (DDL) commands in SQL?
What is the primary function of Data Definition Language (DDL) commands in SQL?
- Granting or revoking user privileges.
- Managing transactions in the database.
- Creating, altering, and dropping tables and views. (correct)
- Updating, inserting, modifying, and querying data.
Which SQL command is used to remove a table and its associated data and definitions from a database?
Which SQL command is used to remove a table and its associated data and definitions from a database?
- DELETE TABLE
- ERASE TABLE
- DROP TABLE (correct)
- REMOVE TABLE
What does establishing a referential integrity constraint between two tables accomplish?
What does establishing a referential integrity constraint between two tables accomplish?
What is the effect of using ON DELETE CASCADE
when defining a foreign key constraint?
What is the effect of using ON DELETE CASCADE
when defining a foreign key constraint?
What is a 'view' in SQL?
What is a 'view' in SQL?
How do views enhance database security?
How do views enhance database security?
In SQL, what is the main purpose of using JOIN operations?
In SQL, what is the main purpose of using JOIN operations?
What is the difference between an INNER JOIN and a LEFT OUTER JOIN?
What is the difference between an INNER JOIN and a LEFT OUTER JOIN?
What is the purpose of a UNION
operator in SQL?
What is the purpose of a UNION
operator in SQL?
What is a key characteristic of using the UNION ALL
operator compared to UNION
?
What is a key characteristic of using the UNION ALL
operator compared to UNION
?
What distinguishes a subquery from a regular query in SQL?
What distinguishes a subquery from a regular query in SQL?
What is a noncorrelated subquery?
What is a noncorrelated subquery?
What is the key difference between a correlated and a non-correlated subquery?
What is the key difference between a correlated and a non-correlated subquery?
Which characteristic describes a stored procedure?
Which characteristic describes a stored procedure?
How do stored procedures improve application performance?
How do stored procedures improve application performance?
What is a disadvantage of using stored procedures?
What is a disadvantage of using stored procedures?
What SQL command is used to execute a stored procedure?
What SQL command is used to execute a stored procedure?
Which of the following is true about variables declared inside a stored procedure?
Which of the following is true about variables declared inside a stored procedure?
In the context of stored procedures, what does an OUT
parameter signify?
In the context of stored procedures, what does an OUT
parameter signify?
What is the purpose of the DETERMINISTIC
characteristic when creating a stored function?
What is the purpose of the DETERMINISTIC
characteristic when creating a stored function?
What is the key distinction between a stored procedure and a stored function?
What is the key distinction between a stored procedure and a stored function?
What is the purpose of SQL triggers?
What is the purpose of SQL triggers?
Which type of trigger is executed before an update operation is performed on a table?
Which type of trigger is executed before an update operation is performed on a table?
What is a potential disadvantage of using SQL triggers?
What is a potential disadvantage of using SQL triggers?
What is the purpose of the IF-THEN-ELSE
statement in SQL stored procedures?
What is the purpose of the IF-THEN-ELSE
statement in SQL stored procedures?
What is the function of a CASE
statement in SQL?
What is the function of a CASE
statement in SQL?
Which SQL statement allows you to execute one or more statements repeatedly?
Which SQL statement allows you to execute one or more statements repeatedly?
What is the function of the LEAVE
statement within a SQL loop?
What is the function of the LEAVE
statement within a SQL loop?
Which of the following best describes a 'catalog' in the SQL environment?
Which of the following best describes a 'catalog' in the SQL environment?
What is the purpose of Data Control Language (DCL) commands in SQL?
What is the purpose of Data Control Language (DCL) commands in SQL?
To establish a referential integrity constraint between two tables in a relational database, which of the following steps is essential?
To establish a referential integrity constraint between two tables in a relational database, which of the following steps is essential?
If an SQL view is created with criteria such that P_PRICE > 50.00
, what happens when new products with prices greater than 50.00 are added to the underlying table?
If an SQL view is created with criteria such that P_PRICE > 50.00
, what happens when new products with prices greater than 50.00 are added to the underlying table?
In SQL, under what circumstances is a self-join typically used?
In SQL, under what circumstances is a self-join typically used?
Consider an SQL query that includes a subquery to find customers who have placed orders ('Outer Query'). Which statement is true about the subquery's execution?
Consider an SQL query that includes a subquery to find customers who have placed orders ('Outer Query'). Which statement is true about the subquery's execution?
Why might creating a vast number of stored procedures lead to difficulties in database server management?
Why might creating a vast number of stored procedures lead to difficulties in database server management?
Which type of stored procedure parameter is used to pass values both into and out of the procedure?
Which type of stored procedure parameter is used to pass values both into and out of the procedure?
When creating stored functions, the DETERMINISTIC
characteristic should be used under which of the following conditions?
When creating stored functions, the DETERMINISTIC
characteristic should be used under which of the following conditions?
What is the primary benefit of using SQL Triggers in database management?
What is the primary benefit of using SQL Triggers in database management?
In SQL, what is the purpose of the WHILE
loop statement in stored procedures?
In SQL, what is the purpose of the WHILE
loop statement in stored procedures?
Flashcards
Relational DBMS (RDBMS)
Relational DBMS (RDBMS)
A data management system that implements a relational data model, where data are stored in tables.
Catalog
Catalog
It describes any object that is a part of the database, regardless of which user created that object.
Schema
Schema
A collection of related objects, including tables, views, domains, constraints, character sets, triggers, and roles.
Data Definition Language (DDL)
Data Definition Language (DDL)
Signup and view all the flashcards
Data Manipulation Language (DML)
Data Manipulation Language (DML)
Signup and view all the flashcards
Data Control Language (DCL)
Data Control Language (DCL)
Signup and view all the flashcards
CREATE TABLE
CREATE TABLE
Signup and view all the flashcards
CREATE VIEW
CREATE VIEW
Signup and view all the flashcards
DROP TABLE
DROP TABLE
Signup and view all the flashcards
ALTER TABLE
ALTER TABLE
Signup and view all the flashcards
SQL REFERENCES Clause
SQL REFERENCES Clause
Signup and view all the flashcards
ON UPDATE CASCADE
ON UPDATE CASCADE
Signup and view all the flashcards
ON UPDATE SET NULL
ON UPDATE SET NULL
Signup and view all the flashcards
ON DELETE RESTRICT
ON DELETE RESTRICT
Signup and view all the flashcards
ON DELETE CASCADE
ON DELETE CASCADE
Signup and view all the flashcards
ON DELETE SET NULL
ON DELETE SET NULL
Signup and view all the flashcards
View
View
Signup and view all the flashcards
Base Tables
Base Tables
Signup and view all the flashcards
Join
Join
Signup and view all the flashcards
INNER JOIN
INNER JOIN
Signup and view all the flashcards
LEFT (OUTER) JOIN
LEFT (OUTER) JOIN
Signup and view all the flashcards
RIGHT (OUTER) JOIN
RIGHT (OUTER) JOIN
Signup and view all the flashcards
FULL (OUTER) JOIN
FULL (OUTER) JOIN
Signup and view all the flashcards
Self Join
Self Join
Signup and view all the flashcards
UNION operator
UNION operator
Signup and view all the flashcards
UNION ALL
UNION ALL
Signup and view all the flashcards
Subquery
Subquery
Signup and view all the flashcards
Noncorrelated Subquery
Noncorrelated Subquery
Signup and view all the flashcards
Correlated Subquery
Correlated Subquery
Signup and view all the flashcards
Stored Procedure
Stored Procedure
Signup and view all the flashcards
Recursive Stored Procedure
Recursive Stored Procedure
Signup and view all the flashcards
Advantages of Stored Procedures
Advantages of Stored Procedures
Signup and view all the flashcards
Disadvantages of Stored Procedures
Disadvantages of Stored Procedures
Signup and view all the flashcards
Variables
Variables
Signup and view all the flashcards
Variable Scope
Variable Scope
Signup and view all the flashcards
Stored Procedure Parameters
Stored Procedure Parameters
Signup and view all the flashcards
IN Parameter
IN Parameter
Signup and view all the flashcards
OUT Parameter
OUT Parameter
Signup and view all the flashcards
INOUT Parameter
INOUT Parameter
Signup and view all the flashcards
Stored Function
Stored Function
Signup and view all the flashcards
Study Notes
Introduction to Advanced SQL
- The importance of SQL in database architecture is less obvious to application users due to relational DBMSs and application generators.
- Many database application users lack direct SQL knowledge.
- SQL-based relational database applications consist of a user interface, database tables, and an SQL-capable Relational Database Management System (RDBMS).
- SQL is used within the RDBMS to create tables, translate user requests, maintain the data dictionary and system catalog, update tables, ensure security, and perform backups/recovery.
- A relational DBMS implements a relational data model, stores data in tables, and represents relationships via common values.
SQL Environment and Commands
- The SQL environment aligns with the SQL:200n standard.
- A catalog contains each database; it describes every database object, regardless of the creator.
- Companies maintain multiple database versions; PROD_C represents the production version which captures real business data.
- A schema is a collection of related objects like tables, views, domains, constraints, character sets, triggers, and roles.
- An information schema within each catalog contains descriptions of all schemas, tables, views, attributes, privileges, constraints and domains.
- The DBMS maintains the catalog based on SQL commands; users can browse catalog contents by using SQL SELECT statements.
- Data Definition Language, Data Manipulation Language and Data Control Language are the SQL commands classifications.
- DDL commands create, alter, and drop tables, views, and indexes.
- DML commands update, insert, modify, and query database data.
- DCL commands allow a DBA to control database access by granting or revoking privileges, and managing transactions.
Defining and Generating SQL Databases
- You may not be allowed to create a database due to system storage allocation.
- Database creation privileges are reserved for the database administrator in some systems.
- Command Structure:
CREATE SCHEMA database_name; AUTHORIZATION owner_user id
- Several SQL DDL CREATE commands are included in SQL:200n
DDL CREATE Command Functions:
CREATE SCHEMA
defines the portion of a database owned by a particular user.- Schemas are dependent on a catalog and contain schema objects.
- Schema objects include base tables and views, domains, constraints, assertions, character sets, and collations
CREATE TABLE
defines a new table (base or derived) and its columns.- Tables are dependent on a schema
- Derived tables are created by executing a query of tables or views.
CREATE VIEW
defines a logical table from one or more tables or views.- Views cannot be indexed.
- Updated views transfer changes to the referenced base tables.
- DDL CREATE commands can be reversed by using a DROP command.
- The
DROP TABLE tablename
command destroys a table and its definition, contents, constraints, views, and indexes. - ALTER TABLE adds, drops, or changes a column in an existing base table.
Creating Tables and Data Integrity Controls
- Once the data model is designed and normalized, the SQL CREATE TABLE command defines the columns for each table.
- Syntax:
CREATE TABLE Customer_T (CustomerlD NUMBER(11,0) NOT NULL, CustomerName VARCHAR2(25) NOT NULL, CustomerAddress VARCHAR 2(30), CustomerCity VARCHAR 2(20), Customer State CHAR (2), CustomerPostal Code VARCHAR 2(9), CONSTRAINT Customer_PK PRIMARY KEY (CustomerID));
" - Syntax establishes foreign keys to establish referential integrity between two tables with a 1:M relationship.
- Referential integrity means that a value in the matching column on the many side must correspond to a value in the primary key, or be NULL.
- The SQL REFERENCES clause prevents a foreign key value from being added if it's not a valid value in the referenced primary key.
ON UPDATE CASCADE
passes changes through to child tables.ON UPDATE SET NULL
changes the CustomerID value in the Order_T table to NULL if the Customer_T table is updated.ON DELETE RESTRICT
prevents customer record deletion if there are orders from that customer.With DELETE CASCADE,
removing a customer removes all associated order records.ON DELETE SET NULL
sets the order records for that customer to null.ON DELETE SET DEFAULT
sets the order records for that customer to a default value.- Update and delete permissions may be revoked if the SQL RDBMS does not provide for primary key referential integrity.
Views
- A view is a virtual table based on a SELECT query.
- The query may extract columns, computed columns, aliases, and aggregate functions from one or more base tables.
- The CREATE VIEW command creates a view.
- Syntax:
CREATE VIEW viewname AS SELECT query
- The CREATE VIEW statement is a data definition command that stores the subquery specification in the data dictionary.
- Views can be used wherever a table name is expected in a SQL statement.
- They are dynamically updated (re-created on demand).
- Views supply a certain level of security in the database by restricting the users to specified columns and rows.
- Views may be used as the basis for reports.
Joining and Combining Database Tables
- Joining tables on common attributes is a key aspect of relational databases.
- A join is performed to retrieve data from more than one table at a time.
SQL JOIN Types:
(INNER) JOIN
: Returns records with matching values in both tables.LEFT (OUTER) JOIN
: Returns all records from the left table and matched records from the right table.RIGHT (OUTER) JOIN
: Returns all records from the right table and the matched records from the left table.FULL (OUTER) JOIN
: Returns all records when there is a match in either the left or right table.- The inner join keyword selects records with matching values in both tables.
- The left join keyword returns all records from the left table and the matched records from the right table; if there is no match, the result from the right side is NULL.
- The right join keyword returns all records from the right table and the matched records from the left table; if there is no match, the result from the left side is NULL.
- The full outer join keyword returns all records when there is a match in either the left or right table.
- a self JOIN is where the table is joined with itself.
- The UNION operator combines the result-set of two or more SELECT statements. Each SELECT statement within UNION must have the same number of columns with similar data types in the same order.
- UNION only selects distinct values by default; use UNION ALL to allow duplicate values.
Subqueries
- Joins are used to get information from two or more tables.
- Example:
SELECT INV_NUMBER, INVOICE.CUS_CODE, CUS_LNAME, CUS_FNAME FROM CUSTOMER, INVOICE WHERE CUSTOMER.CUS_CODE = INVOICE.CUS_CODE;
- Subqueries can process data based on other data.
- Example: To generate a list of vendors who provide products, use
SELECT V_CODE, V_NAME FROM VENDOR WHERE V_CODE NOT IN (SELECT V_CODE FROM PRODUCT);
- A subquery is a query (SELECT statement) inside a query, normally expressed inside parentheses
- The first query in the SQL statement is the outer query; the query inside is the inner query.
- The inner query executes first, is used as input for the outer query.
- The entire SQL statement is sometimes called a nested query.
- Subqueries can be noncorrelated (executed once for the entire outer query) or correlated (executed once for each row returned by the outer query).
- Noncorrelated Subqueries do not depend on data from the outer query and are executed once for the entire outer query.
- Correlated Subqueries makes use of data from the outer query, executes once for each row of the outer query, and can use the EXISTS operator.
Stored Procedures
- A stored procedure is a segment of declarative SQL code stored in the database catalog.
- It is invoked by programs, triggers, or other stored procedures.
- Stored procedures calling themselves are recursive.
- A stored procedure or function is stored within the database and compiled when used.
- Stored procedures can receive input parameters and can return results.
- Stored procedures can be called from standard and scripting languages or the SQL command prompt.
- Stored procedures increase application performance, reduce traffic between application and database server, can be resued, and are secure.
- Potential downsides includes high database server load in both memory and processors, only supporting declarative SQL, being difficult to debug, and possibly complex to write and maintain
- Basic Syntax:
DELIMITER // CREATE PROCEDURE name_of_proc() BEGIN <SQL Command> DELIMITER ;
- Stored procedures contains variables to store immediate results.
- Syntax:
DECLARE variable_name datatype(size) DEFAULT default_value;
- Variables can be declared together if they have the same data type.
- The SET statement can assign other values to a variable.
- Besides SET, the SELECT INTO statement can assign a query result to a variable.
- Syntax:
DECLARE total_products INT DEFAULT 0; SELECT COUNT(*) INTO total_products FROM products ;
Stored Procedures Parameters and Scope
- A variable declared inside a stored procedure is out of scope when its END is reached.
- A variable inside a block BEGIN/END is out of scope when its END is reached.
- The variable is only effective in its scope if you can declare two variables with the same name in different scopes
- A variable with the ‘@' prefix is a session variable that exists until the session ends.
- Stored procedures that require parameters are more flexible and useful.
MySQL Parameters:
- IN is the default mode, it receives parameters as input like an attribute.
- The calling program must pass an argument to the stored procedure
- This parameter's value is always protected.
- OUT is used to pass a parameter as output and its value can be changed inside the stored procedure.
- The new value is passed back to the calling program.
- A procedure cannot access the OUT parameter's initial value when it starts.
- INOUT combines IN and OUT parameters.
- The calling program passes the argument; the procedure can modify it and pass the new value back.
Dropping, Functions and the Function Syntax
DROP PROCEDURE <name of proc>
deletes a procedure.- A function can be called from inside a statement, and returns a scalar value.
- A stored function is special as it returns a single value.
- Stored functions encapsulate common formulas or business rules that are reusable among SQL statements or stored programs.
- Stored functions can be used in SQL statements wherever an expression is valid.
- Syntax:
DELIMITER $$ CREATE FUNCTION function_name( param1, param2,...) RETURNS datatype [NOT] DETERMINISTIC
- First, specify the name with the CREATE FUNCTION keywords.
- List the parameters inside parentheses after the function name (all parameters are IN by default).
- The RETURNS statement specifies the data type of the return value.
- Determine if the function is deterministic, use the DETERMINISTIC keyword.
- The body of the function goes in the BEGIN END block, inside which needs a RETURN statement.
- Deterministic functions always return the same result for the same inputs, deterministic functions may return different results.
Calling a Stored Function and Deterministic Functions
- You pass parameters by listing them inside the parenthesis with a select statement
- Deterministic functions return the same result any time they are called with a specific set of input values
- ABS, ACOS, ASIN, ATAN, ATN2, CEILING, COALESCE, COS, COT, DATALENGTH, DATEADD, DATEDIFF, DAY, DEGREES, EXP, FLOOR, ISNULL, ISNUMERIC, LOG, LOG10, MONTH, NULLIF, POWER, RADIANS, ROUND, SIGN, SIN, SQUARE, SQRT, TAN, YEAR are deterministic functions
- Nondeterministic functions return different results each time they are called even if their inputs are the same values.
- @@CONNECTIONS, @@CPU_BUSY, @@DBTS, @@IDLE, @@IO_BUSY, @@MAX_CONNECTIONS, @@PACK_RECEIVED, @@PACK_SENT, @@PACKET_ERRORS, @@TIMETICKS, @@TOTAL_ERRORS, @@TOTAL_READ, @@TOTAL_WRITE, CUME_DIST, CURRENT_TIMESTAMP, DENSE_RANK, FIRST_VALUE, GETDATE, GETUTCDATE, GET_TRANSMISSION_STATUS, LAG, LAST_VALUE, LEAD, MIN_ACTIVE_ROWVERSION, NEWID, NEWSEQUENTIALID, NEXT VALUE FOR, NTILE, PARSENAME, PERCENTILE_CONT, PERCENTILE_DISC, PERCENT_RANK, RAND, RANK, ROW_NUMBER, TEXTPTR are examples of Nondeterministic Functions
SQL Triggers
- SQL statements or a set of SQL statements activate when an event occurs with a database table are SQL Triggers.
- Events include INSERT, UPDATE, and DELETE.
- Triggers are special stored procedures with procedural code.
- Triggers can check integrity, catch business logic errors at the database level.
- Triggers run scheduled tasks allowing tasks before or after database changes.
- It is useful to audit data changes.
- Extended validation is only provided and cannot replace all validations by triggers.
- SQL Triggers execute invisibly from client-application which connects to the database server so it is difficult to figure out what happen underlying database layer.
- SQL Triggers run every updates made to the table therefore it adds workload to the database and cause system runs slower
Types of Triggers:
- Before Update Trigger is invoked before an update is executed.
- After Update Trigger is invoked after an update occurs.
- Before Insert Trigger is invoked before a new insert is executed.
- After Insert Trigger gets invoked after an insert is implemented.
- Before Delete Trigger is invoked before a delete statement is implemented.
- After Delete Trigger is invoked after a delete operation is implemented.
Conditional Statements in MySQL
- The IF statement has three forms:
IF - THEN, IF-THEN-ELSE, IF-THEN-ELSEIF-ELSE
- The IF-THEN statement executes SQL statements based on a condition.
- The IF-THEN-ELSE statement executes other statements when the IF branch's condition is FALSE.
- The IF-THEN-ELSEIF-ELSE statement executes statements conditionally based on multiple conditions.
The CASE Statement
- An alternative conditional statement to IF, the CASE statement helps for in stored procedures.
- CASE statements result in code that is efficient and readable.
- CASE statements have two forms which are Simple CASE and searched CASE statements.
Looping in MySQL
- The LOOP statement executes one or more statements repeatedly.
- The WHILE loop executes a block of code repeatedly as long as a condition is true.
- The REPEAT statement executes one or more statements until a search condition is true.
- The LEAVE statement exits a labelled flow control.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.