Mysql
Mysql
CONTENTS
1. Introduction 139
2. Constraints 141
4. Views 145
An ISO 9001 : 2000 Recognised Institution
INTRODUCTION TO MYSQL
MySql is a relational database management system (RDBMS) developed by MySql AB later purchased by sun
micro systems. mysql is client and mysqld is server.
To connect to database (mysql -h host_name -p -u user_name)
>mysql –p –u root (note: no semicolon at end of the statement);
Create user and give permissions
mysql> create user ‘username’@’localhost’ identified by ‘password’;
mysql > grant all on databasename.* to ‘username’@’localhost’;
To set password
mysql> SET PASSWORD FOR root@localhost=PASSWORD(‘root’);
To display time ,user and version
mysql> select now(),user(),version();
To execute sql script files
> mysql -p -u username databasename< myscript.sql
To use the database
mysql> use databasename;
To display the tables in database
mysql>show tables ;
To display table structure
mysql>describe tablename;
DATA TYPES
Numeric data types
Type Name Meaning
TINYINT A very small integer
SMALLINT A small integer
MEDIUMINT A medium-sized integer
INT A standard integer
BIGINT A large integer
DECIMAL A fixed-point number
FLOAT A single-precision floating-point number
DOUBLE A double-precision floating-point number
MySQL 139
An ISO 9001 : 2000 Recognised Institution
MySQL 140
An ISO 9001 : 2000 Recognised Institution
CONSTRAINTS
Foreign keys
[CONSTRAINT constraint_name]
FOREIGN KEY [fk_name] (index_columns)
REFERENCES tbl_name (index_columns)
[ON DELETE action]
[ON UPDATE action]
FOREIGN KEY indicates the indexed columns in the child table that must match index values in the parent
table.fk_name is the foreign key ID
REFERENCES names the parent table and the index columns in that table to which the foreign key in the child
table refers.The index_columns part of the REFERENCES clause must have the same number of columns as
the index_columns that follows the FOREIGN KEY keywords
ON DELETE CASCADE causes matching child rows to be deleted when the corresponding parent row is deleted.
In essence, the effect of the delete is cascaded from the parent to the child.This enables you to perform multipletable
deletes by deleting rows only from the parent table and letting InnoDB take care of deleting corresponding rows
from the child table.
Corresponding columns in the parent and child indexes must have compatible types. For example, you cannot
match an INT column with a CHAR column.
Example:
CREATE TABLE parent
(
par_id INT NOT NULL,
PRIMARY KEY (par_id)
) ENGINE = INNODB;
MySQL 141
An ISO 9001 : 2000 Recognised Institution
MySQL 142
An ISO 9001 : 2000 Recognised Institution
SQL QUERIES
Create database Create database if not exists db_name;
If not exists used to check the whether database already exists .
Drop database Drop database db_name;
Create table Create table table_name ;
Drop table Drop table table_name;
Alter table Alter table tbl_name action [, action] ...
Ex: alter table mytbl modify id int;
Ex: alter table mytbl change id id2 int; (change modifies the column name also);
SELECT STATEMENT
SELECT select_list # What columns to select
FROM table_list # The tables from which to select rows
WHERE row_constraint # What conditions rows must satisfy
GROUP BY grouping_columns # How to group results
ORDER BY sorting_columns # How to sort results
HAVING group_constraint # What conditions groups must satisfy
LIMIT count; # Row count limit on results
MySQL 143
An ISO 9001 : 2000 Recognised Institution
Select IDs for grade event rows that correspond to tests (‘T’)
SELECT * FROM score
WHERE event_id IN (SELECT event_id FROM grade_event WHERE category = ‘T’);
The presidents who were born in the same city and state as John Adams:
mysql> SELECT last_name, first_name, city, state FROM president
WHERE (city, state) = (SELECT city, state FROM president
WHERE last_name = ‘Adams’ AND first_name = ‘John’);
Students who are absent
SELECT * FROM student
WHERE student_id IN (SELECT student_id FROM absence);
MySQL 144
An ISO 9001 : 2000 Recognised Institution
VIEWS
Create View
CREATE VIEW vpres AS
SELECT last_name, first_name, city, state FROM president;
Transactional systems typically are characterized as providing ACID properties. ACID is an acronym for Atomic,
Consistent, Isolated, and Durable, referring to four properties that transactions should have:
Atomicity: The statements a transaction consists of form a logical unit.You can’t have just some of them
execute.
Consistency: The database is consistent before and after the transaction executes. In other words, the
transaction doesn’t make a mess of your database.
Isolation: One transaction has no effect on another.
Durability: When a transaction executes successfully to completion, its effects are recorded permanently in
the database.
MySQL 145