7. System Database It is the key working area for your server tempdb is where the SQL Agent process stores any system tasks Msdb This database forms a template for any new database that you create. model This database holds a special set of tables (system tables) that keeps track of the system as a whole master Description Database
8. User Database This is a companion sample database that can be installed with Microsoft SQL Server 2005. The database schema was designed to showcase SQL Server 2005 features. Adventure works This database, like pubs, is a training database. Northwind This database provides a consistent place for training and experimentation pubs Description Database
9.
10.
11.
12.
13.
14.
15.
16.
17. Data Types Accepts an argument that determines size and precision Varies Approx. Numeric Float Monetary units from -214,748.3648 to +214,748.3647 4 Money SmallMoney Monetary units from -263 to 263 plus precision to 4 decimal places. 8 Money Money Fixed Precision and Scale from -10 38 -1 to 10 38 -1. Varies Decimal/Numeric Decimal or Numeric 0 to 255 1 Integer TinyInt Whole Numbers (Small Range) 2 Integer SmallInt Whole Numbers 4 Integer Int Range is from -2 63 to 2 63 -1 8 Integer Bigint For storing binary values 1 Integer Bit Description Size in Bytes Class Data Type Name
Faculty Notes : Database Engine is the core service for storing, processing and securing data. It maintains the relationships between the stored data. It helps in the translation the data to information. It provides controlled access- While logging into SQL Server Management studio, we can select the server type as database engine. It facilitates rapid transaction processing to meet the requirements of the most demanding data consuming applications within the enterprise. The Database Engine provides rich support for sustaining high availability. In Microsoft SQL Server 2005, a Database Engine information worker is a domain expert who understands the relationships between the data stored in a database and is able to translate the data into business information. Information workers rarely interact directly with primary online transaction processing (OLTP) databases, instead they use the applications that are associated with the database. When information workers interact directly with databases it is typically to run ad-hoc queries or reports against secondary reporting databases, or to work with smaller databases that are distributed with workgroup or personal applications. A database in Microsoft SQL Server 2005 is made up of a collection of tables that contain data and other objects that are defined to support the activities performed with the data. These objects include views, indexes, stored procedures, and triggers. Before you can create objects within the database, you must create the database and also understand how to change the settings and the configuration of the database. This includes tasks such as expanding or shrinking the database, or specifying the files that are used to create the database.
Database : The database holds all facts about all the entities. Database allows user for storing and maintaining the data. Transaction Log : Transactions logs are an important part of protecting your data .By keeping track of operations in a log, the database server makes it possible to recover from a wide range of disaster. The two types of Database in SQL Server are : System Databases : System Database are the database used to store the information about SQL Server itself and used for its operations. User Databases : Users Databases are the database created and maintained by the user.
Faculty Notes : Tables - Tables are the objects in the database that actually store the data. Because all other objects in the database depend on their existence, tables can be considered the building blocks of the database. Views - View is an object of a database which contains the query. View when selected it executes the query and extracts the data from the base table. Indexes - Indexes improves the database performance. Indexes help to locate a row in a table directly in a data page instead of going through all the data pages. Stored Procedure - Stored Procedure is a name given to set of T-SQL statements. It includes control structures, looping add return values and so on. User Defined Functions - A function is a grouping of Transact SQL statements that can be reused. SQL server has a large number of built in functions, but these may not meet User needs. For this reason SQL Server gives an ability to create own function called user defined function to perform any task. Rules - Rules provide another means of enforcing domain and user defined integrity rules within user database. Defaults - Defaults are used to fill in the data in the columns if the user does not supply a value. User Defined Data Types - User Defined data Types are based on the system data types. They can be used when several tables must store the same type of data in a column and user must ensure that these columns have exactly the same data type, length, and null ability.
pubs This database provides a consistent place for training and experimentation Northwind This database, like pubs, is a training database
is where the SQL Agent process stores any system tasks
Each database consists of at least two files: one is a primary data file (by default, with the .mdf extension), the other is a log file (by default, with the .ldf extension). There are also optional secondary data files (by default, with the .ndf extension). A database can have only one primary data file, zero or more secondary data files, and one or more log files. Each database file can be used by only one database. When SQL Server is functioning and operating, the database engine keeps track of almost every change that takes place within the database by making entries into the transaction log(.ldf) so that it can be used later if needed.
Faculty Notes: Automicity: Automicity states that either all the data modifications are performed aor none of them are performed. Consistency : Consistency is a state in which all the data is in a consistent state after transaction is completed successfully All the rules in a relational database must be applied to the transaction modifications to maintain complete data integrity Isolation : If two transaction are in progress at once for example two users at different computers might be modifying the same table the transaction is isolated from the other. Durability : Durability states that any changes in data by a completed transaction remains permanently in effect in system Hence ,any change in data due to a completed transaction persists even in the event of a system failure This is ensured by the concept of backing up and restoring transaction logs.
Faculty Notes: Review the points on this slide.
Faculty Notes :
Faculty Notes: Table is an object in the database. A table is collection of rows and columns associated with individual data item. For the above syntax : table_name - The name of the new table. Table names must comply with the rules for identifiers. The table_name must be unique within the database. A table_name can contain a maximum of 128 characters. column_name - The name of a column in the table. Column names must comply with the rules for identifiers and must be unique in the table. data_type - Specifies the column data type. DEFAULT- Specifies the value provided for the column when a value is not explicitly supplied during an insert action. DEFAULT definitions can be applied to any column, except those defined by the IDENTITY property. DEFAULT definitions are removed when the table is dropped. A constant value can be used as a default. IDENTITY - Indicates that the new column is an identity column. When a new row is added to the table, SQL Server Compact Edition provides a unique, incremental value for the column. Identity columns are generally used in conjunction with PRIMARY KEY constraints to serve as the unique row identifier for the table. The IDENTITY property can be assigned only to int columns. Only one identity column can be created per table. Bound defaults and DEFAULT constraints cannot be used with an identity column. You must specify both the seed and increment or neither. If neither is specified, the default is (1,1). seed - The value used for the first row that is loaded into the table. increment - The incremental value added to the identity value of the previous row that is loaded. ROWGUIDCOL - Indicates that the new column is a row global unique identifier column. Only one uniqueidentifier column per table can be designated as the ROWGUIDCOL column. The ROWGUIDCOL property can be assigned only to a uniqueidentifier column. ROWGUIDCOL automatically generates values for new rows inserted into the table. CONSTRAINT - An optional keyword indicating the beginning of a PRIMARY KEY , UNIQUE , or FOREIGN KEY constraint definition. Constraints are special properties that enforce data integrity and create special types of indexes for the table and its columns. constraint_name - The name of a constraint. The constraint_name is optional and must be unique within a database. If a constraint_name is not specified, SQL Server Compact Edition generates a constraint name. NULL | NOT NULL - Keywords that specify whether null values are permitted in the column. NULL is not strictly a constraint but can be specified in the same manner as NOT NULL . PRIMARY KEY - A constraint that enforces entity integrity for a particular column or columns using a unique index. Only one PRIMARY KEY constraint can be created per table. UNIQUE - A constraint that provides entity integrity for a particular column or columns using a unique index. Columns in a UNIQUE constraint can be NULL , but only one NULL value is allowed per column. A table can have multiple UNIQUE constraints. Note: SQL Server Compact Edition can use indexes to enforce PRIMARY KEY and UNIQUE constraints. We recommend that you do not rely on this behavior nor try to modify any indexes that are created as part of a constraint. FOREIGN KEY...REFERENCES -A constraint that provides referential integrity for the data in the column. FOREIGN KEY constraints require that each value in the column exists in the specified column in the referenced table. ref_table - The name of the table referenced by the FOREIGN KEY constraint. ( ref_column [ ,... n ] ) - A column, or list of columns, from the table referenced by the FOREIGN KEY constraint. ON DELETE {CASCADE | NO ACTION} , Specifies what action happens to a row in the table that is created when that row has a referential relationship and the referenced row is deleted from the parent table. The default is NO ACTION . If CASCADE is specified, a row is deleted from the referencing table when the corresponding referenced row is deleted from the parent table. If NO ACTION is specified, SQL Server Compact Edition returns an error and the delete action on the referenced row in the parent table is rolled back. ON UPDATE {CASCADE | NO ACTION} Specifies what action happens to a row in the table that is created when that row has a referential relationship, and the referenced row is updated in the parent table. The default is NO ACTION . If CASCADE is specified, the row is updated in the referencing table when the corresponding referenced row is updated in the parent table. If NO ACTION is specified, SQL Server Compact Edition returns an error and the update action on the referenced row in the parent table is rolled back. column - A column or list of columns, in parentheses, used in table constraints to indicate the columns used in the constraint definition.
Faculty Notes : In the above example, Dbo – schema/owner of the database
Faculty Notes : Insert the following records in the given order : insert into emp values (1,'sss',1,getdate(),1) insert into dept values (1,‘ LKM’ , ’MDC ') insert into emp values (1,'sss',1,getdate(),1)
Faculty Notes : Like operator is using 8 characters. You can drop the rule with following command Drop rule sapid
Faculty Notes : We are inserting partial date in the above query.
Faculty Notes: The INSERT statement allows you to insert a single record or multiple records into a table.
Faculty Notes: Note that the order of the values should correspond to the order of the columns.
Faculty Notes: This is like a Bulk insert. Demo an INSERT statement in SQL Query Analyzer
Faculty Notes: Define UPDATE The UPDATE statement is used to modify the data in a table. Is used to update one or more columns in a row
Faculty Notes: The above update statement updates (changes) the Department Id of the employee whose Employee Id is 113 to Department Id 70
Faculty Notes: The above Update command uses sub queries to update Department Id and Job Id of the employee The first subquery fetches Department Id value of employee whose Employee Id is 100 and assigns it to Department Id in the Select statement The Second subquery fetches Job Id value of employee whose Employee Id is 200 and assigns it to Job Id in the where clause of the Select statement Note: In sub queries always remember the select statement for the sub query should generate only one row of one column i.e. a single value
Faculty Notes: Define DELETE The DELETE statement is used to delete rows in a table. Is used to delete one or more rows NOTE: Be cautious while using this statement as this may delete all records from a table accidentally, especially without the WHERE condition.
Faculty Notes: The above Delete statement will delete employee whose employee Id is 113
Faculty Notes: The above Delete statement is using a sub query to delete records from employee table It deletes employees of Dept Id whose value is generated from the sub query for the Department Type ‘CST’ Note: In sub queries always remember the select statement for the sub query should generate only one row of one column i.e. a single value
Faculty Notes: Review the key points on the slide. The key points should be used to summarize the content covered throughout this presentation.