Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Fundamentals

Similar presentations


Presentation on theme: "Database Fundamentals"— Presentation transcript:

1 Database Fundamentals
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Pete Harris | Microsoft Senior Content Publisher

2 Meet Brian Alderman | ‏@brianalderman
Chief Executive Office, Founder MicroTechPoint Industry-recognized consultant Noted author and conference speaker Brian’s expertise and designs range across Microsoft operating systems More than 25 years of industry experience Brian has been focused on helping IT Pros and Database Administrators (DBAs) better understand core Microsoft technologies for over 25 years. A frequent presenter at SharePoint Conferences around the world, he has authored or contributed to several SharePoint, SQL Server, and other technical books, and is a MCSE, MCT, and MCITP: SharePoint and SQL Server Administrator. Brian has a BS and MS in Computer Information Systems where he graduated summa cum laude from Regis University of Colorado Springs and lives in Scottsdale, AZ where he enjoys playing golf year round and traveling around the world. LinkedIn /brianalderman Blog

3 Meet Pete Harris | @SQLPete
Content Development Manager in Microsoft’s Learning Experiences team Focuses on SQL Server and Web training  With Microsoft since 1995  Part of the first team of developer training folks in the post-Microsoft University era Has built a variety of content and spoken to customers all over the world

4 Course Modules Database Fundamentals
01 | Introducing core database concepts (50 minutes) Define databases, example of relational database tables, and introduce common database terminology 02 | Relational Concepts (50 minutes) Normalization, referential integrity, and constraints 03 | Creating databases and database objects (50 minutes) Data types, database objects, DDL statements, and creating scripts 04 | Using DML statements (50 minutes) DML statements, using the SELECT statement; using INSERT, UPDATE, and DELETE to manage data; indexes and triggers 05 | SQL Server Administration Fundamentals (50 minutes) SQL Server security; securing database and objects; performing database backups and database restores

5 03 | Creating Databases and Database Objects
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Pete Harris | Microsoft Senior Content Publisher

6 Module Overview Data types Database objects DDL statements

7 Data types

8 Data types A data type is an attribute that specifies the type of data that an object can hold as well as the number of bytes of information that can be stored in the object If you have similar data types to choose from but they only differ in byte size, use the data type that has a larger range of values and/or has increased precision Exact numeric data types (int, tinyint) are the most common SQL Server data types used to store numeric information. Approximate Numerics include precision (p) which is the total number of decimal digits that could be stored, both to the left and right of the decimal point.

9 Data types Unicode data types provide storage of international characters, such as Japanese and Chinese, to allow worldwide businesses to use big vendor database products to store their data. Unicode data types takes more bytes to store the data in the database If you have similar data types to choose from but they only differ in byte size, use the data type that has a larger range of values and/or has increased precision Exact numeric data types (int, tinyint) are the most common SQL Server data types used to store numeric information. Approximate Numerics include precision (p) which is the total number of decimal digits that could be stored, both to the left and right of the decimal point.

10 Built-in data type categories
SQL Server 2012’s built-in data types are organized into the following categories: Exact numerics – (bigint, bit, decimal, int, money, numeric, smallint) Approximate numerics (float, real) Date and time (date, datetime2, datetime, datetimeoffset, time) Character strings (char, varchar, text) Unicode character strings (nchar, ntext, nvarchar) Binary strings (binary, varbinary, image) Other data types (cursor, timestamp, uniqueidentifier, table) Large valued data types (varchar(max), nvarchar(max)) Large object data types (text, ntext, image, xml)

11 Data types Money - used where you’ll store money or currency values
Int - used to store whole numbers and when performing mathematical computations Float - commonly used in the scientific community and is considered an approximate-number data type Datetime - used to store date and time values in one of many different formats

12 Data types Char – fixed length non-unicode string data type where n defines the string length Varchar – variable length non-unicode string data type that indicates the actual storage size of the data Bit (Boolean) – integer that can have a null, 0 (False), or 1 (True) value Datetimeoffset – a date combined with time of day that has time zone awareness

13 Data types storage size
Use/Description Storage Size Money Monetary or currency values -922,337,203,685, to 922,337,203,685, 8 bytes Int Integer data from -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647) 4 bytes Float Approximate number E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308 Depends on the value of n Datetime Date Range January 1, 1753, through December 31, 9999 Time Range 00:00:00 through 23:59:59.997 Char Fixed-length, non-Unicode string data. Can be a value from 1 through 8,000 n bytes Varchar Variable-length non-Unicode string. Can be a value from 1 through 8,000 Actual length + 2 bytes Bit Integer with a value of 0 or 1. 1 byte for every 8 bit columns Datetimeoffset Date range January 1,1 A.D. through December 31, 9999 A.D. Time range 00:00:00 through 23:59: Time zone offset range -14:00 through +14:00 10 Bytes

