Complicated Database Manipulation

后端 未结 3 1157
走了就别回头了
走了就别回头了 2021-01-16 13:39

I draw a very simple database design for my ASP.NET application and with a little help here and there I managed to implement my design...but I want to insert data to this ta

3条回答
  •  情书的邮戳
    2021-01-16 14:13

    Sorry to frighten you, but that's a very small database design.

    When I first started to need to do database designs for web applications, I found a crutch. Fortunately, the crutch I used back then is now available in a free tool, NORMA, which works with either Visual Studio 2008 or 2005 (2010 support is coming).

    I made a quick guess about your design and got the following ORM model:

    Kids and Sponsors

    This led to a database with ten tables, including the junction tables necessary for many to many relationships.

    The NORMA tool can generate DDL from the model for several different databases, and can also create XML Schemas and LINQ to SQL objects.


    BTW, here's an ER diagram of the generated database:

    ER


    I don't see why you would need all of this in a single resultset, but here's a query which I think might return it all. I haven't tested this with data:

    SELECT k.KidId, k.Name, k.FavoriteToy, 
        s.SponsorId, s.Name, 
        st.SponsorShipTypeCode, st.Description AS SponsortshipTypeDescription,
        fw.FieldOfWorkCode, fw.Description AS FieldOfWorkDescription,
        Skill.SkillCode, Skill.Description AS SkillDescription,
        Parent.ParentId, Parent.Name AS ParentName
    FROM Kid k
    LEFT OUTER JOIN KidHasParent ON KidHasParent.KidId = k.KidId
    INNER JOIN Parent ON Parent.ParentId = KidHasParent.ParentId
    LEFT OUTER JOIN KidHasSkill ON KidHasSkill.KidId = k.KidId
    INNER JOIN Skill ON Skill.SkillCode = KidHasSkill.SkillCode
    LEFT OUTER JOIN Sponsor s ON s.SponsorId = k.SponsorId
    INNER JOIN SponsorIsOfSponsorShipType Sst ON Sst.SponsorId = s.SponsorId
    INNER JOIN SponsorShipType st ON st.SponsorShipTypeCode = sst.SponsorShipTypeCode
    LEFT OUTER JOIN SponsorParticipatesInFieldOfWork sfw ON sfw.SponsorId = s.SponsorId
    INNER JOIN FieldOfWork fw ON fw.FieldOfWorkCode = sfw.FieldOfWorkCode
    

    I created this query by looking at the ORM diagram to know which paths are optional (LEFT JOIN), and then by using a tool (ApexSQL Edit) which uses the foreign keys to help me build joins. This took under ten minutes.

提交回复
热议问题