0% found this document useful (0 votes)
1 views43 pages

mysql introduction

MySQL is an open-source database management system used for storing and managing data, known for its speed, reliability, and ease of use. It includes tools like the mysql Command-Line Utility, MySQL Administrator, and MySQL Query Browser for database interaction and administration. Key concepts include database structure, data types, table creation, and manipulation commands such as INSERT, UPDATE, and DELETE.

Uploaded by

jeba
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)
1 views43 pages

mysql introduction

MySQL is an open-source database management system used for storing and managing data, known for its speed, reliability, and ease of use. It includes tools like the mysql Command-Line Utility, MySQL Administrator, and MySQL Query Browser for database interaction and administration. Key concepts include database structure, data types, table creation, and manipulation commands such as INSERT, UPDATE, and DELETE.

Uploaded by

jeba
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/ 43

MySQL

Unit 4
What is MySQL
• MySQL is a Database management
System which is used to storing,
retrieving, managing and
manipulating data.
Effectiveness of MySQL
• Cost:
▫ MySQL is open-source, and is usually free to
use (and even modify) the software without
paying for it.
• Performance:
▫ MySQL is fast
• Trusted:
▫ MySQL is used by some of the most important
and prestigious organization and sites, all of
whome entrust it with their critical data.
• Simplicity:
▫ MySQL is easy to install and get up and running.
MySQL Tools
• MySQL is a client-server DBMS.
• It needs a client application to
interact MySQL.
• Three of them are
▫ mysql Command-Line Utility.
▫ MySQL Administrator.
▫ MySQL Query Br0wser.
mysql Command-Line Utility
• mysql is a command-line client tool that is
installed as standard with the MySQL package.
• Typing mysql command in command prompt will
display the following output:
Welcome to the MySQL monitor.
Commands end with ; or \g. Your
MySQL connection id is 234343 to
server version: 5.0.27 Type 'help;'
or '\h' for help. Type '\c' to clear
the buffer.
mysql>
• Commands are typed after the mysql>
prompt
• Commands ends with ; 0r \g;(just
pressing enter will not work)
• Type 'help;' or '\h' for help.
• Type quit or exit to quit command
line utility
MySQL Administrator
• graphical interactive client designed to simplify the
administration of MySQL Servers
• displays icons for selection of different views
• Server information:
▫ displays status and version informaion about the
server and the client.
• Service control:
▫ to stop and start MySQL
• User administration:
▫ to define MySQL users, logins and previleges.
• Catalogs
▫ lists available databases
▫ create new databases and tables
MySQL Query Browser

• graphical interactive client used to


write and execute MySQL commands.
• must be downloaded.
• It prompts for server and login
information and displays application
interface.
• MySQL commands are typed on top of the
screen. click execute button for processing.
• results are displayed in a grid in left of the
screen
• all datasources are displayed at right of the
screen
• MySQL Query browser will automatically write
MySQL statements when the selects tables and
columns.
• history of executed MySQL statements are
maintained.
• Help on MySQL Syntax, functions are available
Prerequisites for MySQL connection
• The hostname (name of the
computer)-localhost if connection to
a locale server
• The port
• A valid username
• The user password
Databases Basics
• Database is a collection of data stored in
some organized fashion.
• a table is a structured file that can store
data of a specific type. Every table in a
database have unique name.
• set of information that describes a table is
known as schema.
• Column: A single field in a table. Each
column has an associated datatype.
• Row: a record in a table. each record saved
is stored in its own row.
• Primary key : A column whose values
uniquely identify every row in a table
Selecting a database
• When first connected to MySQL, no databases
are open to use. To select a database, the
keyword USE is used. It does not return any
results.
• Input:
▫ use crashcourse;
• Output:
▫ Database changed.
Databases and tables
• Information about the databases, tables,
columns, users, previleges are stored within the
databases and tables themselves.
• MySQL SHOW command is used to display this
information.
• Ex: SHOW DATABASES; returns list of available
databases. Database
mysql
forta
flex
Test
crashcourse
show commands
• SHOW TABLES; returns list of available
tables in the currently selected databases.
• Output:

