Create southwind database and table


CREATE DATABASE southwind; /*creates a new database and names it*/
SHOW DATABASES; /*displays all databases*/
USE southwind; /*tells mysql which database you want to work with*/
SELECT DATABASE; /*tells you which database has been selected*/

SHOW TABLES; /*displays any tables that are associated with database*/

mysql> CREATE TABLE products (
-> productID INT UnSIGNED NOT NULL AUTO_INCREMENT,
-> productCode CHAR(3) NOT NULL DEFAULT ”,
-> name VARCHAR(30) NOT NULL DEFAULT ”,
-> quantity INT UNSIGNED NOT NULL DEFAULT 0,
-> price DECIMAL(7,2) NOT NULL DEFAULT 99999.99,
-> PRIMARY KEY (productID)
-> );

SHOW TABLES; /*at this point, with a new database, would show one table*/

DESCRIBE products; /*displays the structure of the table products*/

SHOW CREATE TABLE products \G /*More detailed than DESCRIBE*/

Explanations for structure of each variable:
– productID is INT UNSIGNED – non-negative integers.
– productCode is CHAR(3) – a fixed-length alphanumeric string of 3 characters.
– name is VARCHAR(30) – a variable-length string of up to 30 characters.
Note: We use fixed-length string for productCode, as we assume that the productCode contains exactly 3 characters. On the other hand, we use variable-length string for name, as its length varies – VARCHAR is more efficient than CHAR.
– quantity is also INT UNSIGNED (non-negative integers).
– price is DECIMAL(10,2) – a decimal number with 2 decimal places.
Note: DECIMAL is precise (represented as integer with a fix decimal point). On the other hand, FLOAT and DOUBLE (real numbers) are not precise and are approximated. DECIMAL type is recommended for currency.

The attribute “NOT NULL” specifies that the column cannot contain the NULL value. NULL is a special value indicating “no value”, “unknown value” or “missing value”. In our case, these columns shall have a proper value. We also set the default value of the columns. The column will take on its default value, if no value is specified during the record creation.

We set the column productID as the so-called primary key. Values of the primary-key column must be unique. Every table shall contain a primary key. This ensures that every row can be distinguished from other rows. You can specify a single column or a set of columns (e.g., firstName and lastName) as the primary key. An index is build automatically on the primary-key column to facilitate fast search. Primary key is also used as reference by other tables.

We set the column productID to AUTO_INCREMENT. with default starting value of 1. When you insert a row with NULL (recommended) (or 0, or a missing value) for the AUTO_INCREMENT column, the maximum value of that column plus 1 would be inserted. You can also insert a valid value to an AUTO_INCREMENT column, bypassing the auto-increment.

Now we can add data into the new table called products

This is how to, from terminal, add data into the fields within products
INSERT INTO products VALUES (1001, ‘PEN’, ‘Pen Red’, 5000, 1.23);

In this case, because productID is set for auto_increment, I can enter NULL, and it will automatically pick the next number in sequence). In this case, two more records:
INSERT INTO products VALUES
(NULL, ‘PEN’, ‘Pen Blue’, 8000, 1.25),
(NULL, ‘PEN’, ‘Pen Black’, 2000, 1.25);

In this case, you can specify the fields. Because I left ProductID blank it automatically enters
INSERT INTO products (productCode, name, quantity, price) VALUES
(‘PEC’, ‘Pencil 2B’, 10000, 0.48),
(‘PEC’, ‘Pencil 2H’, 8000, 0.49);

Because I’ve got NULL within the data structure, if a field is entered without info it just defaults without error
INSERT INTO products (productCode, name) VALUES (‘PEC’, ‘Pencil HB’);

This returns an expected error because the first field (productID) cannot be null
mysql> INSERT INTO products values (NULL, NULL, NULL, NULL, NULL);
ERROR 1048 (23000): Column ‘productCode’ is set for NOT NULL

select * from products;
Returns the entries I’ve made within products
Note: I’ve also been playing with myPHPAdmin, and seeing the various changes and reports

delete from products where productID = 1006;
Note: This deletes this record. It also had some missing data.

Categories

Skip to toolbar