SQL Server Lecture 2

649 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
649
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
37
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • In the following slide we are going to know 25 stupid mistakes that should be avoided to get better query readability, maintainability, security and performance.
  • SQL Server Lecture 2

    1. 1. SQL Server 2008Lecture 2Hazem TorabCEO, FounderEnozom Software
    2. 2. Agenda• Student & Courses Example• Creating Database Tables• Data Types• Transact SQL Statements – DML
    3. 3. Students & Courses Example• Problem StatementA training center that teaches training courses for students is asking you to make a database for them.Every student can register for one or more course, the courses are usually opened once every a month. For example “Windows 7” course is available on 1/1/2013 and 1/2/2013.Some course can‟t be registered for a student without passing other courses for example for “SQL Server” course student must pass “Windows 7” & “Database” courses
    4. 4. Write Down Your Design
    5. 5. Creating Database Tables• Before you start, disable the prevention of saving changes that require table re-creation from Tools->options->Designers-> tables and database diagrams
    6. 6. Creating Database Tables• Right click on tables in your database• Type your column names
    7. 7. Creating Database Tables• Set Your Primary Key• Choose the suitable Data Type
    8. 8. Creating Database Tables• Create Relations
    9. 9. Creating Database Tables• Create Relations
    10. 10. Complete Design
    11. 11. SQL Server Data Types• Main Data Types – Int – DateTime – Decimal – Money – Float – Varchar – Nvarchar – XML – Bit – Text• http://www.connectionstrings.com/Articles/Show/sql-server-data- type-reference
    12. 12. Transact SQL – Data Manipulation Language• SelectSelect CourseID, CourseName from Course• InsertInsert into Course (CourseName) values („Windows‟)• UpdateUpdate Course Set Course= „Windows 7‟ where CourseID = 1• DeleteDelete Course where CourseID = 1
    13. 13. SQL Operators• Mathematical OperatorsSELECT 15 + 4, --Addition 15 - 4, --Subtraction 15 * 4, --Multiplication 15 / 5, -- Division 15 % 4; --Modulus
    14. 14. SQL Operators• Equality Operators Operator Example Defined Result =, IS 5=5 5 equal to 5? True 7 IS NOT (!=) !=, IS NOT 7 != 2 True equal to 2? < 7<4 7 less than 4? False > 7>4 greater than 4? True Is 7 less than or <= 7 <= 11 True equal to 11? Is 7 greater than >= 7 >= 11 False or equal to 11? „enozom‟ like Contains the Like True „%e%‟ character „e‟
    15. 15. Joins• Inner Join Get students with their cities Select StudentFirstName, CityName from student inner join city on studenet.CityID= City.CityID• Left join Get All students with their cities and if it has no cities entered Select StudentFirstName, CityName from student left outer join city on studenet.CityID= City.CityID
    16. 16. Joins• Right Join Get All cities whether it has no students Select StudentFirstName, CityName from student right outer join city on studenet.CityID= City.CityID• Cross Join Get All cities whether it has no students and all student whether it has no city Select StudentFirstName, CityName from student right outer join city on studenet.CityID= City.CityID
    17. 17. Using Query Editor for Complex Queries
    18. 18. Using Query Editor
    19. 19. Select Tables and Filters
    20. 20. Aggregate Functions• Sum• Count• Max• Min• AVG• Gets the count of all studentsSelect Count (*) from student
    21. 21. Grouping• Gets the count of student in each city Select CityName, Count(*) from student inner join city on student.cityid = city.cityid Group by CityName
    22. 22. Sorting• Select StudentFirstname, StudentMiddleName, StudentLastName from student order by StudentFirstname, StudentMiddleName, StudentLastName ASC
    23. 23. Exercise• Get list of student registered in specific course• Get Number of student registered in each group for course „Windows 7‟• Get number of student in each city while their firstname starts with „A‟• Get the total fees that spent for each student• Get the collected money for each course
    24. 24. Thank YouHazem TorabHazem.torab@enozom.comhttp://www.enozom.comhttps://www.facebook.com/Enozom

    ×