Introduction to SQL Commands
Learn the preliminary steps to use SQL commands.
We'll cover the following...
This lesson will teach the basic concepts behind entities and relationships along with hands-on SQL commands and aggregate functions. We’ll also go over the primary key, foreign key, and various operators, including Like, AND, and OR.
CRMS
We’ve started to design the database for a basic customer relationship management system (CRMS). Below is a picture of the tables this database is going to have alongside the relationships.
Basic details of the tables
We’ll start with a recap of the entity relationship diagram (ERD) above. Then, we’ll discuss some rules about the columns that may not be obvious from the diagram.
We see four tables.
customersordersproductsorder_items
The customers table
-
The
customerstable has anidcolumn, which is also going to be the primary key of the table. That means each customer will have a uniqueidvalue. -
This table has a
namecolumn. The entries aren’t unique. Two customers may have the samename. It’s a mandatory column because we can’t have a customer without aname.
The orders table
- This table also has an
idcolumn. This is the primary key of the table in that each order will have a uniqueid. - It has a
customer_idcolumn. This will be a reference to a corresponding row in thecustomerstable, and it’ll indicate which customer the order belongs to. We call this a foreign key because it corresponds to the primary key (id) of another table (customers). Thecustomer_idis mandatory. - It has an
order_numbercolumn. This is a string-type column that’s mandatory, and the entry should be unique. - It has an
ordered_atcolumn. This is a time stamp and is mandatory. It records when the customer has issued an order. If it’s not given at order creation, it’ll automatically take the current date time stamp. - There’s a relationship between
customersandorders. One customer can have many orders referencing them, so, the relationship is one-to-many. Also, for every one order, we can find one customer the order belongs to.
The products table
-
This table has an
idcolumn, which is the primary key. -
It has a
namecolumn as well, which is a mandatory string-type column. No two products can have the samename.
The order_items table
-
This table has an
idcolumn, which is the primary key of the table. -
It has a reference to the instance of
orderstheorder_itemsbelongs to. This is theorder_idcolumn. It’s a foreign key. -
There’s a one-to-many relationship between
ordersandorder_items. -
This table has a reference to the product the
order itemrefers to. The reference is the columnproduct_id, which also acts as a foreign key. This is mandatory because we can’t have an order item without reference to an instance ofproducts. -
There is a one-to-many relationship between
productsandorder_items. -
There’s a business rule that says that we can’t have the same product twice in the same order. We use the
quantityfield to allow for many instances of the same product to exist in the same order. Thequantityis an integer type column. It’s mandatory and has the default value1. -
The
order_itemstable is there to help model the properties of the many-to-many relationship betweenordersandproducts. As we said in the previous chapter, a many-to-many relationship is modeled using a table in the middle. In our case, this table isorder_items, and we end up having two one-to-many relationships. This is because one order can have many order items, and one product can have many order items.
There are a lot of details surrounding the data model even for this simple CRMS, and we haven’t listed them all.