Chapter-13 14 MySQL Class 12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

MYSQL REVISION

TOUR
Introduction
 Database
 Collection of data
 Contains information about one particular
enterprise

 Disadvantages
 Data Redundancy (duplications of data)
 Data inconsistency
 Unsharable data
 Unstandardized data
 Insecure data
 Incorrect data
Database Management
System
 Advantages of Database System
 Reduces data redundancy
 Control Data Inconsistency

 Sharing of data

 Enforces standards

 Centralized databases ensures data security

 Integrity can be maintained throughout


the databases
Relational Data Model

 Data model
 set of concepts to describe the structure
of a database and certain constraints that
the database should obey.

 Most commonly used data model


 relational data model
Terms in Relational Model
 Relation  View
 Stores logically related data  Virtual Table
 Data must be atomic in a cell  Derived from one or more
underlying base table(s).
 Domain
 Pool of valyes in a column  Primary Key
Set of one or more attributes that
Tuple


uniquely identifiy tuples(Rows) in a
 Row of relation table
 Attribute  Candidate Key
 Column of a relation  All attribute combination that can
serve as Primary Key
 Degree
 Number of  Alternate Key
attributes(Column)  A Candidate key that is not a
primary key
 Cardinality
 Number of Tuples (Row)  Foreign Key
 Non key attribute whose values are
derived from primary key of some
other table.
Referential Integrity

 It is a system of rules that a DBMS uses


to ensure the relationship between
records in related tables.
MySQL

 It is a freely available open source


RDBMS that uses SQL
 Information is stored in Tables
 Fast, reliable and scalable
 MySQL database refers to the
combination of MySQL server instance
and a MySQL Database
Client/Server Architecture
MySQL and SQL

 Set of commands that is recognized by


nearly all RDBMSs.
 Structured Query Language
 Language that enables us to create and
operate on relational databases,
 which are sets of related information stored in
tables
Classification of SQL
Statements
 Data Definition  Transaction Control
Language (DDL) Language(TCL)
 Perform tasks related  Manage and control
to data definition transactions
 Creating, altering,  Making changes to
dropping database
 Granting and revoking  Undoing changes to
privilages and roles database
 Maintenance  Creating savepoints
commands  Setting properties for
 Data Manipulation current transactions
Language(DML)
 Perform data
manipulation
 Retrieval
 Insertion
 Deletion
 Modification
Common MySQL DataTypes
Data Type Spec
CHAR
VARCHAR String (0-255)
TINYTEXT
TEXT
String(0-65535)
BLOB
MEDIUMTEXT
String(0-16777215)
MEDIUMBLOB
LONGTEXT
String(0-4294967295)
LONGBLOB
TINYINT Integer(-128 to 127)
SMALLINT Integer(-32768 to 32767)
MEDIUMINT Integer(-8388606 to
8388607)
Data Type Spec
INT Integer(-2147483648 to
2147483647)
BIGINT Integer(-
9223372036854775808 to
9223372036854775807)
FLOAT Decimal precise to 23 digits
DOUBLE Decimal (24 to 53 digits)
DECIMAL “DOUBLE”stored as string
DATE YYYY-MM-DD
DATETIME YYYY-MM-DDHH:MM:SS
TIMESTAMP YYYYMMDDHHMMSS
TIME HH:MM:SS
ENUM One of preset options
SET Selection of preset options
Difference between Char and
Varchar
Char Varchar

 Fixed length  Variable length

Blank spaces are  Value stored exactly as



you give.
given to the extra
No blank spaces for
spaces after the 
remaining length
given value.
 Occpies only the
 Occupies entire required spaces and
length specified frees out the memory
Accessing database

 Before making queries, or even create a


table., a database has to be used.

 The command used is


 USE<database name>;
Creating Tables in MySQL

 Tables are defined using CREATE TABLE


command.

 Viewing Structure of a Table


 DESC table-name;

 CREATE TABLE table-name


Ø (column-name datatype size,
Ø column-name datatype size,

Ø column-name datatype size ......);


Creating Table with
Constraints
 Constraint is a condition or check
applicable on a field or set of fields.
 Common types of constraints:

S.No Constraints Description

1 NOT NULL A column cannot have NULL value.


Default value for a column when none is
2 DEFAULT
specified
3 UNIQUE All are values in a column are different

4 CHECK Makes the criteria for the values

5 Primary Key Uniquely identify a row in a table

6 Foreign Key Ensure referential integrity of the data