14 Implicit and explicit conversions
Implicit data type conversions occurs when the SQL Server expression evaluator automatically converts data from one data type to another to complete an operation like a comparison of two values Explicit data type conversions require the use of the CONVERT or CAST function to convert data from one data type to another before an operation like a comparison can be completed. To convert a numeric value into a character string CAST ( $ AS VARCHAR(10) ) Not all data types conversions are supported nchar cannot be converted to image Use CAST instead of CONVERT to adhere to ISO Use CONVERT instead of CAST to take advantage of the style functionality

15 Database objects

16 Tables A table is a collection of rows and columns that is used to organize information about a single topic. Each row within a table corresponds to a single record and contains several attributes that describe the row. EmployeeID LastName FirstName Department 100 Smith Bob IT 101 Jones Susan Marketing 102 Adams John Finance

17 Views A view is simply a virtual table consisting of different columns from one or more tables. Unlike a table, a view is stored in the database as a query object; therefore, a view is an object that obtains its data from one or more underlying tables.

18 Stored procedures A stored procedure is a group of Transact-SQL statements that have been compiled and saved so it can be run several times. Parameters can be passed to and returned from a stored procedure so they can be reused with different values. IF < (SELECT QuantityOnHand FROM Inventory WHERE PartID ) BEGIN -- SQL statements to update tables and process order. END ELSE -- SELECT statement to retrieve the IDs of alternate items -- to suggest as replacements to the customer.

19 User-Defined functions
User-defined functions (udf) are routines that takes zero or more parameters, completes an operation, and return the result of the operation as a value. There are three types of functions Scalar – returns a single data value Table-valued – returns a table data type System – Provided by SQL Server, cannot be modified

20 Primary differences between stored procedures and user-defined functions
Stored Procedures Called independently using EXEC statement Cannot JOIN stored procedures Can be used to modify SQL Server configuration Can use nondeterministic functions such as GETDATE() User-defined Functions Called from within another SQL statement Can JOIN UDF’s Cannot be used to modify SQL Server configuration Always stops execution of T-SQL code if error occurs

21 Naming conventions for your objects
PascalCase - The first letter of the identifier and the first letter of each subsequent concatenated word is capitalized EmployeeTable camelCase - The first letter of the identifier is lowercase and the first letter of each subsequent concatenated word is capitalized employeeTable Tip: Pick a naming convention and use it consistently throughout your database environment

22 Create a ClassDemoDB Database

23 DDL Statements

24 Common DDL statements CREATE – define new entities ALTER – modify existing entities DROP – remove existing entities

25 CREATE statement Used to create new entities in SQL Server including some of the most common entities Database Procedure Table Trigger Default View Index User Login Role CREATE DATABASE Sales ON ( NAME = Sales_dat, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\sales.mdf', SIZE = 10, MAXSIZE = 50, FILEGROWTH = 5 ) LOG ON ( NAME = Sales_log, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\salelog.ldf', SIZE = 5MB, MAXSIZE = 25MB, FILEGROWTH = 5MB ) ;

26 Create new table USE SALES GO --Create new table called Products CREATE TABLE dbo.Products1 ( ProductID int NULL, ProductName varchar(20) NULL, UnitPrice money NULL, ProductDescription varchar(50) NULL );

27 ALTER statement Used to modify existing entities in SQL Server including Database Trigger Table View Index User Login Role Procedure Schema ALTER DATABASE Sales Modify Name = SalesForecast ;

28 DROP statement Used to delete existing entities in SQL Server including Database Trigger Table View Index User Login Role Procedure Schema DROP DATABASE SalesForecast

29 Creating SQL Server Objects and generating scripts
Demo Creating SQL Server Objects and generating scripts

30 Summary A data type is an attribute that specifies the type of data that an object can hold The built-in data types fall into the following categories: Exact numerics Approximate numerics Date and time Character strings Unicode character strings Binary strings Other data types Large valued data types Large object data types

31 Summary A database can be created using SSMS or using the T-SQL CREATE DATABASE statement Database objects such as tables, views, stored procedures, and user-defined functions can be created using SSMS or using a DDL CREATE statement Database objects should be consistently created using either the PascalCase or camelCase naming convention

32 Summary The purpose of a table is to provide structure for storing data within a relational database A view is a virtual table consisting of columns from one or more tables and is stored in the database as a query object A stored procedure is a group of Transact-SQL statements that have been compiled and saved so they can be run several times Scripts can be created from existing objects

33


Download ppt "Database Fundamentals"

Similar presentations


Ads by Google