0% found this document useful (0 votes)
49 views

InfoMan-Week 6-Creating-DBase2021

This document provides instructions for creating and managing a database using SQL commands. It explains how to install MySQL, create a database called Southwind, and build a table with columns like productID, productCode, name, quantity, and price. The document demonstrates commands for creating the table, inserting rows, listing rows, deleting rows, and adding a new column to the table. The goal is for students to learn how to create and use a database by performing tasks like building a table, saving, opening, and manipulating data.

Uploaded by

Sherwin Limbo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

InfoMan-Week 6-Creating-DBase2021

This document provides instructions for creating and managing a database using SQL commands. It explains how to install MySQL, create a database called Southwind, and build a table with columns like productID, productCode, name, quantity, and price. The document demonstrates commands for creating the table, inserting rows, listing rows, deleting rows, and adding a new column to the table. The goal is for students to learn how to create and use a database by performing tasks like building a table, saving, opening, and manipulating data.

Uploaded by

Sherwin Limbo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

AY 2021-2022

INFORMATION
MANAGEMENT

Prepared By:

Rowena S. David
CCS Faculty
Creating a Database
Learning Outcomes
At the end of the topic session, the students should be able to:

1. Demonstrate how to create and use a database


2. Illustrate the building of table
3. Perform saving and opening of database
Procedures:

• Install the SQLyog and MySQL Server in


your computer unit.

• Once you installed it, the MySQL 5.5


command line will be shown in Start
program.

• Go to the command prompt mentioned


above.
• Input your password:
Enter password: 1234
(depends on the password you registered during
installation)
• After you entered your password, the prompt below
will be displayed.
Here are the list of commands for creating a database:
A. Database-Level
• CREATE DATABASE databaseName - Creates a new database
• SHOW DATABASES - Shows all the databases in this
server
DROP DATABASE databaseName - Deletes the database (irrecoverable!)
CREATE DATABASE databaseName - Creates a database
USE databaseName - Sets the default (current) database
Here are the list of commands for creating a database:
B. Table-Level
CREATE TABLE - creates a new table
SHOW TABLES - shows all the tables in the default
database
DROP TABLE tableName - deletes a table

DESCRIBE tableName - describes the details for a table


ALTER TABLE tableName - modifies a table, e.g., ADD
COLUMN and DROP COLUMN
ALTER TABLE tableName ADD columnDefinition
ALTER TABLE tableName DROP columnName
ALTER TABLE tableName ADD FOREIGN KEY (columnNmae) REFERENCES
tableName (columnNmae)
ALTER TABLE tableName DROP FOREIGN KEY constraintName
Here are the list of commands for creating a database:
C. Row-Level
INSERT INTO tableName
VALUES (column1Value, column2Value,...) - Inserts on all Columns
INSERT INTO tableName
VALUES (column1Value, column2Value,...), ... - Inserts multiple rows
INSERT INTO tableName (column1Name, ..., columnNName)
VALUES (column1Value, ..., columnNValue) - Insert on selected
Columns
DELETE FROM tableName WHERE criteria
UPDATE tableName SET columnName = expr, ... WHERE criteria
SHOW DATABASES
You can use SHOW DATABASES to list all the existing databases in
the server.

mysql> SHOW DATABASES;


1. Create a database named Southwind:

2. Set the default database to Southwind.


3. We will create a table named “products” with the following column
properties:
Explanations:
• 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.

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.
Explanations:
• quantity is also INT UNSIGNED (non-negative integers).
• price is DECIMAL(10,2) - a decimal number with 2 decimal places.

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.
Explanations:

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.

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.
4. To create the “products” table, type:

If the command is successful, this message will appear:


5. To insert a row with values, type:

If the command is successful, the “Query OK” message will appear.

6. Inserting multiple rows in one command


(Inserting a NULL to the auto_increment column results in max_value + 1
7. To insert values to selected columns:
( Missing value for the auto_increment column also results in max_value + 1 )

8. To list all rows/records in the “products” table:


9. To list all rows for the specified columns:

10. To delete a particular row or record:


11. To list a row based on a specified criteria:
12. Adding a new column to a table:
END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy