Session 3 - Constraints, views, triggers Flashcards

1
Q

What is the syntax for creating a view?

A

CREATE VIEW viewName as

SELECT STATEMENT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax you can use in SQL 2016 sp1 for creating a view?

A

CREATE OR ALTER VIEW viewName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can’t you have when creating a view?

A

Order By unless you use TOP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the syntax for finding out if a view exists and dropping it if it does?

A

if exists (select * from sys.views where name = ‘viewName’)
DROP VIEW viewName
GO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What query would you use to see all stored procedures and views?

A

SELECT * FROM SYS.SYSCOMMENTS
OR
SYS.SQL_MODULES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you secure the creation of a view or stored procedure?

A

Use WITH ENCRYPTION

CREATE VIEW viewName WITH ENCRYPTION AS
select statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you secure CRUD operations within View?

A

Create view viewName as SELECT STATEMENT WITH CHECK OPTION

It makes it so it secures the view from updates, deletes and modifications to within the WHERE clause in the select statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the limitations of inserting or deleting data from a view

A

It can only insert or delete from a single table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax for creating an index on a view

A

The first index needs to be clustered (basically like a primary key)

CREATE UNIQUE CLUSTERED INDEX indexName ON schema.viewName (columns to include)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are all elements that cannot be contained in a view index?

A
  • -COUNT
  • -ROWSET FUNCTIONS (OPENDATASOURCE, OPENQUERY, OPENROWSET, OPENXML)
  • -OUTER JOINS (LEFT RIGHT FULL)
  • -DERIVED TABLE
  • -STDEV, STDEVP, VAR, VARP OR AVG
  • -CTE
  • -FLOAT, TEXT, NTEXT, IMAGE, XML, OR FILESTREAM COLUMNS
  • -SUBQUERY
  • -OVER CLAUSE (INCLUDES RANKING OR AGGREGATE WINDOW FUNCTIONS)
  • -FULL-TEXT PREDICATES (CONTAINS, FREETEXT)
  • -SUM FUNCTION THAT REFERENCES A NULLABLE EXPRESSION
  • -ORDER BY
  • -CLR USER-DEFINED AGGREGATE FUNCTION
  • -TOP
  • -CUBE, ROLLUP, OR GROUPING SETS OPERATORS
  • -MIN, MAX
  • -UNION, EXCEPT, OR INTERSECT OPERATORS
  • -TABLESAMPLE
  • -TABLE VARIABLES
  • -OUTER APPLY OR CROSS APPLY
  • -PIVOT, UNPIVOT
  • -SPARSE COLUMN SETS
  • -INLINE (TVF) OR MULTI-STATEMENT TABLE-VALUED FUNCTIONS (MSTVF)
  • -OFFSET
  • -CHECKSUM_AGG
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the syntax for creating a trigger?

A
CREATE TRIGGER triggerName ON tableName
INSTEAD OF ( OR FOR/AFTER) UPDATE, DELETE, INSERT (OR 1, 2 OF THOSE)
AS
BEGIN
QUERY
END
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the variable name for finding the how nested a trigger is

A

@@NESTLEVEL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does DML stand for in DML trigger?

A

DATA MANIPULATION LANGUAGE (IE: Insert, Update, Delete)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does DDL stand for in DDL Trigger?

A

DATA DEFINITION LANGUAGE (IE: Create, Alter and Drop)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does SET NOCOUNT ON do?

A

