Database Modeling                                 with SQL Server                        Creating Databases and E/R Diagra...
Table of Contents1.    Introduction to SQL Server2.    Data Modeling – Principles3.    Data Types in SQL Server4.    Creat...
MS SQL Server 2008      Overview
What is Microsoft SQL Server? MS SQL Server             is a Relational Database Management System (RDBMS) from Microsoft...
Connecting to SQL Server Connecting to SQL Server requires   The name of the server machine / IP address   The name of ...
SQL Server Management                          Studio (SSMS) SQL Server Management Studio (SSMS) is     a powerful graphi...
SQL Server Management    Studio – Screenshot                          7
SQL Server Databases             System Databasesmaster   model       tempdb       msdb   distribution         pubs       ...
SQL Server Databases Each SQL Server database consists      of two files:   .mdf file    Contains the core data in the ...
SQL ServerManagement Studio      Live Demo
Relational Data Modeling     Fundamental Concepts
Steps in Database Design Steps in the database design process:  1. Identification of the entities  2. Identification of t...
Identification of Entities Entity tables represent objects from the real world   Most often they are nouns in the specif...
Identification of Columns Columns in the tables are characteristics   of the entities   They have name and type For exa...
Identification of the Columns Columns are clarifications for the entities in the text of the specification, for example: ...
How to Choose a Primary Key? Always define an additional   column for the primary key   Dont use an existing column (for...
Identification of Relationships Relationships are dependencies between the entities:  We need to develop a system that st...
Data Types in SQL  Server 2008
Data Types in SQL Server Numeric   bit (1-bit), integer (32-bit), bigint (64-bit)   float, real, numeric(scale, precisi...
Data Types in SQL Server (2) Binary   data   varbinary(size) – a sequence of bits   image – a binary block up to 1 GB ...
Data Types in SQL Server (3) Other types  timestamp – automatically generated number   whenever a change is made to the ...
Data Types in SQL Server (4) Nullable and NOT NULL types  All types in SQL Server may or may   not allow NULL values Pr...
Database Modeling with SQL Server Management Studio       Creating a Database
Connecting to SQL Server When starting SSMS a window pops up Usually it is            enough to just click the "Connect"...
Working with Object Explorer Object Explorer is the main tool                                to use when working with the...
Creating a New Database   In Object Explorer we go to the "Databases" and    choose "New Database…" from the context menu...
Creating a New Database (2)   In the "New Database" window enter the name of    the new database and click [OK]          ...
Database Modeling with SQL Server Management Studio       Creating E/R Diagrams
Creating an E/R diagram   In the "Database Diagrams" menu choose the    "New Database Diagram"   We can choose from the ...
Database Modeling with SQL Server Management Studio         Creating Tables
Creating Tables If the database                doesnt show immediately in Object Explorer perform "Refresh" [F5] Creatin...
Creating Tables (2) Enter table name                 and define the table columns (name and type):   Enter the    Choose ...
Creating Tables (3) Defining a primary   key    Right click on the column start and select    "Set Primary Key"          ...
Creating Tables (4) Defining an identity columns   Identity means that the values in a certain    column are auto genera...
Creating Tables (5) Setting an identity through the "Column Properties" window                                           ...
Creating Tables (6)   It is a good practice to set             Table                                             name    ...
Creating Tables (7)   When closing the window for the table, SSMS    asks whether to save the table     You can do it ma...
Database Modeling with SQL Server Management Studio   Creating Relationships between Tables
Creating Relationships To create one-to-many relationshipdrag the foreign key column onto the other table  Drag from the...
Self-Relationships Self-relationship                  can be created by dragging a foreign key onto the same table       ...
Database Modeling with SQL Server Management Studio       Naming Conventions
Naming Conventions Tables   Each word is capitalized (Pascal Case)   In English, plural   Examples: Users, PhotoAlbums...
Naming Conventions (2) Primary   key   Use "Id" or name_of_the_table + "Id"   Example: in the Users table the PK column...
Naming Conventions (3) Relationship names (constraints)   In English, Pascal Case   "FK_" + table1 + "_" + table2   Fo...
Naming Conventions (4) Unique key constraints names  "UK_" + table + column  For instance: UK_Users_UserName Views nam...
Database Modeling with SQL Server Management Studio         Live Demo                             46
Database Modeling                                                                                                         ...
Free Trainings @ Telerik Academy "C# and Databases       " course @ Telerik    Academy       academy.telerik.com/…csharp...
Upcoming SlideShare
Loading in...5
×

6. Data Modeling with SQL Server - C# and Databases

4,440

Published on

Database Modeling with SQL Server - Creating Databases and E/R Diagrams with SQL Server Management Studio
Telerik Software Academy: http://academy.telerik.com/school-academy/meetings/details/2012/01/06/desktop-applications-csharp-databases
The website and all video materials are in Bulgarian.
Introduction to SQL Server
Data Modeling – Principles
Data Types in SQL Server
Creating Databases in SQL Server
Creating Tables
Defining a Primary Key and Identity Columns
Creating Relationships between the Tables
One-to-many, Many-to-many, One-to-one
Naming conventions

Published in: Education, Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,440
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
358
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

6. Data Modeling with SQL Server - C# and Databases

  1. 1. Database Modeling with SQL Server Creating Databases and E/R Diagrams with SQL Server Management StudioSvetlin NakovManager Technical Trainerhttp://www.nakov.com/Telerik Software Academyhttp://academy.telerik.com/
  2. 2. Table of Contents1. Introduction to SQL Server2. Data Modeling – Principles3. Data Types in SQL Server4. Creating Databases in SQL Server5. Creating Tables6. Defining a Primary Key and Identity Columns7. Creating Relationships between the Tables  One-to-many, Many-to-many, One-to-one8. Naming conventions 2
  3. 3. MS SQL Server 2008 Overview
  4. 4. What is Microsoft SQL Server? MS SQL Server is a Relational Database Management System (RDBMS) from Microsoft  The main language supported in SQL Server is Transact SQL (T-SQL), an extension of SQL  Powerful, trustworthy, easy-to-use DB server The most recent version is SQL Server 2008 Works only on Windows systems A free distribution exists (SQL Server Express)  http://www.microsoft.com/express/database/ 4
  5. 5. Connecting to SQL Server Connecting to SQL Server requires  The name of the server machine / IP address  The name of the server instance  The name of the database  Username / password (if using SQL Server authentication) Types of authentication in SQL Server  Windows (by using a Windows user credentials)  Mixed (both Windows and SQL Server) 5
  6. 6. SQL Server Management Studio (SSMS) SQL Server Management Studio (SSMS) is a powerful graphical DB management tool  Administrate databases (create, modify, backup / restore DB)  Create and modify E/R diagrams  View / modify table data and other DB objects  Execute SQL queries  Free and easy to use tool  Works with all SQL Server versions 6
  7. 7. SQL Server Management Studio – Screenshot 7
  8. 8. SQL Server Databases System Databasesmaster model tempdb msdb distribution pubs Northwind … User Databases 8
  9. 9. SQL Server Databases Each SQL Server database consists of two files:  .mdf file  Contains the core data in the database  Schema, tables data, and other database objects  .ldf file  Transaction log – keeps track of transactions You need both these files to use the database You can move a database by SQL scripting, backup / restore, or copy the .mdf/.ldf files 9
  10. 10. SQL ServerManagement Studio Live Demo
  11. 11. Relational Data Modeling Fundamental Concepts
  12. 12. Steps in Database Design Steps in the database design process: 1. Identification of the entities 2. Identification of the columns in the tables 3. Defining a primary key for each entity table 4. Identification and modeling of relationships  Multiplicity of relationships 5. Defining other constraints 6. Filling test data in the tables 12
  13. 13. Identification of Entities Entity tables represent objects from the real world  Most often they are nouns in the specification  For example: We need to develop a system that stores information about students, which are trained in various courses. The courses are held in different towns. When registering a new student the following information is entered: name, faculty number, photo and date.  Entities: Student, Course, Town 13
  14. 14. Identification of Columns Columns in the tables are characteristics of the entities  They have name and type For example students have:  Name (text)  Faculty number (number)  Photo (binary block)  Date of enlistment (date) 14
  15. 15. Identification of the Columns Columns are clarifications for the entities in the text of the specification, for example: We need to develop a system that stores information about students, which are trained in various courses. The courses are held in different towns. When registering a new student the following information is entered: name, faculty number, photo and date. Students have the following characteristics:  Name, faculty number, photo, date of enlistment and a list of courses they visit 15
  16. 16. How to Choose a Primary Key? Always define an additional column for the primary key  Dont use an existing column (for example SSN)  Must be an integer number  Must be declared as a primary key  Use identity to implement auto-increment  Put the primary key as a first column Exceptions  Entities that have well known ID, e.g. countries (BG, DE, US) and currencies (USD, EUR, BGN) 16
  17. 17. Identification of Relationships Relationships are dependencies between the entities: We need to develop a system that stores information about students, which are trained in various courses. The courses are held in different towns. When registering a new student the following information is entered: name, faculty number, photo and date.  "Students are trained in courses" – many-to- many relationship  "Courses are held in towns" – many-to-one (or many-to-many) relationship 17
  18. 18. Data Types in SQL Server 2008
  19. 19. Data Types in SQL Server Numeric  bit (1-bit), integer (32-bit), bigint (64-bit)  float, real, numeric(scale, precision)  money – for money (precise) operations Strings  char(size) – fixed size string  varchar(size) – variable size string  nvarchar(size) – Unicode variable size string  text / ntext – text data block (unlimited size) 19
  20. 20. Data Types in SQL Server (2) Binary data  varbinary(size) – a sequence of bits  image – a binary block up to 1 GB Date and time  datetime – date and time starting from 1.1.1753 to 31.12.9999, a precision of 1/300 sec.  smalldatetime – date and time (1-minute precision) 20
  21. 21. Data Types in SQL Server (3) Other types  timestamp – automatically generated number whenever a change is made to the data row  uniqueidentifier – GUID identifier  xml – data in XML format 21
  22. 22. Data Types in SQL Server (4) Nullable and NOT NULL types  All types in SQL Server may or may not allow NULL values Primary key columns  Define the primary key Identity columns  Automatically increased values when a new row is inserted (auto-increment values)  Used in combination with primary key 22
  23. 23. Database Modeling with SQL Server Management Studio Creating a Database
  24. 24. Connecting to SQL Server When starting SSMS a window pops up Usually it is enough to just click the "Connect" button without changing anything 24
  25. 25. Working with Object Explorer Object Explorer is the main tool to use when working with the database and its objects Enables us:  To create a new database  To create objects in the database (tables, stored procedures, relationships and others)  To change the properties of objects  To enter records into the tables 25
  26. 26. Creating a New Database In Object Explorer we go to the "Databases" and choose "New Database…" from the context menu 26
  27. 27. Creating a New Database (2) In the "New Database" window enter the name of the new database and click [OK] 27
  28. 28. Database Modeling with SQL Server Management Studio Creating E/R Diagrams
  29. 29. Creating an E/R diagram In the "Database Diagrams" menu choose the "New Database Diagram" We can choose from the existing tables, which we want to add to the diagram 29
  30. 30. Database Modeling with SQL Server Management Studio Creating Tables
  31. 31. Creating Tables If the database doesnt show immediately in Object Explorer perform "Refresh" [F5] Creating new table: 31
  32. 32. Creating Tables (2) Enter table name and define the table columns (name and type): Enter the Choose the data Choose name of the type of the whether NULLs column here column here are allowed 32
  33. 33. Creating Tables (3) Defining a primary key Right click on the column start and select "Set Primary Key" 33
  34. 34. Creating Tables (4) Defining an identity columns  Identity means that the values in a certain column are auto generated (for int columns)  These values cannot be assigned manually  Identity Seed – the starting number from which the values in the column begin to increase.  Identity Increment – by how much each consecutive value is increased 34
  35. 35. Creating Tables (5) Setting an identity through the "Column Properties" window 35
  36. 36. Creating Tables (6) It is a good practice to set Table name the name of the table at the time it is created  Use the "Properties" window  If its not visible use "View"  "Properties Window" or press [F4] 36
  37. 37. Creating Tables (7) When closing the window for the table, SSMS asks whether to save the table  You can do it manually by choosing “Save Table” from the “File” menu or by pressing Ctrl + S 37
  38. 38. Database Modeling with SQL Server Management Studio Creating Relationships between Tables
  39. 39. Creating Relationships To create one-to-many relationshipdrag the foreign key column onto the other table  Drag from the child table to the parent table 39
  40. 40. Self-Relationships Self-relationship can be created by dragging a foreign key onto the same table 40
  41. 41. Database Modeling with SQL Server Management Studio Naming Conventions
  42. 42. Naming Conventions Tables  Each word is capitalized (Pascal Case)  In English, plural  Examples: Users, PhotoAlbums, Countries Columns  In English, singular  Each word is capitalized (Pascal Case)  Avoid reserved words (e.g. key, int, date)  Examples: FirstName, OrderDate, Price 42
  43. 43. Naming Conventions (2) Primary key  Use "Id" or name_of_the_table + "Id"  Example: in the Users table the PK column should be be called Id or UserId Foreign key  Use the name of the referenced table + "Id"  Example: in the Users table the foreign key column that references the Groups table should be named GroupId 43
  44. 44. Naming Conventions (3) Relationship names (constraints)  In English, Pascal Case  "FK_" + table1 + "_" + table2  For example: FK_Users_Groups Index names  "IX_" + table + column  For example: IX_Users_UserName 44
  45. 45. Naming Conventions (4) Unique key constraints names  "UK_" + table + column  For instance: UK_Users_UserName Views names  V_ + name  Example: V_BGCompanies Stored procedures names  usp_ + name  Example: usp_InsertCustomer(@name) 45
  46. 46. Database Modeling with SQL Server Management Studio Live Demo 46
  47. 47. Database Modeling with SQL Server курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET http://academy.telerik.com програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезаниякурсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
  48. 48. Free Trainings @ Telerik Academy "C# and Databases " course @ Telerik Academy  academy.telerik.com/…csharp-databases Telerik Software Academy  academy.telerik.com Telerik Academy @ Facebook  facebook.com/TelerikAcademy Telerik Software Academy Forums  forums.academy.telerik.com 48
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×