mock lecture in mysql

24
Mock Lecture in MySQL

Upload: katima

Post on 15-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

Mock Lecture in MySQL. MySQL. Is a relational database management system (RDBMS) – a system that organizes data into tables that are related to each other. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Mock Lecture in MySQL

Mock Lecture in MySQL

Page 2: Mock Lecture in MySQL

MySQL

Is a relational database management system (RDBMS) – a system that organizes data into tables that are related to each

other.

– is an open-source database management system, which is available on a variety of platforms, including Windows, Mac OS X, and various distributions of Linux and Unix. It is centered around MySQL Server, which is the component that manages data and executes SQL statements that are issued to it. MySQL Server provides services to other programs, such as stand-alone applications with graphical user interfaces, web servers, and application servers.

Page 3: Mock Lecture in MySQL

MySQL

The MySQL software consists of the MySQL server, several utility programs, and some supporting software.

The MySQL server is the database manager. It runs all the time, in the background, waiting for instructions.

Page 4: Mock Lecture in MySQL

Database

The database is a set of files that contain the data.

You can create new databases, add data to databases, and retrieve data from a database by communicating with the server – sending messages that the server executes.

Page 5: Mock Lecture in MySQL

Communicating with the MySQL Server

You can manage your database, retrieve data from it, or add data to it by sending messages to the MySQL server.

You can communicate using SQL (structured query language), a language developed specifically to interact with the databases.

You build and send a SQL message, called query, to the MySQL server

Page 6: Mock Lecture in MySQL

Communicating with the MySQL Server

The server responds by performing the action defined by the query or, if the server is unable to perform the requested operation, it returns an error message with information about the problem.

Page 7: Mock Lecture in MySQL

The Database Structure

MySQL is an RDBMS, which means that the data is organized into tables. Each database can have many tables. Database tables are organized like other tables that you are familiar with – in rows and columns. Columns are called fields, and rows are called records.

Page 8: Mock Lecture in MySQL

The Database Structure

The focus of each table is an object, also called an entity, that you want to store information about, such as customer, a product or an order.

Each entity has attributes. In the table, each row represents an entity, and the columns contain the attributes for each entity.

– Example: in a table of customers, each row contains information for a single customer, such as name, address and phone number.

Page 9: Mock Lecture in MySQL

The SQL Language

SQL is a simple, English-like language that you can learn quickly.

– Example: a SQL query that retrieves all the data from a database table isSELECT * FROM Customer

The first word of each query is its name, which specifies the action to perform. Some commonly used queries are

CREATE, SELECT, DELETE, INSERT, UPDATE, and ALTER. The query name is followed by words and phrases that defines

the action, such as the table name to be updated or the name of the database to be created.

Page 10: Mock Lecture in MySQL

The SQL Language

NOTE: – SELECT is the same as select or Select to SQL. – However, the parameters, such as table names,

must use the correct case if you are on a Linux system. To Linux, Customer is not the same as customer.

– On the other-hand, Windows, is not case sensitive.

Page 11: Mock Lecture in MySQL

Send a Query with the mysql Client

When the MySQL is installed, a simple, text-based client program is also installed, called mysql.

The program accepts and sends SQL queries to the MySQL server for execution.

The response is returned to the client and displayed onscreen.

Page 12: Mock Lecture in MySQL

Send a Query with the mysql Client

The mysql client must be started from the command line. That is you must be in a command prompt window on Windows or a terminal window on Linux and Mac.

Page 13: Mock Lecture in MySQL

DEMO

1. Open a command prompt window2. Change directory from drive Z:\ to drive C:\3. Type:

Z:\>C: <enter>

4. From C:\5. Type:

C:\>cd\mysql\bin <enter>

Page 14: Mock Lecture in MySQL

DEMO

C:\mysql\bin> To connect to the mysql server type:

C:\mysql\bin>mysqld-nt --console Then, open another command prompt window do

step 1-5 again. To change to mysql prompt type

C:\mysql\bin>mysql –u root

Page 15: Mock Lecture in MySQL

DEMO

mysql prompt

Note: • The prompt tells you that mysql is ready for you to enter commands.

Page 16: Mock Lecture in MySQL

Prompt Meaning

mysql> Ready for new command. -> Waiting for next line of multiple-line command. '> Waiting for next line, collecting a string that begins

with a single quote (‘). "> Waiting for next line, collecting a string that

begins with a double quote (“).

Note: • To send a query, type the entire query at the mysql client prompt. • Each query should end with a semicolon• If you do not end the query with a semi-colon, the mysql client displays new prompt and waits for more inputs. It does not send query until you type a semicolon• Type \c to clear the buffer

Page 17: Mock Lecture in MySQL

SQL COMMANDS

SELECT – retrieve information from a database.

Syntax: SELECT sql command

Example #1: SELECT version();

#2: SELECT current_date;

#3: SELECT user(), curdate();

Syntax: SELECT column1, column2,…. FROM tablename

Example #1: SELECT * FROM myTB;

#2: SELECT num, name, bdate FROM myTB;

Page 18: Mock Lecture in MySQL

SQL COMMANDS

SHOW – retrieve information about the structure of a database or table.

CREATE – create a table or database. USE – to select/use existing database. DESCRIBE – reveal the structure of a table. INSERT – add a row to a table. UPDATE – modify a database entry. DELETE – delete rows from a table. DROP – delete entire tables or databases.

Page 19: Mock Lecture in MySQL

SAMPLE TABLE

users Table

Column Name Type

user_id number

first_name text

last_name text

email text

password text

registration_date date/time

The users table with generic data types

users Table

Column Name Type

user_id MEDIUMINT

first_name VARCHAR

last_name VARCHAR

email VARCHAR

Password CHAR

registration_date DATETIME

The users table with more specific data types

Page 20: Mock Lecture in MySQL

SAMPLE TABLE

users Table

Column Name Type

user_id MEDIUMINT

first_name VARCHAR(15)

last_name VARCHAR(30)

email VARCHAR(40)

Password CHAR

registration_date DATETIME

The users table with set length attributes

CHAR vs. VARCHAR

• Both of these types store strings and can be set with a fixed maximum length. One primary difference between the two is that anything stored as a CHAR will always be stored as string the length of the column (using spaces to pad it). • Conversely, VARCHAR strings will be only as long as the stored string itself.

Page 21: Mock Lecture in MySQL

CREATING A DATABASE

mysql> create database myDB;

Page 22: Mock Lecture in MySQL

CREATING A TABLE

Page 23: Mock Lecture in MySQL

ALTERING TABLES

The ALTER SQL term is primarily used to modify the structure of a table in your database.

The ALTER statement can even be used for renaming the table as a whole.

Syntax:– ALTER TABLE <table name> CLAUSE

Page 24: Mock Lecture in MySQL

ALTERING TABLES CLAUSES

Clause Usage Meaning

ADD COLUMN

ALTER TABLE table_name ADD COLUMN column_name varchar(40)

Adds a new column to the end of the table

CHANGE COLUMN

ALTER TABLE table_name CHANGE COLUMN column_name VARCHAR(60)

Allows you to change the data type and properties of a column.

DROP COLUMN

ALTER TABLE table_name DROP COLUMN column_name

Removes a column from a table, including all of its data.

RENAME AS

ALTER TABLE table_name RENAME AS new_tablename

Changes the name of a table