Tables_in_crashcourse

customers
orderitems
orders
products
productnotes
vendors
Show commands
• SHOW COLUMNS FROM <tablename>;
▫ It returns a row for each field containing the fieldname,
its datatype, whether null is allowed, key information,
default value and extra information.
• Ex: SHOW COLUMNS FROM customers;

Field Type Null Key Default Extra


cust_id int(11) NO PRI NULL auto_increment
cust_name char(50) NO
cust_city char(50) YES NULL
cust_email char(50) YES NULL
cust_phone char(50) YES NULL
Show commands
• SHOW STATUS: to display extensive server
status information.

• SHOW GRANTS: used to display security rights


granted to users.

• SHOW ERRORS and SHOW WARNINGS: used


to display server error or warning messages
MySQL data types
• Datatypes are rules that define what data may be
stored in a column and how that data is actually
stored.
• Major MySQL datatypes are
▫ String
▫ Numeric
▫ Date and time
▫ Binary
String Datatypes

• Fixed-length strings :
▫ fixed number of characters.
• variable-length strings:
▫ text of variable length

• manipulate fixed length faster than variable


length.
String datatypes
Datatype Description
Char Fixed-length string from 1- 255 Chars long. size must be
specified at create time.

VARCHAR same as Char but the size is maximum.


Enum Accepts one of a predefined set of upto 64K strings.
SET Accepts zero or more of a predefined set of upto 64K strings.
TEXT variable length string with the maximum size of 64 K
MEDIUMTEXT TEXT with maximum size of 16K
TINYTEXT TEXT with maximum size of 255 BYTES
LONGTEXT TEXT with maximum size of 4GB
Numeric Datatypes
Datatype Description
BIT A bit field, from 1-64 bits wide.
BOOLEAN Boolean flag, either 0 or 1
INT (or INTEGER) Integer value support numbers from -2147483648 to
2147483647.( if unsigned 0 to 4294967295)
BIGINT Integer value, supports numbers from
-9223372036854775808 to 9223372036854775807
MEDIUMINT Integer value, supports numbers from
-838808 to 8388607
SMALLINT Integer value, supports numbers from
-32768 to 32767
TINYINT Integer value, supports numbers from
-128 to 127
Numeric datatypes(cont..)

DATA TYPE DESCRIPTION

DECIMAL Floating point values with varying levels of precision.

DOUBLE Double-precision floating point values.

FLOAT Single-precision floating point values.

REAL 4-byte floating point values


Date and time Data types: For the
storage of date and time values.
Datatype

DATE 1000-01-01 TO 9999 -12-31 IN THE format yyyy-mm-dd

DATETIME Combination of Date and time

TIMESTAMP Equal DATETIME( smaller range)

TIME HH:MM:SS

YEAR 2 or 4 digit year two digit year(70(1970) to 69(2069))


4 digit year range from1901 to 2155
Binary Datatypes: to store graphics images,
multimedia and wordprocessor documents.

BLOB Blob with a maximum length of 64K


(Binary Large Object)

MEDIUMBLOB Blob with a maximum length of 16MB

LONGBLOB Blob with a maximum length of 4GB

TINYBLOB Blob with a maximum length of 255bytes


Creating and manipulating
tables
Two ways to create database tables

• Using an administration tool: the


interface generates and executes
the MySQL seamlessly.
• With MySQL Statements :
CREATE TABLE SQL statement
is used.
Information required to create a table

• Name of the new table


