Database Keys, Constraints, Views, and Transactions: SQL Fundamentals

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

24 Terms

1
New cards

Primary keys (PK)

PK should be: unique, NOT NULL, stable (doesn't change), narrow (small storage).

2
New cards

Surrogate key

Surrogate key = system-assigned ID with no business meaning (e.g., UserID INT IDENTITY, GUID).

3
New cards

Composite PK example

(StudentID, CourseID) for an Enrollments table (each student can enroll in a course once).

4
New cards

Foreign keys (FK)

FKs connect child → parent.

5
New cards

Good patterns for FKs

Chapters.BookID → Books.BookID, Loans.MemberID → Members.MemberID, Sessions.InstructorID → Instructors.InstructorID.

6
New cards

1NF

1NF: each column is atomic (e.g., store FirstName and LastName, not a single FullName blob).

7
New cards

2NF

2NF: no partial dependency on part of a composite key.

8
New cards

3NF

3NF: no transitive dependency (non-key → non-key).

9
New cards

UNIQUE constraint example

-- UNIQUE username in an app ALTER TABLE Accounts ADD CONSTRAINT UQ_Accounts_Username UNIQUE (Username);

10
New cards

CHECK constraint example

-- Quantity must be non-negative ALTER TABLE Inventory ADD CONSTRAINT CK_Inventory_Quantity CHECK (Quantity >= 0);

11
New cards

Data type tip

email-like strings up to 254 → VARCHAR(254) (saves space vs CHAR, and TEXT is legacy).

12
New cards

Views

A view stores a query definition, not a permanent copy of rows.

13
New cards

Updatability of views

Simple single-table views that keep the PK are often updatable; views with DISTINCT, aggregates, GROUP BY, etc., are typically not.

14
New cards

Simple projection view example

CREATE VIEW dbo.vBooks AS SELECT BookID, Title FROM dbo.Books;

15
New cards

Drop a view

DROP VIEW dbo.vReadingList;

16
New cards

Stored Procedures

Encapsulation of business logic, central permissions (e.g., GRANT EXECUTE), reusable across apps; can return multiple result sets.

17
New cards

Stored Procedure naming

Use schema + verb-noun style: dbo.uspIssueLoan. Avoid sp_ prefix.

18
New cards

Valid parameters example

@MemberID INT, @MinPages INT, @Email NVARCHAR(100).

19
New cards

Calling a stored procedure

EXEC dbo.uspFindMember 42, 'ACTIVE'; -- positional EXEC dbo.uspFindMember @Status = 'ACTIVE', @MemberID = 42; -- named.

20
New cards

Create Procedure example

CREATE PROCEDURE dbo.uspBooksByMinPages @MinPages INT AS BEGIN SELECT BookID, Title, PageCount FROM dbo.Books WHERE PageCount >= @MinPages; END;

21
New cards

Transactions

BEGIN TRANSACTION; ... INSERT/UPDATE/DELETE work here ... COMMIT; -- make permanent or ROLLBACK; -- undo since BEGIN TRANSACTION.

22
New cards

ACID properties

Atomicity: all or nothing. Consistency: constraints remain true before/after. Isolation: concurrent work doesn't see your in-flight changes. Durability: committed data survives crashes.

23
New cards

ERD notation

Fork (crow's foot): "many", Single bar: "exactly one", Open circle: "optional".

24
New cards

Transaction sandbox example

BEGIN TRANSACTION; UPDATE Inventory SET Quantity = Quantity - 1 WHERE ItemID = 9001; -- Validate results here... ROLLBACK; -- or COMMIT when sure.