Page 61 - SQL
P. 61

Chapter 17: Example Databases and Tables




        Examples



        Auto Shop Database


        In the following example - Database for an auto shop business, we have a list of departments,
        employees, customers and customer cars. We are using foreign keys to create relationships
        between the various tables.


        Live example: SQL fiddle



        Relationships between tables



            •  Each Department may have 0 or more Employees
            •  Each Employee may have 0 or 1 Manager
            •  Each Customer may have 0 or more Cars




        Departments



          Id  Name


          1   HR

          2   Sales


          3   Tech


        SQL statements to create the table:


         CREATE TABLE Departments (
             Id INT NOT NULL AUTO_INCREMENT,
             Name VARCHAR(25) NOT NULL,
             PRIMARY KEY(Id)
         );

         INSERT INTO Departments
             ([Id], [Name])
         VALUES
             (1, 'HR'),
             (2, 'Sales'),
             (3, 'Tech')
         ;








        https://riptutorial.com/                                                                               43
   56   57   58   59   60   61   62   63   64   65   66