An one-to-many relation : One to many « Table Join « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Table Join » One to many 
4. 9. 1. An one-to-many relation
3CREATE TABLE Classes(
4>     ClassID int PRIMARY KEY,
5>     ClassTitle varchar(50)
6)
7> GO
1>
2CREATE TABLE ClassGrades(
3>     ClassID int REFERENCES Classes(ClassID)
4>         ON UPDATE CASCADE,
5>     StudentID int,
6>     GradeLetter varchar(2),
7>     Constraint PK_ClassGrades
8>         PRIMARY KEY(ClassID, StudentID)
9)
10>
11INSERT Classes VALUES(1,'SQL')
12INSERT Classes VALUES(999,'Java')
13> GO

(rows affected)

(rows affected)
1>
2INSERT ClassGrades VALUES(11'C+')
3>
4INSERT ClassGrades VALUES(12'A+')
5INSERT ClassGrades VALUES(9992'A')
6> GO

(rows affected)

(rows affected)

(rows affected)
1>
2>
3SELECT CG.StudentID, C.ClassTitle, CG.GradeLetter
4FROM Classes C, ClassGrades CG
5WHERE C.ClassID = CG.ClassID
6> GO
StudentID   ClassTitle                                         GradeLetter
----------- -------------------------------------------------- -----------
          SQL                                                C+
          SQL                                                A+
          Java                                               A

(rows affected)
1>
2UPDATE Classes
3> SET ClassTitle = 'The Life of Jesus Christ'
4WHERE ClassID = 999
5> GO

(rows affected)
1>
2SELECT CG.StudentID, C.ClassTitle, CG.GradeLetter
3FROM Classes C, ClassGrades CG
4WHERE C.ClassID = CG.ClassID
5> GO
StudentID   ClassTitle                                         GradeLetter
----------- -------------------------------------------------- -----------
          SQL                                                C+
          SQL                                                A+
          The Life of Jesus Christ                           A

(rows affected)
1>
2>
3> drop table ClassGrades;
4> GO
1>
2> drop table Classes;
3> GO
4. 9. One to many
4. 9. 1. An one-to-many relation
4. 9. 2. Enables the changes in the Classes table to propagate to the ClassGrades table
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.