Download presentation
Presentation is loading. Please wait.
Published byPatricia Chandler Modified over 8 years ago
1
SQL Basics Review Reviewing what we’ve learned so far…….
2
Objectives DBMS vs. RDBMS What is SQL? SQL Commands - DDL, DML and DCL What are Database Objects? Tables Indexes Views Programming DBMS vs. RDBMS What is SQL? SQL Commands - DDL, DML and DCL What are Database Objects? Tables Indexes Views Programming
3
Relational Database Server Goals ReliabilityAvailabilityScalabilityPerformance Data Integrity & Protection Transaction Isolation ReportingData Analysis
4
SQL Server Physical Data Files Database storage Primarily table data and index data Database Files: Primary data file (*.mdf) Secondary data files (*.ndf) Transaction log file(s) (*.ldf) Filegroups: Logical collections of files Objects can be created on filegroups Database storage Primarily table data and index data Database Files: Primary data file (*.mdf) Secondary data files (*.ndf) Transaction log file(s) (*.ldf) Filegroups: Logical collections of files Objects can be created on filegroups
5
Database Schemas Schemas Logical collection of related database objects Part of full object name: Server.Database.Schema.Object Default schema is “dbo” Managing Schemas CREATE, ALTER, DROP SCHEMA SQL Server Management Studio Can assign default schemes to database users: WITH DEFAULT_SCHEMA ‘SchemaName’ Schemas Logical collection of related database objects Part of full object name: Server.Database.Schema.Object Default schema is “dbo” Managing Schemas CREATE, ALTER, DROP SCHEMA SQL Server Management Studio Can assign default schemes to database users: WITH DEFAULT_SCHEMA ‘SchemaName’
6
DBMS vs RDBMS A DBMS is system software for creating databases, it provides a way to retrieve, update and manage data. A DBMS has to be persistent, i.e. it should be accessible when the program created the data ceases to exist or even the application that created the data restarted. A DBMS also has to provide some uniform methods independent of a specific application for accessing the information that is stored. RDBMS is a Relational Database Management System Relational DBMS. This adds the additional condition that the system supports a tabular structure for the data, with enforced relationships between the tables. This excludes the databases that don’t support a tabular structure or don’t enforce relationships between tables. Persists data in the form of rows and columns Provides primary key, to uniquely identify the rows Creates Indexes Provides virtual table creation Provide multi user accessibility A DBMS is system software for creating databases, it provides a way to retrieve, update and manage data. A DBMS has to be persistent, i.e. it should be accessible when the program created the data ceases to exist or even the application that created the data restarted. A DBMS also has to provide some uniform methods independent of a specific application for accessing the information that is stored. RDBMS is a Relational Database Management System Relational DBMS. This adds the additional condition that the system supports a tabular structure for the data, with enforced relationships between the tables. This excludes the databases that don’t support a tabular structure or don’t enforce relationships between tables. Persists data in the form of rows and columns Provides primary key, to uniquely identify the rows Creates Indexes Provides virtual table creation Provide multi user accessibility
7
SQL SQL (Structured Query Language) is a special-purpose programming language designed for managing data in a relational database. SQL is used to communicate with a database. According to ANSI (American National Standards Institute), it is the standard language for relational database management systems. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. SQL can execute queries against a database SQL can retrieve data from a database SQL can insert records in a database SQL can update records in a database SQL can delete records from a database SQL can create new databases SQL can create new tables in a database SQL can create stored procedures in a database SQL can create views in a database SQL can set permissions on tables, procedures, and views SQL (Structured Query Language) is a special-purpose programming language designed for managing data in a relational database. SQL is used to communicate with a database. According to ANSI (American National Standards Institute), it is the standard language for relational database management systems. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. SQL can execute queries against a database SQL can retrieve data from a database SQL can insert records in a database SQL can update records in a database SQL can delete records from a database SQL can create new databases SQL can create new tables in a database SQL can create stored procedures in a database SQL can create views in a database SQL can set permissions on tables, procedures, and views
8
SQL Commands - DDL, DML, DCL The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be classified into groups based on their nature: DDL – Data Definition Language : Create Alter Drop DML – Data Manipulation Language : Select Insert Update Delete DCL – Data Control Language : Grant Revoke The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be classified into groups based on their nature: DDL – Data Definition Language : Create Alter Drop DML – Data Manipulation Language : Select Insert Update Delete DCL – Data Control Language : Grant Revoke
9
Database Objects Tables Data Storage Data Retrieval Referential Integrity Indexes Improves Query Performance Clustered Non- clustered Views Logical Result Sets Based on SELECT Queries Virtual Tables Programmability Stored Procs Functions Triggers Constraints
10
Tables The data in RDBMS is stored in database objects called tables. The table is a collection of related data entries and it consists of columns and rows. Remember, a table is the most common and simplest form of data storage in a relational database Every table is broken up into smaller entities called fields or columns. A field is a column in a table that is designed to maintain specific information about every record in the table. A record, also called a row of data, is each individual entry that exists in a table. A column is a vertical entity in a table that contains all information associated with a specific field in a table The data in RDBMS is stored in database objects called tables. The table is a collection of related data entries and it consists of columns and rows. Remember, a table is the most common and simplest form of data storage in a relational database Every table is broken up into smaller entities called fields or columns. A field is a column in a table that is designed to maintain specific information about every record in the table. A record, also called a row of data, is each individual entry that exists in a table. A column is a vertical entity in a table that contains all information associated with a specific field in a table
11
Indexes Indexes are created on columns in tables or views. The index provides a fast way to look up data based on the values within those columns. For example, if you create an index on the primary key and then search for a row of data based on one of the primary key values, SQL Server first finds that value in the index, and then uses the index to quickly locate the entire row of data. Without the index, a table scan would have to be performed in order to locate the row, which can have a significant effect on performance. Clustered Index – Only one clustered index on a table or view. Data is sorted only if a clustered index has been defined on a table. Nonclustered Indexes – Can not be sorted like clustered indexes, but can create multiple nonclustered indexes on a table or view. Indexes are created on columns in tables or views. The index provides a fast way to look up data based on the values within those columns. For example, if you create an index on the primary key and then search for a row of data based on one of the primary key values, SQL Server first finds that value in the index, and then uses the index to quickly locate the entire row of data. Without the index, a table scan would have to be performed in order to locate the row, which can have a significant effect on performance. Clustered Index – Only one clustered index on a table or view. Data is sorted only if a clustered index has been defined on a table. Nonclustered Indexes – Can not be sorted like clustered indexes, but can create multiple nonclustered indexes on a table or view.
12
Indexes In addition to an index being clustered or nonclustered, it can be configured in other ways: Composite index: An index that contains more than one column. In both SQL Server 2005 and 2008, you can include up to 16 columns in an index, as long as the index doesn’t exceed the 900-byte limit. Both clustered and nonclustered indexes can be composite indexes. Unique Index: An index that ensures the uniqueness of each value in the indexed column. If the index is a composite, the uniqueness is enforced across the columns as a whole, not on the individual columns. For example, if you were to create an index on the FirstName and LastName columns in a table, the names together must be unique, but the individual names can be duplicated. A unique index is automatically created when you define a primary key or unique constraint: Primary key: When you define a primary key constraint on one or more columns, SQL Server automatically creates a unique, clustered index if a clustered index does not already exist on the table or view. However, you can override the default behavior and define a unique, nonclustered index on the primary key. Unique: When you define a unique constraint, SQL Server automatically creates a unique, nonclustered index. You can specify that a unique clustered index be created if a clustered index does not already exist on the table. Covering index: A type of index that includes all the columns that are needed to process a particular query. For example, your query might retrieve the FirstName and LastName columns from a table, based on a value in the ContactID column. You can create a covering index that includes all three columns. In addition to an index being clustered or nonclustered, it can be configured in other ways: Composite index: An index that contains more than one column. In both SQL Server 2005 and 2008, you can include up to 16 columns in an index, as long as the index doesn’t exceed the 900-byte limit. Both clustered and nonclustered indexes can be composite indexes. Unique Index: An index that ensures the uniqueness of each value in the indexed column. If the index is a composite, the uniqueness is enforced across the columns as a whole, not on the individual columns. For example, if you were to create an index on the FirstName and LastName columns in a table, the names together must be unique, but the individual names can be duplicated. A unique index is automatically created when you define a primary key or unique constraint: Primary key: When you define a primary key constraint on one or more columns, SQL Server automatically creates a unique, clustered index if a clustered index does not already exist on the table or view. However, you can override the default behavior and define a unique, nonclustered index on the primary key. Unique: When you define a unique constraint, SQL Server automatically creates a unique, nonclustered index. You can specify that a unique clustered index be created if a clustered index does not already exist on the table. Covering index: A type of index that includes all the columns that are needed to process a particular query. For example, your query might retrieve the FirstName and LastName columns from a table, based on a value in the ContactID column. You can create a covering index that includes all three columns.
13
Views A view is a virtual table that consists of columns from one or more tables. Though it is similar to a table, it is stored in the database. It is a query stored as an object. Hence, a view is an object that derives its data from one or more tables. These tables are referred to as base or underlying tables.
14
Designing a Database Normalization Reduces redundancy and improves data modification performance Denormalization is often done to enhance reporting performance (at the expense of disk space and redundancy) Referential Integrity Maintains the logical relationships between database objects Normalization Reduces redundancy and improves data modification performance Denormalization is often done to enhance reporting performance (at the expense of disk space and redundancy) Referential Integrity Maintains the logical relationships between database objects
15
Database Normalization Database normalization is the process of efficiently organizing data in a database. There are two reasons of the normalization process: Eliminating redundant data, for example, storing the same data in more than one tables. Ensuring data dependencies make sense. Normalization guidelines are divided into normal forms; think of form as the format or the way a database structure is laid out. The aim of normal forms is to organize the database structure so that it complies with the rules of first normal form, then second normal form, and finally third normal form. Database normalization is the process of efficiently organizing data in a database. There are two reasons of the normalization process: Eliminating redundant data, for example, storing the same data in more than one tables. Ensuring data dependencies make sense. Normalization guidelines are divided into normal forms; think of form as the format or the way a database structure is laid out. The aim of normal forms is to organize the database structure so that it complies with the rules of first normal form, then second normal form, and finally third normal form.
16
Summary The Structured Query Language (SQL) defines a standard for interacting with relational databases Most platforms support ANSI-SQL 92 Most platforms provide many non-ANSI-SQL additions Most important data modification SQL statements: SELECT: Returning rows UPDATE: Modifying existing rows INSERT: Creating new rows DELETE: Removing existing rows The Structured Query Language (SQL) defines a standard for interacting with relational databases Most platforms support ANSI-SQL 92 Most platforms provide many non-ANSI-SQL additions Most important data modification SQL statements: SELECT: Returning rows UPDATE: Modifying existing rows INSERT: Creating new rows DELETE: Removing existing rows
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.