Search code examples
sqlindexingsql-server-2008-express

Adding SQL indices


I'm currently working on a SQL assignment that wants me to: Add an index for each foreign key and an index on Company for Customer and Shipper.

I've created the 3 tables that were needed, created the foreign and primary keys, and so on. But my textbook has not mentioned anything about indices and I am at a loss as to what to do. If you have an answer, it'd be nice to know how you got to it.

The structure of the tables:

Customer

CustomerID (PK) | Company   | ContactName  | Phone

Order

OrderID (PK)    | OrderDate | ShippedDate  | ShipperID  | Freight  | CustomerID (FK)

Shipper

ShipperID (PK)  | Company   | Phone

Solution

  • You should be looking at the online documentation, but...

    To create indexes for foreign keys:

    create index Order_ShipperID on Order(ShipperID);
    create index Order_CustomerID on Order(CustomerID);
    

    To create indexes on Company for Customer and Shipper:

    create index Customer_Company on Customer(Company);
    create index Shipper_Company on Shipper(Company);
    

    The names of the indexes can be anything, but I usually follow this naming convention.


    BTW, the choice of name "Order" is a poor one because its a reserved word.