Update a many-to-many relation (student, class, classgrade) : many 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 » many to many 
4. 10. 1. Update a many-to-many relation (student, class, classgrade)
4CREATE TABLE Students(
5>     StudentID int Primary Key,
6>     FirstName nvarchar(30),
7>     LastName nvarchar(50),
8>     FullName AS (FirstName + ' ' + LastName)
9)
10> GO
1>
2CREATE TABLE Classes(
3>     ClassID int Primary Key,
4>     ClassTitle varchar(50)
5)
6> GO
1>
2CREATE TABLE ClassGrades(
3>     ClassID int,
4>     StudentID int,
5>     GradeLetter varchar(2),
6>     Constraint PK_ClassGrades
7>         PRIMARY KEY(ClassID, StudentID),
8>     Constraint FK_Classes_ClassID
9>         FOREIGN KEY(ClassID)
10>         REFERENCES Classes(ClassIDON UPDATE CASCADE,
11>     Constraint FK_Students_StudentID
12>         FOREIGN KEY(StudentID)
13>         REFERENCES Students(StudentIDON UPDATE CASCADE
14)
15> GO
1>
2SELECT FROM Classes
3> GO
ClassID     ClassTitle
----------- --------------------------------------------------

(rows affected)
1>
2>
3> --Insert classes rows
4INSERT Classes VALUES(1,'SQL')
5INSERT Classes VALUES(999,'Java')
6> GO

(rows affected)

(rows affected)
1>
2> --Insert Students rows
3INSERT Students VALUES(1'Poor', 'DBA')
4INSERT Students VALUES(2'Better', 'DBA')
5> GO

(rows affected)

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

(rows affected)

(rows affected)

(rows affected)
1>
2UPDATE Classes
3> SET ClassID = 998,
4>     ClassTitle = 'Java'
5WHERE ClassID = 999
6> GO

(rows affected)
1>
2SELECT FROM Classes
3> GO
ClassID     ClassTitle
----------- --------------------------------------------------
          SQL
        998 Java

(rows affected)
1>
2> drop table ClassGrades;
3> drop table Classes;
4> drop table Students;
5> GO
4. 10. many to many
4. 10. 1. Update a many-to-many relation (student, class, classgrade)
4. 10. 2. Implementing Many-to-Many Relationships with Foreign Keys
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.