day 3 - basics of mysql what is mysql what is mysql how to make basic tables how to make basic...

8
Day 3 - Basics of MySQL Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic How to make basic tables tables Simple MySQL Simple MySQL commands. commands.

Upload: cynthia-cooper

Post on 17-Dec-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands

Day 3 - Basics of MySQLDay 3 - Basics of MySQL

What is MySQLWhat is MySQL How to make basic tablesHow to make basic tables Simple MySQL Simple MySQL commands.commands.

Page 2: Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands

CSE 498 Day 3 - Basics of MySQL 2 / 8

What is MySQL?What is MySQL?

MySQL is a true multi-user, multi-threaded SQL MySQL is a true multi-user, multi-threaded SQL database server. SQL is the most popular database server. SQL is the most popular relational database in the world.relational database in the world.

Goals of system:Goals of system:– Client/server implementation of a relational database Client/server implementation of a relational database

system.system.– Speed of transactions and queries going through.Speed of transactions and queries going through.– Robustness of the system.Robustness of the system.– Easy to use and learn.Easy to use and learn.– Be able to handle large systems easily and quickly.Be able to handle large systems easily and quickly.

Page 3: Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands

CSE 498 Day 3 - Basics of MySQL 3 / 8

Relational DBRelational DB– A relational database is the reality of a E/R diagram model. A relational database is the reality of a E/R diagram model.

It uses the basic parts of the E/R diagram and enforces them It uses the basic parts of the E/R diagram and enforces them in a database.in a database.

Basic PartsBasic Parts– Keys & Attributes are the same Keys & Attributes are the same – Table: Entity relation of a E/R diagram.Table: Entity relation of a E/R diagram.– Record: An instance of a row of data in a table.Record: An instance of a row of data in a table.

What is a relational database?What is a relational database?

Page 4: Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands

CSE 498 Day 3 - Basics of MySQL 4 / 8

Data Types & BasicsData Types & Basics

Data TypesData Types– Integers: TinyInt, SmallInt, MediumInt, Int, BigIntIntegers: TinyInt, SmallInt, MediumInt, Int, BigInt– Real Numbers: Float, Double, Decimal, RealReal Numbers: Float, Double, Decimal, Real– Time: Date, DateTime, Timestamp, Time, Year (Y2K Compliant)Time: Date, DateTime, Timestamp, Time, Year (Y2K Compliant)– String: Char, VarCharString: Char, VarChar– Text: TinyText, MediumText, LongTextText: TinyText, MediumText, LongText

Basic MySQL CommandsBasic MySQL Commands– Note: All commands/queries end with a semi-colon.Note: All commands/queries end with a semi-colon.– show tables;show tables;

• Command will show you the name of all the tables currently in the DB.Command will show you the name of all the tables currently in the DB.

– desc <table name>;desc <table name>;• Command will show you information about the requested table.Command will show you information about the requested table.

Page 5: Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands

CSE 498 Day 3 - Basics of MySQL 5 / 8

CREATE TABLE commandCREATE TABLE command

Command for creating a table in the DBCommand for creating a table in the DB– CREATE TABLE table_name (attribute definition, …)CREATE TABLE table_name (attribute definition, …)– attribute definition : column_name data_type (options)attribute definition : column_name data_type (options)– options : NOT NULL/NULL, AUTO INCREMENT, options : NOT NULL/NULL, AUTO INCREMENT,

PRIMARY KEY, DEFAULT default_value,PRIMARY KEY, DEFAULT default_value,

(reference_definition)(reference_definition)– reference_definition : reference_definition :

REFERENCE table_name (column_name, …) REFERENCE table_name (column_name, …)

MATCH FULL/MATCH PARTIALMATCH FULL/MATCH PARTIAL

Example: Category table from Day 2Example: Category table from Day 2 > CREATE TABLE category (cat_id INT NOT NULL PRIMARY KEY, cat_name var_char NOT > CREATE TABLE category (cat_id INT NOT NULL PRIMARY KEY, cat_name var_char NOT

NULL);NULL);

Page 6: Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands

CSE 498 Day 3 - Basics of MySQL 6 / 8

ALTER TABLE & DROP TABLE CommandALTER TABLE & DROP TABLE Command

ALTER TABLE - Used to change a table.ALTER TABLE - Used to change a table.– ALTER TABLE table_name (alter_spec)ALTER TABLE table_name (alter_spec)– Three basic alter_specs:Three basic alter_specs:

• ADD : Used to add a column, index specification, or primary key ADD : Used to add a column, index specification, or primary key specificationspecification

• CHANGE: Used to change a column specification in the tableCHANGE: Used to change a column specification in the table• DROP: Used to drop a column from the table.DROP: Used to drop a column from the table.

– Example: Add a column to the category table.Example: Add a column to the category table. > ALTER TABLE category ADD cat_alias var_char NULL;> ALTER TABLE category ADD cat_alias var_char NULL;

DROP TABLE - Used to delete a table.DROP TABLE - Used to delete a table.– DROP TABLE table_nameDROP TABLE table_name– Note: Be careful with this one because once it’s gone, it’s Note: Be careful with this one because once it’s gone, it’s

gone.gone.

Page 7: Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands

CSE 498 Day 3 - Basics of MySQL 7 / 8

Used to insert data into tables.Used to insert data into tables. FormatFormat

– INSERT INTO table_name (col_name, …) VALUES INSERT INTO table_name (col_name, …) VALUES (expression, …), (expression, …), …(expression, …), (expression, …), …

Example: Insert info into the category table…Example: Insert info into the category table… > INSERT INTO category (cat_id, cat_name) VALUES (1, “Action/Adventure”);> INSERT INTO category (cat_id, cat_name) VALUES (1, “Action/Adventure”);

Insert CommandInsert Command

Page 8: Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands

CSE 498 Day 3 - Basics of MySQL 8 / 8

DELETE & UPDATE CommandsDELETE & UPDATE Commands

Both commands use a query format to run the Both commands use a query format to run the command on the data in a table. Queries are command on the data in a table. Queries are explained in the next set of slides.explained in the next set of slides.

DELETE - Used to delete information from a table.DELETE - Used to delete information from a table.– DELETE FROM table_name WHERE (rule)DELETE FROM table_name WHERE (rule)– Example: Delete the ‘oldies’ category from the table.Example: Delete the ‘oldies’ category from the table.> DELETE FROM category WHERE cat_name = “Oldies”;> DELETE FROM category WHERE cat_name = “Oldies”;

UPDATE - Used to update table information.UPDATE - Used to update table information.– UPDATE table_name SET col_name = expression WHERE (rule)UPDATE table_name SET col_name = expression WHERE (rule)– Example: Change category “Scary Movies” to “Horror”Example: Change category “Scary Movies” to “Horror”> UPDATE category SET cat_name = “Horror” WHERE cat_name = “Scary Movies”;> UPDATE category SET cat_name = “Horror” WHERE cat_name = “Scary Movies”;