Full Study Flashcards
What are the three main Objects in a Database, with a explanation of what they are?
Entities – Major object that holds significant value to the organization.
Attributes – Characteristics that gives additional information.
Relationships - logical linkage between two entities that describes how those entities are associated with each other.
What are the two types of Attributes?
Identifiers - Used to specify a unique characteristic of the entity. This will be used to identify the entity.
Descriptor - Merely used to provide more information on the entity. This does not uniquely identify the entity.
What is Cardinality?
cardinality usually represents the relationship between the data in two different tables by highlighting how many times a specific entity occurs in comparison to another.
What are the different types of Cardinality?
One-to-One - A single instance of one entity is associated with a single instance of another entity.
One-to-Many - An instance of an entity (called the parent) is associated with zero or more of another entity (called the child).
Many-to-Many - Many instances of one entity are associated with many instances of another entity.
What are the guidelines for a ERD?
Singular
Named after an Entity
Mixed Case
Same Naming
Name some ways to check data integrity
Entity - A way to uniquely identify every row in a table.
Domain - Refers to the data type and nullability of a column.
Reverential - Primary and foreign keys remain synchronized.
What is a non-decomposable column?
A column that can’t be broken down into more columns.
What are the advantages of using a non-decomposable column?
Easier to Update
Easier to Query
Specific Data Integrity
What are the rules for Normalization?
1 – Dependencies Between Data are identified.
2 – Redundant Data is Minimized.
3 – The data model is flexible and easier to maintain.
What are the different normal forms?
Zero Normal Form – Before we start the normalization steps, we look at our question and list all the attributes we will need in our database.
First Normal Form - An entity is in the first normal form if it contains no repeating groups.
Second Normal From - A relationship is in the second normal form (2NF) if it is in the first normal form, and all its attributes depend on the whole primary key.
Third Normal Form - A relationship is in the third normal form (3NF) if all the attributes are non - transitively dependent on the primary key.
Name different types of objects in SQL
Literal Values - A literal value is an alphanumerical, hexadecimal, or numeric constant.
Delimiters - Used to allow the use of reserved keywords as identifiers.
Comments - There are two different ways to specify a comment.
Identifiers – Identify Database Objects.
Reserved Keywords – Set of names with a reserved meaning.
Name different types of a Data Integrity?
Entity Integrity - a way of distinguishing between different entities. In other words, a way to uniquely identify every row in a table.
Domain Integrity - concerned with ensuring that column values fall within an acceptable range of values (the domain). Domain integrity also refers to the data type and nullability of a column.
Referential Integrity - refers to the requirement that primary and foreign keys remain synchronized between parent and child tables.
What can the Alter Table Statement do?
Add a column.
Remove a constraint.
Change a column data type.
Add a check constraint.
Add a foreign key.
Add a default constraint.
Remove a table or column.
Add a unique constraint.
Add a primary key.
What are the Advantages of Indexing?
Much quicker path to any row in a table.
Enforce entity integrity.
Improve the performance of queries that use joints.
Improve the performance of queries that use the ORDER BY clause.
What are guidelines to clustered indexing?
Only one clustered index can be created per table.
The leaf pages are the table’s data pages, and the rows are physically stored in order to the index key.
Clustered indexes are smaller than non-clustered indexes.
What are guidelines to non-clustered indexing?
Up to 249 non-clustered indexes can exist on a table.
The logical order of the rows is different than the physical order of the data.
Create a clustered index before creating any non-clustered indexes.
What are guidelines to Composite Indexing?
Up to 16 columns can be used in an index. The combined size of the columns cannot exceed 900 bytes.
The order in which the columns are used in the index is very important
When building a composite index, use the most unique column as the first column.
What are the columns that can be indexed?
Primary Keys and Foreign Keys
Columns you often search.
Cover the Query
Search for a range of values
Sort the results of a query.
Why do we use joins?
To create a single query that returns data from multiple tables.
What are the different types of Joins?
INNER JOINS
On the right is a visual representation of
an INNER JOIN between two tables. Only
the records that have a link to both tables
will be displayed.
Also known as an equi-join.
An INNER JOIN cannot be nested inside a
LEFT JOIN or RIGHT JOIN. There must
always be a relational link between tables
used in a join query.
OUTER JOINS
Outer joins differ from INNER JOINs in that
rows that do not have matches in a joined
table can be returned.
LEFT JOIN
All records from table A will be displayed
with the related records from table B. If
there are no related records, a NULL will
be displayed.
RIGHT JOIN
All records from table B will be displayed
with the related records from table A. If
there are no related record, a NULL will be
displayed.
What is a Sub-Query?
A simple example of a sub-query would be to return all the titles of books that have a price less than that of the average price of all the books.
What is a Common Table Expression?
The common table expression (CTE) is a powerful construct in SQL that helps simplify a query. CTEs work as virtual tables (with records and columns), created during the execution of a query, used by the query, and eliminated after query execution. CTEs often act as a bridge to transform the data in source tables to the format expected by the query.
What is the Syntax for a Common Table Expression?
WITH ghostTableName
(Col1, Col2)
AS
(
SELECT Col1ID, SUM(Col2)
FROM Table
GROUP BYCol1ID
)
SELECT Columns
SELECT Col1ID, SUM(Col2)
FROM Table
INNER JOIN ghostTableName
GROUP BY Col1ID
What are scripts and batch?
A query is a single SQL DML statement.
A batch is a collection of one or more T-SQL statements.
The SQL script file and Query analyser window can contain multiple batches.
If there are multiple batches, then the batch separator keyword terminates each batch.
How do you declare a Variable in SQL?
DECLARE @variable_Name DATATYPE,
@variable_Name DATATYPE,
@variable_Name DATATYPE
What is the syntax for assigning a variable?
Using the SET keyword:
DECLARE @Name VARCHAR(30),
@Surname VARCHART(30)
SET @Name = ‘John’
SET @Surname = (SELECT LastName FROM
Employee WHERE EmpID = 123
Using SELECT
DECLARE @Name VARCHAR(30),
@Surname VARCHART(30)
SELECT @Name = FirstName, @Surname
= LastName
FROM Employee WHERE EmpID = 123
The Begin and End Keyword?
The begin and end keyword are used to define the begin and end of a certain set of demand
The primary purpose of statement blocks is to define the group of statements that are affected by other control-of-flow statements such as IF, WHILE, and CASE. Each of these control-of-flow statements operates with only a single statement or a statement block.
What does the GOTO command do?
The GOTO command causes execution of a batch to immediately resume at a label. A label is merely an identifier that is followed by a colon to identify it as an independent entity. A label can be placed anywhere in your script as a commenting feature, regardless of whether a GOTO refers to it or not.
What does the return command do?
The RETURN command enables you to immediately stop execution of a batch. When the RETURN command is executed, the script execution resumes at the next batch in the script. When used in a script, the RETURN command takes no parameters. It is executed as an independent statement.
What does the if statement do?
If a Boolean expression, which follows the keyword IF, evaluates to true, then IF executes one Transact-SQL statement (or more, enclosed in a block). The optional ELSE statement can also contain a statement or block. It will be executed if the Boolean expression is false.
What is the syntax for a IF statement?
IF ‘Boolean Expression’
T/SQL statement/Block
ELSE
T/SQL statement/Block
What does the while statement do?
The WHILE statement repeatedly executes one Transact-SQL statement (or more, enclosed in a block) while the Boolean expression evaluates to true. In other words, if the expression is true, the statement (or block) is executed, and then the expression is evaluated again to determine if the statement (or block) should be executed again. This process repeats until the expression evaluates to false.
What does the BREAK statement do?
When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
What does the CONTINUE statement do?
We use this Continue statement inside the While loop. If the execution finds the SQL continue statement inside the While loop, it will stop executing the current loop iteration and starts the new iteration from the beginning.
What does the CASE statement do?
CASE is the extension of IF…ELSE statement. Unlike IF…ELSE, where only the maximum of one condition is allowed, CASE allows the user to apply multiple conditions to perform different sets of actions.
What are the different types of Transaction Modes?
Auto commit Transactions - Each individual statement is a transaction.
Explicit Transaction - Each transaction is explicitly started with the BEGIN TRANSACTION statement and explicitly ended with a COMMIT or ROLLBACK statement.
Implicit Transactions - A new transaction is implicitly started when the prior transaction completes.
What is a transaction mode?
Transactions group a set of tasks into a single execution unit. Each transaction begins with a specific task and ends when all the tasks in the group successfully complete. If any of the tasks fail, the transaction fails. Therefore, a transaction has only two results: success or failure.
What are the Benefits of using a TRY…CATCH?
Exceptions provide a clean way to check for errors without cluttering the code.
Exceptions provide a mechanism to signal errors directly rather than using some side effects.
Exceptions can be seen by the programmer and checked during the compilations process.
What are the main points of using a Stored Procedure?
Pre-Compiled Code: They are precompiled and therefore can run with less overhead than would be experienced by submitting the logic in a script.
Sharing Application Logic: Common tasks can be shared between any number of different applications.
Reliability: The approach of using many procedures to accomplish one specific task also enables you to evaluate the effectiveness of every piece.
Security: The use of stored procedures provides security control above and beyond the use of the GRANT and REVOKE statements.
What are commands that you can do with procedures?
Creating A stored Procedure
Changing a Procedure
Deleting a Procedure
Providing a stored procedure with context
Running the Procedure
Specifying Parameters and their values
What are the benefits of using Views?
Security - You can restrict users to access directly to a table and allow them to access a subset of data via views.
Simplicity - You can simplify the complex queries with joins and conditions using a set of views.
Consistency - Hide the complex queries logic and calculations in views.
What are some things that you can do with a view?
Creating a View
Using a view
Altering a view
Removing a view
Inserting into a view.
Updating a View
Delete statement in a view.
What is a with check option?
The system tests whether the inserted value of the enter_date column evaluates to true for the condition in the WHERE clause of the SELECT statement. The attempted insert fails because the condition is not met.
What does WITH ENCRYPTION and WITH SCHEMANDING do?
WITH ENCRYPTION option encrypts the information in the system catalogue.
That way you cannot view the text used to create the object.
WITH SCHEMABINDING, binds the view (or UDF) to the database objects that it references.
Any attempt to modify the structure of the database object that the function references fail.
What are the steps in creating a View?
Declare Variables – Declare the variables to be used within the code block.
Set/Select Statement – Initialize the variables to a specific value.
Declare Cursor - Populate the cursor with values that will be evaluated.
Open Statement - Open the cursor to begin data processing.
Fetch Statement - Assign the specific values from the cursor to the variables.
While Statement - Condition to begin and continue data processing.
BEGIN…END statement – Start and End of the code block.
Close Statement - Releases the current data and associated locks but permits the cursor to be re-opened.
Deallocate Statement – Destroys the cursor.