• Name and definition of the table
columns.
Syntax:
CREATE TABLE tablename
(
column datatype [NULL/NOT NULL]
[CONSTRAINTS],
column datatype [NULL/NOT NULL]
[CONSTRAINTS],
...
);
• Table definitions are enclosed within
paranthesis.
• Columns are separated by commas(,)
• Each column definition starts with column name
followed by datatype.
• Primary keys are specified by PRIMARY KEY
keyword.
• The entire statement is terminated with
semicolon(;).
eXAMPLE
CREATE TABLE customers
(
cust_id int NOT NULL AUTO_INCREMENT,
cust_name char(50) NOT NULL,
cust_city char(50) NOT NULL DEFAULT
TVL,
cust_email char(50) NOT NULL,
PRIMARY KEY(cust_id)
)ENGINE =InnoDB;
cust_id cust_name cust_city cust_email

NULL VALUES:
A column that allows NULL values also allows rows to be inserted with no values
for that column.

A column that does not allow null values does not accept rows with no values.

PRIMARY KEYS:
Primary key values must be unique.

AUTO_INCREMENT:
To generate unique numbers when a new row inserted.

DEFAULT VALUES:
Default values are used if no value is specified when a row is inserted.
Engines

• MySQL has an internal engine that


actually manages and manipulates data.
• MySQL supports several engines with
different capabilities.
• The default engine is MyISAM
Engine Types
• InnoDB: Transaction safe engine. Does
not support full-text searching.
• MEMORY: Data is stored in memory
instead of disk and so it is fast. It is
suitable for temporary table.
• MyISAM: high performance engine. It
supports full-text searching.
Updating Tables
• To update table definitions ALTER TABLE
statement is used.
• It needs
▫ Name of the table to be altered.
▫ List of changes to be made
• Syntax:
ALTER TABLE tablename
(
ADD column datatype [NULL|NOT NULL] [CONSTRAINS],
CHANGE column columns datatype [NULL|NOT NULL]
[CONSTRAINS],
DROP COLUMN,

);
EXAMPLES
1. Add a column
ALTER TABLE vendors ADD vend_phone
CHAR(20);
2. Remove a column
ALTER TABLE vendors ADD vend_phone
CHAR(20);
3. Add a constrain
ALTER TABLE orderitems ADD CONSTRAIN
fk_orderitems_products FOREIGN KEY
(prod_id) REFERENCES customers (cust_id);
Steps to change complex structure
• Create a new table with new column layout.
• Use INSERT SELECT to copy the data from old
table to new tble.
• Verify the new table
• Rname the old table
• Rename the new table with the name previously
used by the old table
• Re-create any triggers, stored procedures,
indexes and foreign keys as needed.
DELETING Tables
• Tables are deleted using DROP TABE statement
Syntax:
DROP TABLE tablename;
Example:
DROP TABLE customer2;
RENAMING TABLES
• RENAME TABLE statement is used.
Syntax:
RENAME TABLE oldname to newname;
Example:
RENAME TABLE customers2 to customers;
INSERTING DATA
• INSERT can be used in many ways
▫ To insert a single complete row
▫ To insert a single partial row
▫ To insert multiple rows
▫ To insert the results of a query
Syntax
INSERT INTO table_name
(column_1,column_2,...)
VALUES (value_1,value_2,...);

Required Information:
• tablename
• values to be inserted.
Insert complete rows
Name address city state zip contact

Insert into customers (Name, address,


city, state, zip, contact) values (‘John’,
‘Perumalpuram’, ‘Tirunelveli’,
‘Tamilnadu’, 627005, 234908);
Inserting Multiple rows
Insert into customers (Name, address, city, state,
zip, contact)
VALUES (‘John’, ‘Perumalpuram’, ‘Tirunelveli’,
‘Tamilnadu’, 627005, 234908), (‘Jessi’,
‘Weststreet’, ‘Alankulam’, ‘Tamilnadu’, 627135,
235678);
Inserting Retrieved data
Insert into customers
(Name, address, city, state, zip, contact)
SELECT
Cust_name, cust_addr, cust_city, cust_state, zip,
city
FROM custnew;

You might also like