Professional Documents
Culture Documents
www.kaniyam.com
MySQL
( Free Open Source Software ) Database System.
, .
"" ,
2012 . MySQL
,
.
, editor@kaniyam.com
.
http://kaniyam.com/mysql-book-in-tamil
. .
, .
.
editor@kaniyam.com
www.kaniyam.com
MySQL
2013
2013 .
:
:
. ,
, , www.kaniyam.com
. .
.
:
http://dev.kaniyam.com/projects/kaniyam/files
www.kaniyam.com
www.kaniyam.com
.
,
11 2013
: nithyadurai87@gmail.com
: http://nithyashrinivasan.wordpress.com
www.kaniyam.com
1 MySQL - ......................................................................................................................... 11
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
MySQL - ........................................................................................................... 11
MySQL- install ............................................................................................................. 13
Ubuntu Linux- MySQL- install ................................................................................14
Configuration................................................................................................................................ 15
Query cache.................................................................................................................................. 16
MySQL clients............................................................................................................................... 17
CENTOS/RHEL - ........................................................................................................ 18
Windows- MySQL- install ........................................................................................ 18
4 Tables ...................................................................................................................... 46
4.1
4.2
4.3
4.4
Table ..................................................................................................................... 46
database- table- .....................................................................................49
database- table- ...................................................51
database- table- Copy ....................................................................52
www.kaniyam.com
7
4.5 Tables- database- ........................................53
4.6 database- table - .................................................................54
4.7 Tables - ................................................................................................ 55
5 Columns - ............................................................................................................................ 57
5.1 column- table- .................................................................................57
5.2 Column- ......................................................................................58
5.3 column- table- .....................................................................................59
6 Indexes - ............................................................................................................................. 61
6.1
6.2
6.3
6.4
www.kaniyam.com
8
9 Library Database- data- ...................................................100
9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
www.kaniyam.com
9
13.4 Wildcards- IP address- users- ............148
13.5 database/table- ..................................149
13.6 Password - ............................................................................................................. 150
13.7 Users- tables- privileges- .......................151
13.8 User privileges ..........................153
13.9 User- privileges- ....................................................................153
13.10 user- .................................................................................155
13.11 Network access- ...............................................................................156
13.12 User authentication- ......................................................................156
13.13 SSL - ..........................................................157
www.kaniyam.com
10
15.8 Server Crash- .................................................................................................182
15.9 ERRORS ................................................................................................184
Can't Connect to MySQL Server................................................................................................ 184
Access Denied.............................................................................................................................. 186
Too Many Connections................................................................................................................ 186
MySQL Server Has Gone Away.................................................................................................. 187
Got Error from Table Handler..................................................................................................... 188
15.10 ......................................................................................................................188
16 ................................................................................................................................... 190
........................................................................................................................................... 190
............................................................................................................................................ 190
................................................................................................................................. 191
............................................................................................................................ 192
www.kaniyam.com
11
1 MySQL -
Database data- ,
. SQL(Structured
Query Language) database- data-
. RDBMS , database-
data-
Management software
.
1.1 MySQL -
MySQL- .
MySQL server MySQL client
. MySQL client
front end tool . Windows- console
prompt GNU/Linux-- shell prompt ,
SQL commands . commands- MySQL
server . MySQL
server-
. MySQL server-
result- .
www.kaniyam.com
12
Server-
www.kaniyam.com
13
MySQL- install .
MySQL- machine- install .
.
BSD,LinuX, Mac OS X, Solaris windows
platform- MySQL- install
.
www.kaniyam.com
14
12.10- MySQL .
debian ubuntu Linux Mint distribution- .
Terminal- .
sudoaptgetinstallmysqlservermysqlclient
MySQL- repository-
download install . MySQL- root
user- password . password
.
MySQL- .
sudomysql_secure_installation
.
1. Anonymous users-
www.kaniyam.com
15
2. localhost- root-
3. test database-
MySQL- .
1.4 Configuration
MySQL- configuration options- .
/etc/mysql directory- MySQL- config directory .
www.kaniyam.com
16
/etc/mysql/my.cnf
file MySQL- configuration options- .
Log file, port number, ip binding, performance options .
options .
port=3306
MySQL server- 3306 port- .
user=mysql
mysql user , user- server .
server- .
datadir=/var/lib/mysql
MySQL- database folder- . Backup
, folder- backup .
bindaddress=127.0.0.1
ip address- MySQL server .
log_error=/var/log/mysql/error.log
Configuration connection- file- log
.
1.5 Query cache
MySQL server- select queries-, results- cache-
. , server- .
options .
query_cache_limit=1m
query_cache_size=16m
www.kaniyam.com
17
m mb . RAM-
. .
query_cache_limit=2m
query_cache_size=32m
my.cnf file- , MySQL server- restart
.
MySQL server- restart
sudoservicemysqlrestart
18
sudoaptgetinstallMySQLadmin
PHPMyAdmin
sudoaptitudeinstallphpmyadmin
root- password . .
/usr/bin/mysqladminurootpassword'COMPLEXPASSWORDHERE'
password- root-
.
/usr/bin/mysqlsecureinstallation
.
1.8 Windows- MySQL- install
MySQL- (MySQL Community Server 5.5.29)
http://mysql.com/downloads - . msi
file- .
MySQL 5.1.35 -.
---
www.kaniyam.com
19
---
www.kaniyam.com
20
www.kaniyam.com
21
www.kaniyam.com
22
www.kaniyam.com
23
www.kaniyam.com
24
www.kaniyam.com
25
www.kaniyam.com
26
www.kaniyam.com
27
www.kaniyam.com
28
www.kaniyam.com
29
www.kaniyam.com
30
www.kaniyam.com
31
syntax .
shell> mysql [-h host] [-u user_name] [-p] [db_name]
optional
arguments- .
-h : host-
. , machine-
local server- .
-u : Username- . ,
UNIX username- ,
MySQL- authenticate .
-p : password- .
user account- , password
, .
www.kaniyam.com
32
Database_name : database-
. , Use
command- , database-
.
, options ,
mysql -?
shell prompt - run .
arguments- .
2.2 Client server-
mysql> prompt-
exit
type mysql-server- .
www.kaniyam.com
33
syntax .
echo'SELECT1+1'|mysqlusome_userp
Shell commands- , MySQL- pipes input
redirection- shell commands- .
shell command , input file-
commands- read MySQL- .
MySQL, username password- authenticate
, commands- execute
output file- .
mysqluuserp<input_file>output_file
34
CTRL+a
CTRL+e
ALT+b .
ALT+f .
CTRL+w .
CTRL+u command-
CTRL+ l commands- ,
ALT+u uppercase- .
ALT+l lowercase-
ALT+_ .
www.kaniyam.com
35
2 . --host
host .
option- . h
host .
Comments- # - .
, mysql -? run
.
2.6 MySQL-
MySQL command-line client- ,
command function- ,
helpcommand_or_function_name
enter . command function
. 'show tables'
command- syntax
helpshowtables
enter . output- .
www.kaniyam.com
36
MySQL ,
helpcontents
type . command, MySQL server-
, .
2.7
www.kaniyam.com
37
38
commands ,
ctrl+r type command
2.10 Tools
MySQL Administrator MySQL query browser
tools . MySQL
AB- GUI Applications .
open source . Cross-Platform- (Linux,Mac OSX
&Windows) .
MySQL Query Browser, ad-hoc queries- ,
.
www.kaniyam.com
39
www.kaniyam.com
40
3 Databases
Databases data- ,
container . MySQL Server
- databases .
database- .
tables - .
Databases - ,
. containers
. database -
.
3.1 database- server-
database- , server-
databases .
command .
SHOWDATABASES;
server- databases- .
install MySQL- command result -
.
www.kaniyam.com
41
database- . command .
CREATEDATABASEexams;
42
3.2 Database-
Databases . MySQL -
SQL command - .
MySQL - version -
.
1. MySQL server - .
2. Database - directory -
3. Server - .
process, MySQL - version - ,
server database - tables -
. database-
, tables-
. .
3.3 database- server- :
drop DB Command, database - table
- . command -
.
, tables - database
- .
DROPDATABASEexams;
www.kaniyam.com
43
www.kaniyam.com
44
database query -
. query- database-
.
SELECTtitleFROMlibrary.book;
.
SELECTtitleFROMbook;
.
3.5 LIKE Operator -
word- word
databases- Like operator -
.
SHOWDATABASESLIKE'my%';
command - install database -
run result - .
www.kaniyam.com
45
Like Operator- .
Percent(%) Underscore(_)
.
Percentage(%) - , Like Command
Percentage - 0
.
underscore(_) - , Like Command
Underscore- replace ,
.
www.kaniyam.com
46
4 Tables
database- tables .
table-
table definition - . rows columns
format- data- . table
.
4.1 Table
Address
.
Address , ,
.
index tab,
.
.
www.kaniyam.com
47
table -
. (
) columns ,
( ) rows table -
.
index- , table -
column- index
. .
www.kaniyam.com
48
MySQL Table- :
Table data- .
Table Body - row data- .
Table definition - row- data
Columns- .
Column - Name(eg: book_id) Type(eg:
Integer)- . Type-
. data
. Column, Integer
, 1
6502 3.14
.
row- Column-
. Column-
. Book_id
Column, SMALLINT Type- ,
data row- .
tables- Relation- Columns-
table- degree .
www.kaniyam.com
49
50
elements- .
tables - process
.
table - code .
CREATETABLEmarksheet(
student_idINT,
nameVARCHAR(255)NOTNULL,
marksINT
);
code - .
www.kaniyam.com
51
4. Table- , ,
command- run .
DESCmarksheet;
DESCRIBEmarksheet;
table- .
4.3 database- table-
'marksheet' table- 'marks' ,
rename table command- .
RENAMETABLEmarksheetTOmarks;
www.kaniyam.com
52
CREATETABLEnew_tableLIKEold_table;
INSERTnew_tableSELECT*FROMold_table;
www.kaniyam.com
53
queries - ,
table - ( indexes table options
) copy .
4.5 Tables- database-
MySQL - version -, database-
, tables-
. database- rename process
.
1. database - table -
. MySQL
Administrator tool - database -
.
2. database - , database-
database-
.
3. database - table - SHOW TABLES
RENAME TABLE command - , database -
.
4. database - users -, permissions
- database - .
5. test .
6. database -
database - .
Books database - library database - book,borrower &
loan 3 tables - code .
www.kaniyam.com
54
Temporarilydisablepermissions
CREATEDATABASElibrary;
RENAMETABLEbooks.bookTOlibrary.book;
RENAMETABLEbooks.borrowerTOlibrary.borrower;
RENAMETABLEbooks.loanTOlibrary.loan;
Migratepermissions
Reenablepermissions
www.kaniyam.com
55
4.7 Tables -
command, default database- tables
- .
2 command- database-
, database - tables -
.
3 command, Like operator- condition -
tables- .
SHOWTABLES;
SHOWTABLESINdatabase_name;
SHOWTABLESLIKE'word%';
commands- outputs .
www.kaniyam.com
56
www.kaniyam.com
57
5 Columns -
Table - Columns - .
column - table -
. column -
, tables - back-up .
5.1 column- table-
table - Column-
. syntax .
ALTERTABLEtable_name
ADDCOLUMN[column_definition];
Create table statement.- Column definition -
, column definition . ,
Alter table table_name .
rank Column, 'marksheet'
table- .
ALTERTABLEmarksheet
ADDCOLUMNrankVARCHAR(10)NOTNULL;
www.kaniyam.com
58
5.2 Column-
table- column- , column-
process .
, Alter table add column Alter table
change column . syntax .
ALTERTABLEtable_name
CHANGECOLUMNcolumn_name[column_definition];
column definition type attributes - column
name - . column name - column
definition- statement-, column name
.
query-, 'marksheet' table - 'rank'
column- 'grade' .
, width- .
www.kaniyam.com
59
ALTERTABLEmarksheet
CHANGECOLUMNrankgradeCHAR(6)NOTNULL;
60
www.kaniyam.com
61
6 Indexes -
Indexes table- column -
MySQL Server .
tables - data -
indexes .
6.1 Index- table-
Create index command - table-
columns- index- . syntax .
CREATEINDEXindex_name
ONtable_name(column_name,...);
syntax-, table- index - , index-
column-
.
query- index
.
CREATEINDEXaONmarksheet(student_id);
www.kaniyam.com
62
6.2 Index-
Index - .
index definition .
index - , index -
index - .
6.3 Index-
Index - syntax . table -
index - . syntax
.
DROPINDEXindex_nameONtable_name;
www.kaniyam.com
63
query- index .
DROPINDEXaONmarksheet;
6.4 Identifiers
MySQL Identifiers database column names
. case sensitive .
identifier- lower case letters - .
MySQL platform - , my.cnf file-
lower_case_table_names configuration variable 1 .
.
shell prompt- , 'my.cnf'- 'gedit'- open .
www.kaniyam.com
64
www.kaniyam.com
65
Library-
.
, e-mail
.
www.kaniyam.com
66
www.kaniyam.com
67
library database,
tables container .
7.2 tables-
table-
.
tables . .
www.kaniyam.com
68
Library- - ,
.
-
.
-
.
table-
:- book title, book author,book condition, person name, person
email-id . table-
,
,
2 table- .
memory space- redundant
data- table- .
table-
, table- duplicate data- .
,
duplicate .
table-
,
table- query
.
Reference- book person tables ,
table- refer transaction
loan table- .
3 table- .
www.kaniyam.com
69
code- .
Candidate Keys:
www.kaniyam.com
70
Primary Key:
table- column- title- candidate key
. primary key duplicate
column- title .
duplicate
candidate key- table- primary key- .
, synthetic primary key unique
column- primary key-
. candidate keys table- columns
. primary key- candidate
key- .
book table- 3 candidate keys .
book title, book author & book condition.
columns- duplicate . Duplicate
column- column-
duplicates . 3
column- duplicate
.
book_id synthetic primary key-
. column duplicate
. column table-
row- identify primary key .
www.kaniyam.com
71
book_id column- .
column type- column-
.
library- database 50,000
. column-
UNSIGNED SMALLINT datatype- . 0-
65,535 datatype .
book title book author columns-
. CHAR
VARCHAR datatypes . Char
fixed-length varchar
(variable length) data type . 0-
255 characters .
www.kaniyam.com
72
condition column- .
column- good poor
.
row-
ENUM data
type .
column- ENUM data type
, column-
list- .
column- memory
space .
lowercase letters-
table- primary key- column-
table- foreign key- . table-
column-
, primary key foreign key
.
Column- ,
column- column-
title . sql keywords- date
index column title- .
column- title-
,
Underscore(_) . .
book
table- columns-
,
www.kaniyam.com
73
Book_id
Title
Author
Cond
74
query query-
COMMENT keyword
. keyword, column table data-
optional keyword .
www.kaniyam.com
75
CREATETABLEloan(
loan_idINTUNSIGNEDNOTNULLAUTO_INCREMENT,
person_idTINYINTUNSIGNEDNOTNULL,
book_idSMALLINTUNSIGNEDNOTNULL,
date_lentDATENOTNULL,
PRIMARYKEY(loan_id)
)COMMENT"Storeinfoonbookloans";
www.kaniyam.com
76
tables- data- .
www.kaniyam.com
77
table- data- ,
insert statement
. syntax .
INSERT[INTO][db_name.]table_name
(list,of,columns,...)
VALUES(list,of,values,...)
www.kaniyam.com
78
INSERTbook(title,author,cond)
VALUES('Silanerangalilsilamanithargal','Jayakanthan',
'good');
79
www.kaniyam.com
80
query
query- rows- insert .
8.2 Auto increment column- data-
row - insert , auto-increment
column- , column
www.kaniyam.com
81
. , column
. book
table- book_id column auto-increment
.
row- delete auto-increment column-
.
,
rows- , data-
.
row - insert , auto-increment column-
NULL
.
.
CREATETEMPORARYTABLEdemo(
idINTNOTNULLAUTO_INCREMENT,
PRIMARYKEY(id)
);
INSERTdemo()VALUES();
SELECTidFROMdemo;#idcontains1
INSERTdemo(id)VALUES(NULL);
SELECTidFROMdemo;#idcontains1and2
www.kaniyam.com
82
INSERTdemo(id)VALUES(4);
SELECTidFROMdemo;#idcontains1,2and4
INSERTdemo(id)VALUES(NULL);
SELECTidFROMdemo;#idcontains1,2,4
and5
www.kaniyam.com
83
www.kaniyam.com
84
INSERTperson(name,email)
VALUES('Nithya','nithyadurai87@gmail.com'),
('Shrinivasan','tshrinivasan@gmail.com'),
('Kanmani','Kanmanishrini@gmail.com');
85
www.kaniyam.com
86
www.kaniyam.com
87
INSERTtable_one(list,of,columns)SELECT...;
, 'person' table- data-, 'info'
table- query- .
INSERTinfo(id,name,email)SELECT*fromperson;
www.kaniyam.com
88
89
data- MySQL-
. syntax .
LOADDATAINFILE'some_file.txt'
INTOTABLE'some_table'(list,of,columns,...);
text file- data- field- tab-,
row- line- .
LOAD DATA INFILE command, file-
import
. file- row- command- read
table- column mapping-
row .
/home/nithya/data.txt
file person table-
.
upload command .
LOADDATAINFILE'/home/nithya/data.txt'
INTOTABLEperson(name,email);
www.kaniyam.com
90
www.kaniyam.com
91
92
INSERTloan(book_id,person_id,date_lent)
VALUES(1,1,'20121119');
www.kaniyam.com
93
table- user
variable- assign , user variable- query-
.
user variable- format, @name . name
A-Z , 0-9
$,- user variable-
.
variable- := operator-
assign . :
SELECT@place1:='Kanchipuram';
'Kanchipuram' '@place1' user
variable . query- , run
variable .
SELECT@place1;
www.kaniyam.com
94
www.kaniyam.com
95
www.kaniyam.com
96
SELECT@book_id:=book_id,
@person_id:=person_id,
@date:='20121212'
FROMbook,person
WHEREbook.title='silanerangalilsilamanithargal'
ANDperson.name='Kanmani';
INSERTloan(book_id,person_id,date_lent)
VALUES(@book_id,@person_id,@date);
www.kaniyam.com
97
. query-
queries-
.
www.kaniyam.com
98
,
www.kaniyam.com
99
SELECTNOW()+0;
run , format- date- return .
www.kaniyam.com
100
www.kaniyam.com
101
9 Library Database-
data-
table- queries-
.
query table- data- return .
SELECT*FROMbook;
102
SELECTauthorFROMbookLIMIT3,2;
# 3 rows- , 2 rows- return .
103
SELECT*FROMbookORDERBYtitle;
# 'book' table- records-, title-
.
code- .
query, server- .
SELECT*FROMbookORDERBYtitle;
query, return row- title- list-
. .
Sila nerangalil sila manithargal (Row 1)
The 7 Habits of Highly Effective People (Row 2)
The Immortals of Meluha (Row 3)
Tuesdays with Morrie (Row 4)
Kallo kaaviyamo (Row 5)
Thanneer thesam (Row 6)
Yen Yetherku Yeppadi (Row 7)
www.kaniyam.com
104
www.kaniyam.com
105
www.kaniyam.com
106
e
, query- .
SELECTtitleFROMbookWHEREtitleLIKE'%e';
Like operator
wildcard characters .
%(Percent) - 0
characters- .
_(Underscore) - character-
character- .
www.kaniyam.com
107
book- table- S
, 8 author-
query- .
SELECTauthorFROMbook
WHEREauthorLIKE'S________%';
'Sujatha' author- .
7 .
www.kaniyam.com
108
average
.
.
SELECTMIN(marks),AVG(marks),MAX(marks)FROMperson;
YYYYMMDDHH:MM:SS(Eg:'2005093018:43:01')
YYYYMMDDHHMMSS(Eg:20050930184301)
www.kaniyam.com
109
CURDATE() function ,
CURTIME() function ,
now() function .
.
date/time-
.
, greater than(>),less than(<) equal
to(=) .
SELECT*FROMloan
WHEREdate_lent<=
DATE_SUB(CURDATE(),INTERVAL1WEEK);
www.kaniyam.com
110
SELECT*FROMloan
WHEREdate_lent>=
DATE_SUB(CURDATE(),INTERVAL1WEEK);
query- loan
table-,
data- .
query- loan table-,
data
data- .
www.kaniyam.com
111
,
.
,
Greenwich Mean Time(GMT) Coordinated Universal
Time(UTC)- .
logic- program- .
.
www.kaniyam.com
112
9.8 NULL-
NULL . empty zero-
. queries- .
SELECTNULL=FALSE;
SELECTNULL='';
SELECTNULL=0;
SELECTNULL=NULL;
www.kaniyam.com
113
114
www.kaniyam.com
115
2. INTO DUMPFILE
INTO DUMPFILE clause- select
statement-
file- .
INTO DUMPFILE clause row- return select
statement- . select statement-
columns
file- .
.
SELECT*FROMbook
WHEREbook_id=5
INTODUMPFILE"/tmp/book1";
www.kaniyam.com
116
file- .
www.kaniyam.com
117
10 Library database-
Tables-
queries- table-
. ,
sql- Joins, Subqueries Union
queries- ,
tables-
.
10.1
Relational databases- , data-
tables-
. table- row-
column- table- primary key
. table- primary key
column, table- primary key- ,
table- foreign key .
book table- book_id primary key ,
column table- row-
. , person table- person_id
column table- row-
.
loan table- "
" book
person table- .
loan table- book_id
person_id columns, table- foreign key .
www.kaniyam.com
118
,
loan table- , book_id
column- book table-
.
, loan
table- person_id person table-
. loan table-
. transaction
date_lent column .
loan,person book tables-
.
10.2 Where clause tables-
tables- data-
, tables- primary key foreign key
.
, tables- data-
tables- select statement- from clause-
. where clause- tables-
primary key-foreign key .
syntax .
SELECTcolumns
FROMtable1,table2
WHEREtable1.foreign_key=table2.primary_key;
where clause- . ,
, table- row- ,
www.kaniyam.com
119
table- row-
result .
query-, loan, person book
tables- ,
.
SELECTloan.date_lent,person.name,book.title
FROMloan,book,person
WHEREloan.book_id=book.book_id
ANDloan.person_id=person.person_id;
query- select statement- column-
prefix, column table-
qualifier . columns
tables- , column- table-
MySQL- error- .
column- table
name- qualifier- .
query- output .
www.kaniyam.com
120
SELECTloan.date_lent,person.name,book.title
FROMloan,book,person
WHEREloan.book_id=book.book_id
ANDloan.person_id=person.person_id
ANDperson.name='Nithya';
www.kaniyam.com
121
syntax .
SELECTcolumns
FROMtable1
JOINtable2
ONtable1.foreign_key=table2.primary_key;
Join tables- queries,
. Joining conditions ON clause-
, where clause- conditions-
.
Join keyword tables- Inner join Outer Join
.
10.4 Inner join tables-
Where clause- ,
Join keyword .
.
SELECTloan.date_lent,person.name,book.title
FROMloan
JOINbook
ONloan.book_id=book.book_id
JOINperson
ONloan.person_id=person.person_id;
www.kaniyam.com
122
www.kaniyam.com
123
JOINpersonASc
ONa.person_id=c.person_id;
www.kaniyam.com
124
SELECTcolumns
FROMtable1
LEFTOUTERJOINtable2
ONtable1.foreign_key=table2.primary_key;
loan person table- ,
, Outer Join query-
.
SELECTp.name,MAX(l.date_lent)
FROMpersonp
LEFTOUTERJOINloanl
ONp.person_id=l.person_id
GROUPBYp.name;
Query- output .
person table-
.
table- .
table, NULL-
.
www.kaniyam.com
125
SELECTp.name,MAX(l.date_lent)asmaximum_date
FROMpersonp
LEFTOUTERJOINloanl
ONp.person_id=l.person_id
GROUPBYp.name;
www.kaniyam.com
126
query table-
column- data- , result set-
, column table-
.
columns- alias name-
.
10.8 Subquery tables-
queries subquery
. select statement- query-
conditions- paranthesis-
query subquery . subquery- execute
. subquery-
main query- execute . syntax
.
SELECTcolumns
FROMtable1
WHEREcol1IN(SELECTcol2FROMtable2WHERE...);
www.kaniyam.com
127
www.kaniyam.com
128
subqueries- ,
queries- execute .
.
SELECTbook_idFROMbookWHEREcond='good';
SELECTDISTINCTb.nameFROMloanaJOINpersonb
ONa.person_id=b.person_idWHEREa.book_idIN(1,4,5,8,9);
query,
.
result- condition- query,
.
www.kaniyam.com
129
queries- ,
subquery- query- . subquery-
.
www.kaniyam.com
130
www.kaniyam.com
131
132
UNION Operator ,
result set .
SELECT*FROMbook
UNION
SELECT*FROMloan;
133
UNION
SELECTnameFROMperson;
SELECTcondFROMbookwherecond='good'
UNIONALL
SELECTnameFROMperson;
good
.
www.kaniyam.com
134
11 Library database-
data-
,
.
.
'update' command, table- data-
. syntax .
UPDATEtable_nameSETcolumn_name=valueWHEREcondition;
syntax .
SET - column-
. column-
, .
SET name0='value', name1='some_other_value',
...
Column-
. query, book table-
'cond' column- good .
UPDATEbookSETcond='good';
www.kaniyam.com
135
row- query-
.
UPDATEbook
SETcond='poor'
WHEREauthor='Jayakanthan';
www.kaniyam.com
136
www.kaniyam.com
137
www.kaniyam.com
138
12 Library database-
data-
table- row- 'delete' command
. syntax .
DELETEFROMtable_nameWHEREsome_conditions;
FROM - table- .
www.kaniyam.com
139
DELETEbook,loan
FROMbook,loan
WHEREbook.book_id=loan.book_id
ANDbook.cond='poor';
syntax .
list,of,tables - tables-
rows .
FROM - condition- .
12.2
MySQL- 'undo'
. ,
www.kaniyam.com
140
DROPDATABASElibrary;
query- run , 'library' database-
tables- .
queries , run , undo
. logs, backups data recovery tool
.
'library' database- data-
.
Users-, permissions- .
database-
, database- root account-
. , select queries run
permissions user- create , user
login . , adhoc
queries- run
.
adhoc backups- .
adhoc backups server admin backup
,
backup .
www.kaniyam.com
141
. options-
.
SETSQL_SAFE_UPDATES=1;
www.kaniyam.com
142
ROLLBACK
.
transaction-,
rollback- commit- .
BEGIN;
UPDATEbookSETauthor="Sujatha";
SELECT*FROMbook;
ROLLBACK;
BEGIN;
UPDATEbookSETAUTHOR="Sujatha"
WHEREauthor="Vairamuthu";
SELECT*FROMbook;
COMMIT;
www.kaniyam.com
143
13 Users-
MySQL users database .
.
MySQLs privilege system
database -
. username database
remote address .
, password-
database - .
13.1 Server- user-
user- , admin rights
. , MySQL- install 'root'
user, 'admin rights' user- . syntax
.
CREATEUSERuser@hostIDENTIFIEDBY'password';
, Nithya' user, 192.168.1.10
remote IP address- . remote IP
address- abcd password- login .
ip address- password
login .
CREATEUSER'Nithya'@'192.168.1.10'
IDENTIFIEDBY'abcd';
www.kaniyam.com
144
, ip address ,
local host name domain name
.
MySQL
database- localhost . .
CREATEUSER'Nithya'@'localhost'
IDENTIFIEDBY'super';
'Nithya'
user , password-
.
create user command, MySQL version 5.0.2
version- . version-
command . version- database
, MySQL tables- user,host database-
. version- ,
user table- insert command-
. .
www.kaniyam.com
145
INSERTINTOuser(Host,User,Password)
VALUES('123.456.78.90','Nithya',
PASSWORD('abcd'));
FLUSHPRIVILEGES;
www.kaniyam.com
146
user ,
active user session-
. session-
.
drop user command, MySQL version => 5.0.2-
. version- delete
command . .
DELETEFROMuser
WHEREUser='Nithya'
ANDHost='192.168.1.10';
FLUSHPRIVILEGES;
13.3 user-
. username
,
username surname- .
surname ,
username- .
" " ,
" " , RENAME USER command
www.kaniyam.com
147
- , .
RENAMEUSERold_user@hostTOnew_user@host;
rename user command, MySQL version >= 5.0.2-
. version-
update command . .
UPDATEuser
SETUser='Nithya_Shrinivasan'
WHEREUser='Nithya_Duraisamy'
ANDHost='localhost';
UPDATEdb
SETUser='Nithya_Shrinivasan'
WHEREUser='Nithya_Duraisamy'
ANDHost='localhost';
FLUSHPRIVILEGES;
www.kaniyam.com
148
149
192.168.0.x IP range- IP
.
,
login username password-
query- .
CREATEUSER'Karthik'@'%'IDENTIFIEDBY'phrasebook';
SELECTDbFROMdb
WHEREUser='user'ANDHost='host';
SELECTTable_nameFROMtables_priv
WHEREUser='user'
ANDHost='host'ANDDb='db';
database- db
table- user host- query- .
www.kaniyam.com
150
SELECTDb,Table_nameFROMtables_priv
WHEREUser='Nithya'ANDHost='localhost'
user, tables-
tables_priv table- user, host db
condition- query- .
13.6 Password -
MySQL - user- login ,
password- query- .
SETPASSWORD=PASSWORD('murali');
'murali' password .
admin- login ,
password- query- .
SETPASSWORDFORkumar@localhost=
PASSWORD('xxxx');
www.kaniyam.com
151
www.kaniyam.com
152
GRANTSELECT,INSERTONlibrary.book
TO'karthik'@'localhost'IDENTIFIEDBY'bca';
user- , ALL
PRIVILEGES ALL keyword- .
'kumar' user- library database- 'book'
table- .
GRANTALLPRIVILEGESONlibrary.book
TO'karthik'@'localhost';
user- ,
IDENTIFIED BY clause - .
www.kaniyam.com
153
www.kaniyam.com
154
command- ,
Grant statements- .
SHOWGRANTSFOR'karthik'@'localhost';
statement- password,
password- encrypt .
www.kaniyam.com
155
www.kaniyam.com
156
#mysqld_safeskipnetworking&
Network access- disable , user- remote
hosts MySQL- .
MySQL- remote access- , MySQL-
firewall, appropriate port-
. , 3306 port .
www.kaniyam.com
157
MySQLuroot
password MySQL- ,
password- .
updateusersetpassword=PASSWORD("NEWROOTPASSWORD")where
User='root';
user authentication- enable FLUSH PRIVILEGES
command- MySQL server- -skip-grant-tables
restart .
13.13 SSL -
MySQL database server- client- encrypt
network SSL- .
, server- compile enable
. enable
command- .
SHOWVARIABLESLIKE'have_openssl';
command, Yes No variable-
.
compile time- enable ,
server- --with-vio --with-openssl
configure switches- recompile .
encrypt SSL ,
certificate files- .
certificate authority certificate, server certificate client
certificate .
www.kaniyam.com
158
MySQLsslca=/path/to/CAcert\
sslcert=/path/to/servercert\
sslkey=/path/to/clientcert
www.kaniyam.com
159
www.kaniyam.com
160
compile libmysqlclient-
.
shell>gccomytestmytest.clmysqlclient
compile , error messages ,
mysql_config utility- compiler
options . cflags, --include
libs switches- mysql_config- run ,
. .
shell>mysql_configcflagsincludelibs
I/usr/include/mysqlmcpu=i486fnostrengthreduce
I/usr/include/mysql
L/usr/lib/MySQLlMySQLclientlcryptlnsllmlzlc
lnss_fileslnss_dnslresolv
lclnss_fileslnss_dnslresolv
14.2 MySQL-
MySQL database- mysql_init()
mysql_real_connect() function calls .
function, MySQL type- object- initialize .
function, object- argument-
. , API calls- , object
database connection-
Resource argument- .
Port argument database- TCP/IP Port-
. localhost 0- .
www.kaniyam.com
161
MYSQLmysql;
mysql_init(&mysql);
mysql_real_connect(&mysql,"host","user",
"password","dbname",
port,unix_socket,client_flag);
, socket- named pipe-
unix socket argument- NULL- .
mysql client library- features- enable ,
client_flag argument- NULL- .
mysql_real_connect() -
return mysql object . object-
argument- object- .
variable- assign . ,
, function, NULL
return .
www.kaniyam.com
162
www.kaniyam.com
163
MYSQL_RESresult;
MYSQL_ROWrow;
result=mysql_use_result(&mysql);
row=mysql_fetch_row(result);
mysql_fetch_row() function, execute query-
row- mysql_row structure-
. row[0]- , row[n-1]
row- . n data set- column-
.
mysql_num_fields() column- ,
mysql_num_rows() rows- .
14.5 Error Messages-
mysql_errno() mysql_error() functions ,
message- .
function arguments MySQL object .
function- return error
, execute query-
error . execute
query mysql_errno()
function, 0- mysql_error() function, NULL
return .
mysql_errno(&mysql);
mysql_error(&mysql);
functions- database-
query- execute
www.kaniyam.com
164
14.6
MySQL ,
,
mysql_close() function . mysql_init() allocate
resource- de-allocate .
mysql_close(&mysql);
#include <stdio.h>
#include <mysql/mysql.h>
main() {
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int numrows, numcols, c;
mysql_init(&mysql);
/* Establish a database connection */
if (!mysql_real_connect(&mysql, "localhost",
www.kaniyam.com
165
"username", "password",
"dbname", 0, NULL, 0))
{
fprintf(stderr,
"Failed to connect to database: Error %d:
%s\n", mysql_errno(&mysql),
mysql_error(&mysql));
}
/* Execute a query */
char query[] = "SELECT book_id, cond, title
FROM book";
if (mysql_query(&mysql, query))
{
fprintf(stderr,
"Error executing query: Error %d: %s\n",
mysql_errno(&mysql), mysql_error(&mysql));
}
/* Assign the result handle */
result = mysql_use_result(&mysql);
if (!result)
{
fprintf(stderr,
"Error executing query: Error %d: %s\n",
www.kaniyam.com
166
mysql_errno(&mysql), mysql_error(&mysql));
}
/* Find the number of columns in the result */
numcols = mysql_num_fields(result);
/* Loop through the result set to display it */
while (row = mysql_fetch_row(result)) {
for(c=0; c<numcols; c++) {
printf("%s\t", row[c]);
}
printf("\n");
}
}
167
www.kaniyam.com
168
www.kaniyam.com
169
$errno=Mysql>errno;
$errstr=MySQL>errstr;
14.12
DBI method- .
program- exit , resources
. Exit , resource-
undef command- handle- .
#!/usr/bin/perl
useMysql;
/*Establishadatabaseconnection*/
$dbh=Mysql>connect(undef,"dbname","username","password")
ordie("Failedtoconnecttodatabase:Error"
.Mysql>errstr);
/*Executeaquery*/
$sql_statement="SELECTbook_id,cond,titleFROMbook";
$sth=$dbh>query($sql_statement)
ordie("Errorexecutingquery:Error".
$dbh>errno);
/*Loopthroughtheresultsettodisplayit*/
www.kaniyam.com
170
while(@row=$sth>fetchrow){
for($i=0;$i<$sth>numfields;$i++){
print$row[$i]."\t";
}
print"\n";
}
14.13 PHP API
PHP Applications- MySQL database-
.
PHP enable web server- MySQL- support-
<?php phpinfo();?> script-
.
script- web browser- MySQL support MySQLi support
. MySQLi MySQL Improved PHP
API- version . MySQL 4.1.3
version- . procedural
object-oriented . MySQLi-
. MySQL API , MySQLi- procedural
. Compile time- MySQLi support-
withmysql=/path/to/mysql_config switch-
enable .
14.14 MySQL-
$conn=mysqli_connect("host","user",
"password","dbname");
$conn=newmysqli("host",
"user","password","dbname");
www.kaniyam.com
171
$result=mysqli_query(query,$conn);
$result=$conn>query(query);
Object-oriented query() function, query-
argument- result handle- . result
object- , object-
methods- queries- process .
14.16 Result set-
procedural mysqli_fetch_array() function
, result- data record- .
www.kaniyam.com
172
www.kaniyam.com
173
mysqli_errno() errno() .
14.18
PHP script MySQL resources
.
mysqli_close($conn);
$conn>close();
, resources-
procedural mysqli_close() function-
object-oriented conn object- close()
method- . Database , result resource-
procedural mysqli_free_result()
function object-oriented result object-
free_result() method .
<?php
/*Establishadatabaseconnection*/
$conn=newmysqli("localhost","user","password","dbname");
if(!$conn){
echo"Failedtoconnecttodatabase:Error".
$conn>error()."<br>\n";
exit;
}
/*Executeaquery*/
www.kaniyam.com
174
$sql_statement="SELECTbook_id,cond,titleFROMbook";
$result=$conn>query($sql_statement);
if(!$result){
echo"Errorexecutingquery:Error:".
$conn>error()."<br>\n";
exit;
}
/*Loopthroughtheresultsettodisplayit*/
echo"<table>\n";
while($row=$result>fetch_array()){
echo"<tr>\n";
for($i=0;$i<$result>num_rows;$i++){
echo"<td>".$row[$i]."</td>\n";
}
echo"</tr>\n";
}
echo"</table>";
?>
www.kaniyam.com
175
MySQL Server-
.
.
15.1 Backup
database-
.
, data- , backup
file- data-
.
database- backup
.
database- update backup
database- offline-
.
backup . Full
backup Incremental backup .
.
15.2 Full Backup
database- sql file-
backup mysqldump command .
database- table- create statement-,
table- row- insert statement
sql file- .
www.kaniyam.com
176
177
mysqldumpuser=rootpassword=murali\
databaseslibraryexams>dump.sql
www.kaniyam.com
178
www.kaniyam.com
179
mysqluser=userhost=hostport=port\
password=passworddbname<filename.sql
180
www.kaniyam.com
181
CHECKTABLEtable;
REPAIRTABLEtable;
command, book table- error
.
CHECKTABLEbook;
command- output , table
. table-
.
www.kaniyam.com
182
www.kaniyam.com
183
Uptime:4days8hours20min3sec
output- server- UP
. server client-
client program- .
output- UP time
mysqld crash- ,
tables corrupt-
. mysqld - command- data
directory- run .
www.kaniyam.com
184
myisamchksilentforce*/*.MYI
system- table- .
mysqld- --log switch- restart , my.cnf
file- [mysqld] log- . execute
sql command- file- log .
commands- crash-
.
http://bugs.mysql.com/ bug database- query
, bug report- file .
crash- server- .
system- RAM- hard drives-
MySQL- crash- . server
crash- system-
hardware- .
15.9 ERRORS
errors-
.
Can' t Connect to MySQL Server
error, server machine- mysqld process
.
ERROR2002:Can'tconnecttolocalMySQLserver
throughsocket'/var/lib/mysql/mysql.sock'(2)
ERROR2003(HY000):Can'tconnecttoMySQLserver
on'123.45.67.89'(113)
www.kaniyam.com
185
www.kaniyam.com
186
windows- .
>netstata|find"LISTENING"
remote MySQL server- ,
mysqld- firewall
settings- . Port 3306 port
port- TCP/IP traffic- .
error message "MySQL server-
" " "
. MySQL server-
password- "
" error .
Access Denied
username password
database- username
error .
ERROR1045(28000):Accessdeniedforuser
'user'@'host'(usingpassword:YES)
MySQL database- privilege table- ,
connection arguments-
--password -p switch-
password- .
-p- argument database name
.
Too Many Connections
www.kaniyam.com
187
MySQL-
max_connections variable- .
100 . 100-
error
.
ERROR1040:Toomanyconnections
variable- my.cnf file-
errors- .
[mysqld]
max_connections=200
. MySQL,
. system
max_connections-
.
variable-
. superuser-
.
super user-
.
mysqladmin processlist command-
.
MySQL Server Has Gone Away
mysqld- client-
error .
ERROR2006:MySQLserverhasgoneaway
8
. wait_timeout system variable-
. variable- seconds-
www.kaniyam.com
188
. error,
mysql program
.
error , mysqld process
. mysql- , mysqld
.
Got Error from Table Handler
error database- table storage file- .
Error1030:Goterror141fromtablehandler
myisamchk utility-
. message- error ,
. error perror command
error message- .
shell>perror141
141=Duplicateuniquekeyorconstraintonwrite
orupdate
error myaqld-
myisamchk -- recover .MYI file- run .
error message, system
. .
$perror28
Errorcode28:Nospaceleftondevice
15.10
.
www.kaniyam.com
189
http://dev.mysql.com/doc/refman/5.0/en/temp0105.html
online manual-
.
MySQL mailing list- topics-
. lists-
.
mailing list- http://lists.mysql.com/
.
MySQL
AB- . MySQL AB ,
support- . support packages-
MySQL knowledge base- .
MySQL Knowledge base
technical articles- searchable
library
24 , 7 support
.
MySQL expert , remote-
.
www.mysql.com/support/ .
www.kaniyam.com
190
16
.
, , .
.
.
, ,
.
.
.
.
editor@ kaniyam. com
.
:
www.kaniyam.com
191
.
.
, .
.
,
.
.
, , , , ,
.
.
editor@kaniyam.com
.
,
.
editor@kaniyam.com .
.
.
www.kaniyam.com
192
.
.
.
2013 .
http://creativecommons.org/licenses/by-sa/3.0/
.
,
, , , ,
, .
: . editor@kaniyam.com +91 98417 95468
: . , 13 11 , ,
600035 . : +91 94455 54009 amachu@kaniyam.com
.
www.kaniyam.com
193
www.kaniyam.com