Create a table with the
following constraints:

Column name Type Constraint


Exam_Numbe Integer Primary Key
r
Student_ID Variable Unique
Character
Last_Name Variable Should not be a NULL value
Character
First_Name Variable
Character
Age Integer Also it should be within the range
16-19
Class Variable Set the Value “Grade-12” if no
character value is given
CREATE TABLE Student (
-> ExamNo Integer PRIMARY KEY,
-> StudId Varchar(10) UNIQUE,
-> LastName Varchar (20) NOT NULL,
-> FirstName Varchar(20),
-> Age Integer CHECK ((Age>=16) AND
(Age<=19)),
-> Class Varchar(10) DEFAULT 'Grade-
11');
Creating Foreign Key

 Relating 2 tables.
 Create table marks
Ø ( MID integer,
Ø ExNo integer,

Ø Total integer,

Ø Result varchar(5),

Ø Foreign Key(ExNo) references STUDENT


(Exam_Number) );
Adding a Foreign Key to the
existing Table
 A new Foreign Key can be added to the
existing Table with the ALTER TABLE
command,

 ALTER TABLE <table-name> ADD


FOREIGN KEY (<colum name that has to
be designated as foreign key> )
references <master-table> (primary key
of the master table) );
Inserting data into Value
 Rows are added to relations using INSERT
command of SQL.
 INSERT INTO table-name [<column-list>]
VALUES (<value-list>);

 If the column order is well-known then the


column list can be omitted
 INSERT INTO table-name VALUES (<value-
list>);

 Dates are by default entered as


 ‘YYYY-MM-DD’
SELECT Command
 Selecting all data
 SELECT * FROM table-name;
 Selecting particular rows
 SELECT * FROM table-name WHERE conditions;
 Selecting particular columns
 SELECT <column-name(s)> FROM table-name;
 Eliminating Redundant Data
 The DISTINCT keyword eliminates duplicate
results from the result
 SELECT DISTINCT column-name FROM table-name;
 Selecting from All the Rows
 If DISTINCT is replaced with the keyword ALL ,
then the result retains he duplicate output rows.
 SELECT ALL <column-name> FROM <table-name>;
Performing Simple
Calculations
 To perform simple calculations, write
the expression/formula.

 SELECT 3*9*6;

 SELECT curdate();
Using Column Aliases

 The columns that are selected in a query


can have different names for the output
purposes

 Syntax:
 SELECT<column-name> AS <alias-name>
FROM <table-name>;
Condition based on a Range

 BETWEEN is used to define a range of


values taht the column must fall in to
make the condition TRUE.

 SELECT* FROM table-name


WHERE column-name BETWEEN start
AND end;
Condition based on a List

 IN and NOT IN operator is used

 SELECT * FROM table-name WHERE


<column-name> IN (‘value1’, ‘value2’,
‘value3’);

 SELECT * FROM table-name WHERE


<column-name> NOT IN (‘value1’, ‘value2’,
‘value3’);
Condition Based on Pattern
matches
 String matching operator LIKE
 Percent(%) matches any substring
 SELECT * FROM table-name WHERE name
LIKE ‘%y’;
 Ending with y

 Underscore(_ ) the _ character matches


any character
Searching for NULL

 The NULL value in a column can be


searched in a table using IS NULL, in
the WHERE Clause.
 SELECT* FROM table-name WHERE
column-name IS NULL;
Modifying Data in Table
 Data can be modified in the table using
UPDATE command
 UPDATE <table name> SET <column name> =
<value>;
 This command will change the entire column
with the given value

 To change the value of the column for a


particular column alone, then
 WHERE clause and the new data using SET
keyword, can be used
 UPDATE <table name> SET <column name> =
<value> WHERE (conditions);
Deleting data from Tables

 To delete some data from tables, use


DELETE commands.
 It removes rows from a table
 Not individual field values
 DELETE FROM <tablename> WHERE
<condition>;
 To remove all the contents of the table
 DELETE FROM <tablename>;
Dropping tables

 To delete the entire table existence, it


has to DROPped.

 DROP TABLE <tablename>;

 Todrop the table from the database after


checking the existence of the same.

 DROP TABLE IF EXISTS <tablename>;


Altering Tables

 It changes the definition of existing


tables.
 ALTER TABLE command is used:
 To add a column
 ALTER TABLE <table> ADD <columnname>
<datatype><size> [constraint];
 To add an integrity constraint
 To redefine a column(datatype, size,
default value)

You might also like