mock lecture in mysql
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 PresentationTRANSCRIPT
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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>
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
DEMO
mysql prompt
Note: • The prompt tells you that mysql is ready for you to enter commands.
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
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;
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.
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
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.
CREATING A DATABASE
mysql> create database myDB;
CREATING A TABLE
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
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