Suppresses the (#Row(s) Affected) message from appearing when a script is done.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

WHAT IS THE STORED PROCEDURE AND SYNTAX FOR GETTING THE CONFIGURATION OF NESTED TRIGGERS?

A

EXEC SP_CONFIGURE ‘NESTED TRIGGERS’;
TO UPDATE:
EXEC SP_CONFIGURE ‘NESTED TRIGGERS’, 0;
RECONFIGURE –SETS THE RUN VALUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Within a trigger, how can you check if a column has been updated and if so run a query?

A

USE IF UPDATE(columnName)
BEGIN
QUERY
END

18
Q

How is COLUMNS_UPDATED function used?

A

EACH COLUMN IS IN ORDER AND GOES FROM 1,2,4,8,16,32,64,128 SO YOU TEST FOR THE SUM OF COLUMNS UPDATED IE:

IF COLUMNS UPDATED() & 3 = 3 –MEANING THAT COLUMNS 1 AND 2 WERE UPDATED

BEGIN
QUERY
END

19
Q

What are the types of triggers?

A

INSTEAD OF and FOR / AFTER

20
Q

What is a Foreign Key

A

A column that references a primary key or unique constraint in another table. You can only input a field if it exists in the referenced primary key or unique constraint.

21
Q

What are the two ways that SQL finds data in a table? Which is more efficient?

A

SEEK - Used when an Index exists. MUCH more efficient.

SCAN - Used when no Index exists. Has to search entire table.

22
Q

What is the syntax for creating a Foreign Key?

A

ALTER TABLE

ADD CONSTRAINT ConstraintName FOREIGN KEY (ColumnName) REFERENCES schema.TableName(ColumnName)

23
Q

How do you tell the system how to handle the Primary Key change with regard to the Foreign Key?

A

At the end of the Foreign Key creation query, you need to add:

ON [UPDATE, DELETE] [CASCADE, NO ACTION, SET NULL, SET DEFAULT]

24
Q

Can you have multiple columns in a single Foreign Key?

A

Yes, this is called a COMPOUND FOREIGN KEY

25
Q

How do you suppress the number of rows updated messages?

A

SET NOCOUNT ON

26
Q

What are the three ways to find out what number the IDENTITY is on in a table?

A

@@IDENTITY
SCOPE_IDENTITY()
IDENT_CURRENT(TableName)

27
Q

What does @@IDENTITY show you?

A

The global variable for the last value that was returned for the last Identity globally.

28
Q

What does SCOPE_IDENTITY() show you?

A

The last identity value that was returned within a scope (function, stored procedure, etc.)

29
Q

What does IDENT_CURRENT(TableName) show you?

A

The last value returned by the identity in the table specified.

30
Q

How can you order a view and return all rows?

A

SELECT TOP(100) PERCENT [COLUMNS] FROM [TABLE] ORDER BY [COLUMNS]

31
Q

What is the function used to get an object ID out of SQL?

A

OBJECT_ID(‘Object Name’)

32
Q

What is the function for getting the View, Stored Procedure or Function definition?

A

OBJECT_DEFINITION(ObjectId)

33
Q

What access does a view have?

A

All data that can be returned from the view from any of the tables AS LONG AS THE TABLES ARE IN THE SAME SCHEMA

34
Q

When creating a view that is going to have an index attached to it while also having a join, what option needs to be added?

A

WITH SCHEMABINDING:

CREATE VIEW [SCHEMA].[VIEWNAME] WITH SCHEMABINDING AS
SELECT STATEMENT

35
Q

Can you create a UNIQUE CLUSTERED INDEX on a VIEW when the view returns duplicate values?

A

NO. It is like creating a primary key, the values all have to be unique.

36
Q

Can you drop a table that has a VIEW attached to it?

A

No, you have to drop the view first, then drop the table.

37
Q

What are the two special tables created within a trigger?

A

INSERTED
DELETED

These allow you to see the data that was inserted or deleted from a table during the table modification.

38
Q

What’s the maximum nest trigger value?

A

32

39
Q

How do you set up AFTER trigger nesting?

A

EXEC SP_CONFIGURE ‘NESTED TRIGGERS’;

RECONFIGURE – NEEDED TO COMPLETE CHANGE

40
Q

What are the steps needed to be able to insert into an identity column?

A

SET IDENTITY_INSERT TableName ON;

INSERT INTO TABLE (columns) VALUES (values) –You HAVE to expressly label the columns that you are going to insert into for an identity column even with identity_insert ON