business solutions. formatting it is sometimes necessary to alter the output of a query for the sake...

18
Business Solutions Business Solutions

Upload: stephen-cameron

Post on 13-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

Business SolutionsBusiness Solutions

Page 2: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

FormattingFormatting

It is sometimes necessary to alter the It is sometimes necessary to alter the output of a query for the sake of output of a query for the sake of readability such as in report readability such as in report generation. This can also be applied generation. This can also be applied to a view creation which users will to a view creation which users will share.share.

Page 3: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

SUBSTRINGSUBSTRING

Returns a part of a character or binary Returns a part of a character or binary string.string.

SUBSTRING (expression, start, length)SUBSTRING (expression, start, length)

SUBSTRING (‘SQL Programming’, 1, 3) = SUBSTRING (‘SQL Programming’, 1, 3) = SQLSQL

Page 4: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

CONVERTCONVERT

Changes one datatype to another.Changes one datatype to another.

CONVERT(datatype[length], expression)CONVERT(datatype[length], expression)

CONVERT(char(2), ‘SQL’) = SQCONVERT(char(2), ‘SQL’) = SQ

CONVERT(int, ‘10’) = 10CONVERT(int, ‘10’) = 10

Page 5: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

Using them together...Using them together...

Select substring(title_id, 1, 2) as alpha

convert(int, substring(title_id, 3, 4)) as numfrom titles

Page 6: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

Other dataOther data

Col_length - returns the length defined for a Col_length - returns the length defined for a specific column.specific column.

COL_LENGTH(‘table_name’, ‘col_name’)COL_LENGTH(‘table_name’, ‘col_name’)

Data_length - returns the length of the data Data_length - returns the length of the data in an expressionin an expression

DATA_LENGTH(expression)DATA_LENGTH(expression)

Page 7: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

REPLICATEREPLICATE

Replicates a given character or string Replicates a given character or string the number of times specified.the number of times specified.

REPLICATE(char, int)REPLICATE(char, int)

Page 8: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

ROUNDROUND

Rounds a number according to the Rounds a number according to the specified accuracy.specified accuracy.

ROUND (col, int)ROUND (col, int)

Page 9: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

CHARINDEXCHARINDEXReturns the index position of the Returns the index position of the

specified string.specified string.

CHARINDEX(search_str, str)CHARINDEX(search_str, str)

CHARINDEX(‘.’, ‘12.3’) = 3CHARINDEX(‘.’, ‘12.3’) = 3

Page 10: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

TransactionsTransactionsA transaction is a set of SQL statements A transaction is a set of SQL statements

that represent a unit of work or a that represent a unit of work or a procedural operation.procedural operation.

A transaction is not complete unless all off A transaction is not complete unless all off its steps are followed through.its steps are followed through.

This can be critical to maintaining data This can be critical to maintaining data integrity such as when an account must integrity such as when an account must be credited while debiting another.be credited while debiting another.

Page 11: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

Why transactions?Why transactions?Transactions are necessary for the purpose of Transactions are necessary for the purpose of

concurrency control and recoveryconcurrency control and recovery

concurrency controlconcurrency control - allowing multiple users simultaneous - allowing multiple users simultaneous accessaccess

recoveryrecovery- allowing the database system to return the - allowing the database system to return the database to a reliable state after a failure.database to a reliable state after a failure.

Page 12: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

ConcurrencyConcurrency

• Lost-update problemLost-update problem

• LockingLocking– database system puts a lock on database system puts a lock on

accessed data so it cannot be altered accessed data so it cannot be altered until lock is released.until lock is released.

Page 13: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

LockingLocking

Since many users may be trying to Since many users may be trying to access the same data simultaneously access the same data simultaneously the DBMS has a locking mechanism the DBMS has a locking mechanism which locks data which is in use.which locks data which is in use.

This provides a solution to concurrency This provides a solution to concurrency problems which would arise if locking problems which would arise if locking were not available.were not available.

Page 14: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

2 Types of Locks2 Types of LocksExclusiveExclusive

- for UPDATE, INSERT, and DELETE (write - for UPDATE, INSERT, and DELETE (write operations)operations)- no other transaction can acquire lock until - no other transaction can acquire lock until original is releasedoriginal is released

SharedShared- applied during non-update or read operations - - applied during non-update or read operations - usually SELECTusually SELECT- prevents write operations from acquiring lock- prevents write operations from acquiring lock- allows other read operations to share lock- allows other read operations to share lock

Page 15: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

RecoveryRecovery

• Allows a database to bounce back Allows a database to bounce back after a system failureafter a system failure

• must decidemust decide– what transactions are incompletewhat transactions are incomplete

– which transactions completed but were which transactions completed but were not written and must be redonenot written and must be redone

Page 16: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

User-defined TransactionsUser-defined Transactions

• Allows user to define any number of Allows user to define any number of SQL statements as a transaction SQL statements as a transaction and instruct the database to process and instruct the database to process them as one unit.them as one unit.

Page 17: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

Defining a TransactionDefining a Transaction

• A transaction starts with the keyword A transaction starts with the keyword BEGINBEGIN

BEGINBEGINSQL statementSQL statementSQL statementSQL statementSQL statementSQL statement

COMMITCOMMIT

Page 18: Business Solutions. Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This

Finishing the TransactionFinishing the Transaction

• If the transaction goes successfully If the transaction goes successfully then the COMMIT command will then the COMMIT command will commit the changes to the database.commit the changes to the database.

• However, if an error occurs the However, if an error occurs the ROLLBACK command can be used to ROLLBACK command can be used to restore the database to its state prior restore the database to its state prior to the transaction.to the transaction.