Your SlideShare is downloading. ×
CIS 282 Final Review
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
710
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. CIS-282
  • 2. Database Server  Operates as a separate “process”  Need to communicate with the server using a separate application  Connection required to transmit request and response  Management Studio and SQL Server are different programs
  • 3. Managing SQL Server  Management Studio gives access to most functions/operations  Query window provides method to write/execute T- SQL statements  “T” = transact  SQL Service Manager provides way to start/stop Server
  • 4. SQL Server Databases  Two files are maintained  Data file (.mdf)  Transaction Log file (.ldf)  Files can be spread over multiple machines  Always need a primary file group
  • 5. Database Contents  Tables  User tables hold data  System tables hold data about the database  Stored Procedures, User Defined Functions  Views  Indexes
  • 6. Default Databases  Master: Logins/User roles; system configuration; names/locations of databases; SQL Server startup  Tempdb: Lasts duration of SQL Server session; temporary holding space  Model: Sets characteristics of new databases  MSDB: Holds information about jobs SQL Server Agent runs
  • 7. Security  Windows Authentication: Windows network logins used  Mixed-mode: Windows OR SQL Server logins used  Login: To access server  User: To access a database  Network security trumps all….
  • 8. Roles  Server – administrative functions  Task based:  Backup  Create database  Roles are fixed  Database/Application – groups of users with common activities  Read/write all data  Read/write select data  Read data only
  • 9. Normalization  Process for reducing duplicate and redundant data  1st Normal Form: eliminate repeating groups  2nd Normal Form: each column in a table depends on the primary key  can’t get information with only part of the key  3rd Normal Form: A column cannot be dependent on another column that isn’t the primary key
  • 10. Relationships  Types  One-to-one  One-to-many  Many-to-many  Requires a primary key on the ‘one’ side, foreign key on the ‘many’ side  Referential Integrity defines how values work across tables
  • 11. Table Definition  Fields need a name and datatype  SQL Server datatypes are different from Access and procedural programming language datatypes  Fieldnames can include spaces – but poor practice  Identity – create unique number for each row
  • 12. Constraints  Allow Null – value not required to add record  Check Constraints – test field or row values  Default value – if no data is entered into a field  Unique – values must be different for each row
  • 13. Database Diagrams  Illustrate database design  Add, modify tables, indexes, relationships  Can control view, table details
  • 14. Indexes  Help to select and find records  Serve as a “cross-reference”  Can be based on one or more fields
  • 15. Good Index Characteristics  Low maintenance columns  Includes columns used frequently in Where or Order By clauses  Don’t have to go out to table for data  Help search for ranges of records
  • 16. Action Queries  Insert: insert into tablename(fieldlist) values(valuelist)  Update: update tablename set field1=value1, field2=value2  Delete: delete from tablename [where condition]
  • 17. Select Statement  Can use an alias for fields and/or tables  Select fields  From table  Join table On field=field  Inner – both tables have records with matching values  Outer – all records from one table and any matches found in second table
  • 18. Where  Conditions to determine records returned  Multiple conditions require And … Or  Can use where clause to implement join between tables  Sub-queries allow tables to be limited by values in a second table without using a join
  • 19. Views  Pre-defined select statement  Used like a virtual table SELECT * FROM v_CurrentEmployees
  • 20. View Performance  By default views run just as a query processed from command line  Adds second step to execution:  select from view  execute view itself  Can add an index to speed execution  Specific requirements when creating an index on a view
  • 21. Declaring & Using Variables  Use Declare  Must use Set or Select to assign  Variables are local to a procedure
  • 22. System Variables  @@ identifies system variables  Value of a system variable can change  @@Rowcount changes with next select, update, insert or delete  @@Error changes with each instruction  @@Identity changes with next insert
  • 23. Decision-making  IF … ELSE  No end if  Need to use Begin/End if have more than one instruction to execute IF StartDate < EndDate Begin … End ELSE
  • 24. Simple Case Statement  CASE  Similar to SELECT CASE  Compares one value to different cases CASE Category WHEN ‘pop_comp’ THEN ‘Popular Computing’ WHEN ‘mod_cook’ THEN ‘Modern Cooking’ END
  • 25. Searched Case  No test expression  Each WHEN has a boolean test CASE WHEN Points >= 90 THEN ‘A’ WHEN Points < 90 AND Extra > 0 THEN ‘A’ END
  • 26. Errors  Errors can occur because of SQL statement  Invalid syntax, or data type  Errors can also reflect business rules  Data doesn’t match requirements
  • 27. Try/Catch  Similar to .Net languages  Need to include BEGIN/END BEGIN TRY <code> END TRY BEGIN CATCH <error handling code> END CATCH
  • 28. BATCH  Batch is a logical group of SQL statements  Run-time error will halt execution only of FURTHER steps  Can break up multiple steps using GO  Not available in all tools  GO causes editing tool to send statements to that point for execution  GO isn’t sent to SQL Server
  • 29. Stored Procedures  “Code” written in Structured Query Language  Different from a Script  Script is a text file that is run  Sproc is compiled and stored in the database
  • 30. Using Stored Procedures  Use Execute to call  Procedures generate a return value  Return value is about success of execution  Exec spName(parameters) doesn’t capture the return value  Exec @Variable = spName(parameters) captures return value
  • 31. Parameters  Can use any SQL data type or User Defined Data Types  Input: Values sent into the stored procedure  Required unless there’s a default value  Output: Values sent to the calling routine  Output parameter is different than return value!  Must be specified as output; default is an input parameter
  • 32. Writing Stored Procedures  Make modular  Use RaisError to send a message  Message can be a string or refer to a message number  User-defined messages start at 50001 and are stored on server – not in database!
  • 33. User-defined Functions  Similar to stored procedures, but  Aren’t executed (no EXEC)  Can be run “in-line” (as part of another SQL statement)  Return a value (scalar) or table, don’t have a “return value” or output parameters
  • 34. Triggers  Typically used to enforce rules, log activity, or maintain data (archive)  Similar to a stored procedure  Respond to actions on a table  Trigger is not called  Trigger is specific to a table  Table can have multiple triggers  One trigger can respond to different actions
  • 35. Triggers & Constraints  Constraints give better performance  Triggers can span tables, databases, servers; constraints are table specific  If testing a value inserted in one table depends on an entry in another table, use a trigger  If testing a value depends on an entry in the current row, a relationship, or function result, use a constraint
  • 36. Types of Triggers  FOR/AFTER execute after data has been written to tables  Constraints have already been satisfied  INSTEAD OF executes before changes are written  Trigger must complete requested action if appropriate
  • 37. Update()  Can test if a column has changed  Update(columnname) If Update(FirstName) Print ‘FirstName has changed’
  • 38. Transactions  Provides method for canceling an operation  Can restore rows, columns to original state in event of error or business logic failure  Use when changes will either be committed or discarded in entirety  Begin Tran: Identifies the start  Commit Tran: Write changes  Rollback Tran: Cancel changes
  • 39. ACID  Atomicity: All of the changes will be accepted or none of the changes will be accepted  Consistency: Data is either in its original or changed state  Isolation: If multiple transactions occur, data is never available in an intermediate state  Durability: Once finished, all changes are complete and changes can only be done by another transaction/unit of work
  • 40. Locking & Concurrency  Locking prevents other operations from changing that data  Concurrency refers to multiple actions running against database at the same time  What happens if you want to change data I’m working with?