Vous êtes sur la page 1sur 25

shannu93.blogspot.

in

UNIT I
INTRODUCTION:
Introduction to Open Sources
Open Source is a certification mark owned by the Open Source Initiative
(OSI). It refers to any program whose source code is made available for use or
modification as users or other developers. Open source software (OSS) refers to
software that is developed, tested, or improved through public collaboration and

n
distributed with the idea that the must be shared with others, ensuring an open

.i
future collaboration. ((OSS) is computer software with its source code made available and

ot
licensed with a license in which the copyright holder provides the rights to study, change and
sp
distribute the software to anyone and for any purpose. Open-source software is very often
og
developed in a public, collaborative manner.)

Definition:
bl
3.

Open-source software is computer software whose source code is available


9

under a license (or arrangement such as the public domain) that permits users to study,
nu

change, and improve the software, and to redistribute it in modified or modified form. Based
on two principles we can call particular software as open source software
an

Principle1:
sh

The software source code should be available with license and that license contain
permissions they are

1) The user is able to study the code

2) The user able to change the code

3) The may able to improve the code


shannu93.blogspot.in

Principle2:

The license should not have certain restrictions in terms of

1) Technology

2) Field 3) Hardware

Technology:

Here Technology means operating system, in the computer science there are many different
operating systems available, here the software must support all kinds of operating sytems
such as windows, UNIX, Linux and Mac os.

.i n
Field:

ot
Now a days computer enter into many fields such as agriculture, medical and Biotechnology

sp
fields. here the software must supports or works in all fields
og
Hardware:
bl

In this context hardware means devices such as Nokia, Samsung and celkon. Here the
3.

software should work on or supports all kinds of devices.


9

Need of Open Sources:


nu

The Open Source Definition is used by the Open Source Initiative to determine
an

whether or not a software license can be considered open source.


The distribution terms of open-source software must comply with the following
sh

criteria:
1. Free Redistribution
The license shall not restrict any party from selling or giving away the software as a
component of an aggregate software distribution containing programs from several different
sources. The license shall not require a royalty or other fee for such sale.

2. Source Code
The program must include source code, and must allow distribution in source code as well
as compiled form. Where some form of a product is not distributed with source code, there
must be a well-publicized means of obtaining the source code for no more than a reasonable
shannu93.blogspot.in

reproduction cost preferably, downloading via the Internet without charge. The source code
must be the preferred form in which a programmer would modify the program. Deliberately
obfuscated source code is not allowed. Intermediate forms such as the output of a
preprocessor or translator are not allowed.

3. Derived Works

The license must allow modifications and derived works, and must allow them to be
distributed under the same terms as the license of the original software.

4. Integrity of The Author's Source Code


The license may restrict source-code from being distributed in modified form only if the

n
license allows the distribution of "patch files" with the source code for the purpose of

.i
modifying the program at build time. The license must explicitly permit distribution of

ot
software built from modified source code. The license may require derived works to carry a

sp
different name or version number from the original software.
5. No Discrimination Against Persons or Groups
og
The license must not discriminate against any person or group of persons.
bl

6. No Discrimination Against Fields of Endeavor


The license must not restrict anyone from making use of the program in a specific field of
3.

endeavor. For example, it may not restrict the program from being used in a business, or from
9

being used for genetic research.


nu

7. Distribution of License
an

The rights attached to the program must apply to all to whom the program is redistributed
without the need for execution of an additional license by those parties.
sh

8. License Must Not Be Specific to a Product


The rights attached to the program must not depend on the program's being part of a
particular software distribution. If the program is extracted from that distribution and used or
distributed within the terms of the program's license, all parties to whom the program is
redistributed should have the same rights as those that are granted in conjunction with the
original software distribution.
9. License Must Not Restrict Other Software
The license must not place restrictions on other software that is distributed along with the
licensed software. For example, the license must not insist that all other programs distributed
on the same medium must be open-source software.
shannu93.blogspot.in

10. License Must Be Technology-Neutral: No provision of the license may be predicated


on any individual technology or style of interface.

Advantages of Open Sources:

1. Lesser hardware costs:

Since Linux and open source solutions are easily portable and compressed, it takes lesser
hardware power to carry out the same tasks when compared to the hardware power it takes on
servers, such as, Solaris, Windows or workstations. With this less hardware power advantage,
you can even use cheaper or older hardware and still get the desired results.

n
2. High-quality software:

.i
ot
Open source software is mostly high-quality software. When you use the open source
software, the source code is available. Most open source software are well-designed. Open
sp
source software can also be efficiently used in coding. These reasons make open source
og
software an ideal choice for organizations.

3. No vendor lock-in:
bl
3.

IT managers in organizations face constant frustration when dealing with vendor lock-ins'.
Lack of portability, expensive license fees and inability to customize software are some of the
9
nu

other disadvantages. Using open source software gives you more freedom and you can
effectively address all these disadvantages.
an

4. Integrated management:
sh

By using open source software, you can benefit from integrated management. Open source
software uses technologies, such as, common information model (CIM) and web based
enterprise management (WBEM). These high-end technologies enable you to integrate and
combine server, application, service and workstation management. This integration would
result in efficient administration.

5. Simple license management:

When you use open source software, you would no longer need to worry about licenses.
Open source software enables you to install it several times and also use it from any location.
shannu93.blogspot.in

You will be free from monitoring, tracking or counting license compliance.

6. Lower software costs:

Using open source software can help you minimize your expenses. You can save on
licensing fees and maintenance fees. The only expenses that you would encounter would be
expenditure for documentation, media and support.

7. Abundant support:

n
You will get ample support when you use open source software. Open source support is

.i
mostly freely available and can be easily accessed through online communities. There are

ot
also many software companies that provide free online help and also varied levels of paid

sp
support. Most organization who create open source software solutions also provide
maintenance and support.
og
8. Scaling and consolidating:
bl

Linux and open source software can be easily scaled. With varied options for clustering, load
3.

balancing and open source applications, such as email and database, you can enable your
9

organization to either scale up and achieve higher growth or consolidate and achieve more
nu

with less.
an

Applications of Open Sources:


sh

Open source software is booming: here we round up over 480 open source applications for
you to use or build upon.

OSS is user-facing, software that does something useful under the operation of a human
being. The following applications are all FOSS, in most cases the result of many years of
wide user involvement and continuing improvement.

Open Source Operating System:

An open-source operating system is one that is available to the general public for use
and/or modifications from its original design free of charge. Open source means that you can
shannu93.blogspot.in

get the source code of the s/w for free (source code is the code of the program written in a
certain programming language). Operating system is the s/w that you use to operate your pc
(like Windows or Linus) OS can be any LINUX or UNIX flavour, but important thing is File
system. ZFS is best suit for storage, so snapshot, writable clone and replication is free. ZFS is
very simple and convenient. You can use OS under VMware or virtual box that support.

LINUX:

Introduction:

It was created in October 1991 by a University of Helsinki student named Linus Torvalds

n
(Linux stands for Linus’s UNIX). Linux itself is actually just the kernel; it Implements

.i
multitasking and multiuser functionality, manages hardware, allocates Memory, and enables

ot
applications to run. A very popular open source operating system that runs on a verity of

sp
hardware platforms. Linux is widely deployed as a server OS. Linux is multi-tasking, multi
user operating system. Although modified by numerous people.
og
General Overview:
bl
3.

A simple description of the UNIX system, also applicable to Linux, is this: "On a UNIX
system, everything is a file; if something is not a file, it is a process."This statement is true
9

because there are special files that are more than just files (named pipes and sockets, for
nu

instance), but to keep things simple, saying that everything is a file is an acceptable
an

generalization. A Linux system, just like UNIX, makes no difference between a file and a
directory, since a directory is just a file containing names of other files. Programs, services,
sh

texts, images, and so forth, are all files. Input and output devices, and generally all devices,
are considered to be files, according to the system. In order to manage all those files in an
orderly fashion, man likes to think of them in an ordered tree-like structure on the hard disk,
as we know from MS-DOS (Disk Operating System) for instance. The large branches contain
more branches, and the branches at the end contain the tree's leaves or normal files. For now
we will use this image of the tree, but we will find out later why this is not a fully accurate
image.
shannu93.blogspot.in

Kernel Mode and User Mode:

Kernel Mode:

In Kernel mode, the executing code has complete and unrestricted access to the underlying
hardware. It can execute any CPU instruction and reference any memory address. Kernel
mode is generally reserved for drivers which operate on the lowest-level, most trusted
functions of the operating system. Crashes in kernel mode are catastrophic; they will halt the
entire PC. Mode where all kernel programs execute (different drivers). It has access to every
resource and underlying hardware.

User Mode:

.i n
In User mode, the executing code has no ability to directly access hardware and RAM or

ot
reference memory. The reason for this is because if all programs ran in kernel mode, they

sp
would be able to overwrite each other’s memory. Code running in user mode must delegate to
system APIs to access hardware or memory. (If it needs to access any of these features – it
og
makes a call to the underlying API). Due to the protection afforded by this sort of isolation,
bl

crashes in user mode are always recoverable. Most of the code running on your computer will
execute in user mode.(Each process started by windows except of system process runs in user
3.

mode).
9
nu
an
sh
shannu93.blogspot.in

UNIT II

LINUX

Process
Linux can manage the processes in the system, each process is represented by
a task_struct data structure (task and process are terms which Linux uses interchangeably).
The task vector is an array of pointers to every task_struct data structure in the system.

This means that the maximum number of processes in the system is limited by the size of

n
the task vector; by default it has 512 entries. As processes are created, a new task_struct is

.i
ot
allocated from system memory and added into the task vector. To make it easy to find, the
current, running, process is pointed to by the current pointer.
sp
As well as the normal type of process, Linux supports real time processes. These processes
og
have to react very quickly to external events (hence the term ``real time'') and they are treated
differently than normal user processes by the scheduler. Although the task_struct data
bl

structure is quite large and complex, but its fields can be divided into a number of functional
areas:
3.

State:
9

As a process executes it changes state according to its circumstances. Linux processes


nu

have the following states:


Running:
The process is either running (it is the current process in the system) or it is ready to
an

run (it is waiting to be assigned to one of the system's CPUs).


Waiting:
sh

The process is waiting for an event or for a resource. Linux differentiates between two
types of waiting process; interruptible and uninterruptible. Interruptible waiting
processes can be interrupted by signals whereas uninterruptible waiting processes are
waiting directly on hardware conditions and cannot be interrupted under any
circumstances.
Stopped:
The process has been stopped, usually by receiving a signal. A process that is being
debugged can be in a stopped state.
Zombie:
This is a halted process which, for some reason, still has a task_struct data structure
in the task vector. It is what it sounds like, a dead process

Classification of Processes:
1. Compute-bound versus I/O bound. Linux implicitly favors I/O bound processes
over compute bound processes (why?).
shannu93.blogspot.in

2. Another classi_cation:
 Interactive processes. Examples: shells, text editors, GUI applications.
 Batch processes. Examples: compilers, database search engine, web server,
number-crunching.
 Real-time processes. audio/video applications, data-collection from physical
sensors, robot controllers.
Process States in Linux:
There are _ve process states de_ned in /usr/include/linux/sched.h.
#define TASK_RUNNING 0
#define TASK_INTERRUPTIBLE 1
#define TASK_UNINTERRUPTIBLE 2
#define TASK_ZOMBIE 4
#define TASK_STOPPED 8

Advanced Concepts:

.i n
A RUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS. If you have two

ot
terminal windows showing on your screen, then you are probably running the same terminal
program twice—you have two terminal processes. Each terminal window is probably running

sp
a shell; each running shell is another process.When you invoke a command from a shell, the
corresponding program is executed in a new process; the shell process resumes when that
og
process completes.
Advanced programmers often use multiple cooperating processes in a single application
to enable the application to do more than one thing at once, to increase application
bl

robustness, and to make use of already-existing programs. Most of the process manipulation
functions described in this chapter are similar to those on other UNIX systems. Most are
3.

declared in the header file <unistd.h>; check the man page for each function to be sure.
Process IDs:
9

Each process in a Linux system is identified by its unique process ID, sometimes referred
nu

to as pid. Process IDs are 16-bit numbers that are assigned sequentially by Linux as new
processes are created. Every process also has a parent process.
an

Printing the Process ID:


#include <stdio.h>
#include <unistd.h>
sh

int main ()
{
printf (“The process ID is %d\n”, (int) getpid ());
printf (“The parent process ID is %d\n”, (int) getppid ());
return 0;
}
Killing a Process:
kill a running process with the kill command. Simply specify on the command line
the process ID of the process to be killed. The kill command works by sending the process a
SIGTERM, or termination, signal. This causes the process to terminate, unless the executing
program explicitly handles or masks the SIGTERM signal.
Creating a Process using System:
The system function in the standard C library provides an easy way to execute a
command from within a program, much as if the command had been typed into a
shell.
shannu93.blogspot.in

#include <stdlib.h>
int main ()
{
int return_value;
return_value = system (“ls -l /”);
return return_value;
}

Scheduling
Linux schedules the parent and child processes independently; there’s no guarantee of which
one will run first, or how long it will run before Linux interrupts it and lets the other process
(or some other process on the system) run. In particular, none, part, or all of the ls command
may run in the child process before the parent completes. Linux promises that each process
will run eventually—no process will be completely starved of execution resources.

.i n
Process Scheduling in Linux:

ot
Scheduling Mechanism: how to switch.
sp
Scheduling Policy: when to switch and what process to choose. Some scheduling objectives:
1. fast process response time
og
2. avoidance of process starvation
3. good throughput for background jobs
4. support for soft real time processes
bl

Linux uses dynamically assigned process priorities for non real-time processes. Processes
running for a long time have their priorities decreased while processes that are waiting have
3.

their priorities increased dynamically.


9

Scheduling Parameters:
nu

 Processes are preemptible in user mode but not in kernel mode.


 How long is a quantum? Examine INIT TASK macro in include/linux/sched.h
an

header _le. All processes inherit the default quantum value via fork from the
init task.
#define DEF_COUNTER (10*HZ/100) /* 100 ms time slice */
sh

#define MAX_COUNTER (20*HZ/100)


#define DEF_NICE (0)
 Processes have two types of priorities:
 Static priority. Between 1 and 99. Used by real-time processes.
 Dynamic priority. For non real-time processes. Sum of the base time quantum
and of the number of ticks of CPU time left to the process before its quantum expiers
in the current epoch.

Invoking the scheduler:


The scheduler proper is the function schedule() in kernel/sched.c.
 Direct Invocation. Examples: A process must be blocked because a resource is not
available, a device driver can invoke schedule() directly if it will be executing a long
iterative task.
 Lazy Invocation. By setting the need resched ag _eld of current to 1. Examples:
shannu93.blogspot.in

 When current has use up its quantum of CPU time (done by update process times
function in kernel/timer.c)
 When a process is woken up and its priority is higher than that of the current process.
 When a setscheduler() or sched yield() system call is issued.
Actions performed by schedule():
 treat current process
 select process
 switch process
Linux SMP scheduler:
 Each cpu runs the schdule function on its own and communicate via shared data
structures.
 Processor a_nity is implemented through extra goodness for staying on the same cpu.
Performance of Linux Scheduler:
 The scheduling algorithm does not scale well.
 The prede_ned quantum is too large for high system loads.

n
 I/O bound process priority boosting is not optimal.

.i
 Support for real-time applications is weak.

ot
Several attempts have been made to provide alternate schedulers. An interesting tool called
Lockmeter is available to study performance of SMP scheduling.
System Calls Related to Scheduling:
nice()
sp
og
getpriority()
setpriority()
sched getscheduler()
bl

sched setscheduler()
3.

sched getparam()
sched setparam()
9

sched yield()
sched get priority min()
nu

sched get priority max()


sched rr get interval()
an

Linux Personalities:
sh

Personalities:

personality - set the process execution domain

SYNOPSIS:

int personality(unsigned long persona);

(or)

#include <sys/personality.h>

int personality(unsigned long persona);


shannu93.blogspot.in

DESCRIPTION:

Linux supports different execution domains, or personalities, for each process. Among
other things, execution domains tell Linux how to map signal numbers into signal actions.
The execution domain system allows Linux to provide limited support for binaries compiled
under other Unix-like operating systems. personality will make the execution domain
referenced by persona the new execution domain of the current process

RETURN VALUE:
On success, the previous persona is returned. On error, -1 is returned, and errno is set
appropriately.
ERRORS:
EINVAL The kernel was unable to change the personality.
CONFORMING TO:
personality() is Linux-specific and should not be used in programs intended to be portable.

.i n
CLONING:

ot
clone() creates a new process, in a manner similar to fork(2). It is actually a library function
sp
layered on top of the underlying clone() system call, hereinafter referred to as sys_clone. A
description of sys_clone is given towards the end of this page. Unlike fork(2), these calls
og
allow the child process to share parts of its execution context with the calling process, such as
the memory space, the table of file descriptors, and the table of signal handlers. (Note that on
this manual page, "calling process" normally corresponds to "parent process". But see the
bl

description of CLONE_PARENT below.)


3.

The main use of clone() is to implement threads: multiple threads of control in a program that
9

run concurrently in a shared memory space.


nu

When the child process is created with clone(), it executes the function application fn(arg).
(This differs from fork(2), where execution continues in the child from the point of
an

the fork(2) call.) The fn argument is a pointer to a function that is called by the child process
at the beginning of its execution. The arg argument is passed to the fn function.
sh

The child_stack argument specifies the location of the stack used by the child process. Since
the child and calling process may share memory, it is not possible for the child process to
execute in the same stack as the calling process. The calling process must therefore set up
memory space for the child stack and pass a pointer to this space to clone(). Stacks grow
downwards on all processors that run Linux (except the HP PA processors),
so child_stack usually points to the topmost address of the memory space set up for the child
stack.

The low byte of flags contains the number of the termination signal sent to the parent when
the child dies. If this signal is specified as anything other than SIGCHLD, then the parent
process must specify the __WALL or __WCLONE options when waiting for the child
with wait(2). If no signal is specified, then the parent process is not signaled when the child
terminates.
shannu93.blogspot.in

SIGNALS:
if something unexpected or unpredictable happens:

 a floating-point error
 a power failure
 an alarm clock "ring" (discussed soon)
 the death of a child process
 a termination request from a user (i.e., a Control-C)
 a suspend request from a user (i.e., a Control-Z)

These kind of events are often called interrupts

 i.e., they interrupt the normal flow of the program to service interrupt handler

.i n
When Linux recognizes such event it sends corresponding signal,

ot
 e.g., floating point error: kernel sends offending process signal number 8

sp
og
bl
3.

Signals can send:


 To the kernel
9

 To any process can send any other process a signal as long as it has permission.
nu

 receiving process suspends its current flow of control


 executes signal handler
 resumes original flow when signal handler finishes
an

Signals can use:


 To “protect” our program from Control-C
sh

 To arrange for alarm clock to terminate a long running program


 To let our calendar pop up a window etc...
Signal Types:
 Two signal types
 standard signal (traditional unix signals)
 delivered to a process by setting a bit in a bitmap
 one for each signal
 thus there cannot be multiple instances of the same signal; bit can be one (signal) or
zero (no-signal)
 real-time signals (or queued signals)
 defined by POSIX 1003.1b where successive instances of the same signal are
significant and need to be properly delivered.
 In order to use queued signals, you must use the sigaction () system call, rather
than signal ()
shannu93.blogspot.in

Signals Defining:
 Signals are defined in /usr/include/signal.h
 other platform-specific header files
e.g., /usr/include/asm/signal.h
 Programmer may chose that
 particular signal triggers a user-defined signal handler
 triggers the default kernel-supplied handler
 signal is ignored
Terminal Signals:
 Easiest way to send signal to foreground process
 press Control-C or Control-Z
 when terminal driver recognizes a Control-C it sends SIGINT
 signal to all of the processes in the current foreground job
 Control-Z causes SIGSTP to be sent by default
 SIGINT terminates a process

n
 SIGTSTP suspends a process

.i
ot
sp
og
bl
9 3.
nu
an
sh
shannu93.blogspot.in

UNIT III

OPEN SOURCE DATABASE

My SQL: Introduction:
MySQL is a relational database management system (RDBMS) that runs as a
server providing multi-user access to a number of databases. It is a popular choice of database
for use in web applications, and is a central component of the widely used LAMP web
application software stack—LAMP is an acronym for "Linux, Apache, MySQL, and
Perl/PHP/Python". It is used in some of the most frequently visited web sites on the Internet,
including Flickr, Nokia.com, and YouTube etc.
MySQL is written in C and C++. Its SQL parser is written in yacc, and a
homebrewed lexical analyzer named sql_lex.cc. It works on many different system platforms,

n
including Linux, Mac OS X, Microsoft Windows, AIX, BSDi, FreeBSD, HP-UX,

.i
eComStation, i5/OS, IRIX, NetBSD, Novell NetWare, OpenBSD, OpenSolaris, OS/2 Warp,
QNX, Solaris, Symbian, SunOS, SCO OpenServer, SCO UnixWare, Sanos and Tru64. Many

ot
programming languages with language-specific APIs include libraries for accessing MySQL
databases. These include MySQL Connector/Net for integration with Microsoft's Visual
sp
Studio (languages such as C# and VB are most commonly used) and the ODBC driver for
Java.
og
Setting up account:
bl

When you try to access MySQL database server from client such as mysql or
even programming language such as php or perl you need a user account. MySQL has
3.

sophisticated user management system that controls who can access server and from which
client system. It uses special tables in mysql database. In order to create a new user account
9

you need a MySQL root account password. Next you need to use the GRANT SQL
nu

command to set up the MySQL user account. Finally, use the account's name and password to
make connections to the MySQL server.
Please note that MySQL user accounts are different from UNIX/Linux login accounts.
an

Procedure for setting up a MySQL user account:


sh

Login in as mysql root user (at shell prompt type following command :):
$ mysql -u root -p
OR
$ mysql -u root -h myserver-sever.com -p
Create a new mysql database called demo
mysql> CREATE DATABASE demo;
Create a new user called user1 for database demo
mysql> GRANT ALL ON demo.* TO user1@localhost IDENTIFIED BY
'mypassword';
Note: GRANT ALL means all privileges i.e. user is permitted do anything. She
can read, modify or delete data, but only on tables in the demo database. She
cannot access any other database.
How do I connect to MySQL server using user1 account?
User user1 can connect to mysql server demo database using following
command:
shannu93.blogspot.in

$ mysql -u user1 -p demo


OR
$ mysql -u user1 -h mysql.server.com -p demo
Where,
-u user1: MySQL Username
h : MySQL server name (default is localhost)
-p : Prompt for password
demo: demo is name of mysql database (optional)

Starting and terminating:


To start mysql server:
/etc/init.d/mysqld start
To stop mysql server:
/etc/init.d/mysqld stop
To restart mysql server

n
/etc/init.d/mysqld restart
Tip: Redhat Linux also supports service command, which can be use to start, restart, stop any

.i
service:

ot
# service mysqld start
# service mysqld stop
# service mysqld restart
sp
To start the mysql program, try just typing its name at your command-line prompt. If mysql
og
starts up correctly, you'll see a short message, followed by a mysql> prompt that indicates the
program is ready to accept queries. % mysql
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is
bl

18427 to server version: 3.23.51-log


Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 3
3.

mysql>
9

To terminate a mysql session, issue a QUIT statement:


mysql> QUIT
nu

You can also terminate the session by issuing an EXIT statement or (under Unix) by typing
Ctrl-D.
an

Writing MySQL-Based Programs:


MySQL Client Application Programming Interfaces:
sh

It shows how to write MySQL-based programs using Perl, PHP, Python and it's possible to
use several other languages as well. MySQL APIs provide a standard way for you, the
application developer, to express database operations. Each API translates your instructions
into something the MySQLserver can understand. MySQL client APIs provide the following
capabilities
• Connecting to the MySQL server; selecting a database; disconnecting
from the server:
Every program that uses MySQL must first establish a connection to the server, and most
programs also will specify which database to use. Some APIs expect the database name to be
supplied at connect time. Others provide an explicit call for selecting the database. In
addition, well-behaved MySQL programs close the connection to the server when they're
done with it.
• Checking for errors:
Many people write MySQL programs that perform no error checking at all, which makes
them difficult to debug when things go wrong.
shannu93.blogspot.in

.
• Issuing queries and retrieving results:
The whole point of connecting to a database server is to run queries. Each API
provides at least one way to issue queries, as well as several functions for
processing the results of queries.
• Using prepared statements and placeholders in queries:
One way to write a query that refers to specific data values is to embed the values directly in
the query string. Most APIs provide another mechanism that allows you to prepare a query in
advance that refers to the data values symbolically. When you execute the statement, you
supply the data values separately and the API places them into the query string for you.
• Including special characters and NULL values in queries:
Some characters such as quotes and backslashes have special meaning in queries, and you
must take certain precautions when constructing queries containing them. The same is true
for NULL values. If you do not handle these properly, your programs may generate SQL
statements that are erroneous or that yield unexpected results.
• Handling NULL values in result sets:

n
NULL values are special not only when you construct queries, but in results returned from

.i
queries. Each API provides a convention for dealing with them.

ot
Connecting to the MySQL Server, Selecting a Database, and Disconnecting:
Each API provides functions for connecting and disconnecting. The connectionroutines
sp
require that you provide parameters specifying the MySQL user account you want to use.
The programs in this section show how to perform three fundamental operations that are
og
common to the vast majority of MySQL programs:
• Establishing a connection to the MySQL server:
Every program that uses MySQL does this, no matter which API you use. The details on
bl

specifying connection parameters vary between APIs, and some APIs provide more
flexibility than others.
3.

• Selecting a database:
Most MySQL programs select a database, either when they connect to the
9

server or immediately thereafter.


nu

• Disconnecting from the server:


Each API provides a means of shutting down an open connection. but shutting down the 9
an

connection explicitly allows the server to perform an orderly close on its end immediately.

Record Selection Techniques:


sh

Specifying Which Columns to Display:


To indicate what kind of information you want to see from a table, name a
column or a list of columns and the table to use.
SELECT * FROM `student`
Id name class mark
1 John Deo Four 75
2 Max Ruin Three 85
We have not placed any restriction here and asked for all the fields with all the records. Now
if we want to restrict our results and get only name field from the table.
SELECT name FROM `student`
This will return only name field from the table. We can ask for more fields & all field names
we have to separate by comma. We can include as many field names we want from a table.
SELECT name, class FROM `student`
Giving Names to Output Columns
Whenever you retrieve a result set, MySQL gives every output column a name.
shannu93.blogspot.in

MySQL assigns default names to output columns, but if the defaults are not suitable, you can
use column aliases to specify your own names. To give a result set column a name of your
own choosing, use AS name to specify a column alias. The following query retrieves the
same result as the previous one, but renames the first column to date_sent:
mysql> SELECT
-> DATE_FORMAT(t,'%M %e, %Y') AS date_sent,
-> srcuser, size FROM mail;
+--------------+---------+---------+
| date_sent | srcuser | size |
+--------------+---------+---------+
| May 11, 2001 | barb | 58274 |
| May 12, 2001 | tricia | 194925 |

Combining Columns to Construct Composite Values 0


One way to do this is to use CONCAT( ). Column values may be combined to produce
composite output values.

n
CONCAT(srcuser,'@',srchost)

.i
ot
How do I select specific records:
The statement below will select all columns (*) from the tablename you specify, where the
column you specify contains the value you specify.
SELECT * FROM tablename WHERE column = '' sp
og
When specifing value, be sure that unless the values are numbers, that you quote them. You
may also want to quote the value so that any quotes within the value are escaped.
SELECT * FROM tablename WHERE age > 40 && age < 50
bl

SELECT * FROM tablename WHERE name = 'John'


mysql> SELECT t, srcuser, srchost FROM mail WHERE srchost LIKE 's%';
3.

+---------------------+---------+---------+
| t | srcuser | srchost |
9

+---------------------+---------+---------+
nu

| 2001-05-11 10:15:08 | barb | saturn |


| 2001-05-13 13:59:18 | barb | saturn |
an

| 2001-05-14 17:03:01 | tricia | saturn |


Queries such as the preceding one that test a given column to see if it has any of several
different values often can be written more easily by using the IN( ) operator. IN( ) is true if
sh

the column is equal to any value in its argument list:


mysql> SELECT t, srcuser, dstuser FROM mail
-> WHERE srcuser IN ('gene','barb','phil');
+---------------------+---------+---------+
| t | srcuser | dstuser |
+---------------------+---------+---------+
| 2001-05-11 10:15:08 | barb | tricia |
| 2001-05-12 15:02:49 | phil | phil |

Working with Strings:


Like most data types, strings can be compared for equality or inequality or relative
ordering. However, strings have some additional properties to consider:
• Strings can be case sensitive (or not), which can affect the outcome of string operations.
• You can compare entire strings, or just parts of them by extracting substrings.
• You can apply pattern-matching operations to look for strings that have a certain structure.
shannu93.blogspot.in

Types of Strings:
MySQL can operate on regular strings or binary strings. "Binary" in this context has
little to do with the presence of non-ASCII values, so it's useful right at the outset to make a
distinction:
• Binary data may contain bytes that lie outside the usual range of printable ASCII
characters.
• A binary string in MySQL is one that MySQL treats as case sensitive in comparisons. For
binary strings, the characters A and a are considered different. For non-binary strings, they're
considered the same.
The following table, metal, is used in several sections of this chapter:
mysql> SELECT * FROM metal;
+----------+
| name |
+----------+
| copper |

n
| gold |
| iron |

.i
| lead |

ot
| mercury |
| platinum |
| silver |
| tin |
sp
og
+----------+
Working with Dates and Times:
MySQL's Date and Time Formats
bl

MySQL provides DATE and TIME column types for representing date and time values
3.

separately, and a DATETIME type for combined date-and-time values.


These values have the following formats:
9

• DATE values are handled as strings in CCYY-MM-DD format, where CC, YY, MM, and DD
represent the century, year within century, month, and day parts of the date.
nu

• TIME values are represented as strings in hh:mm:ss format, where hh, mm, 2
and ss are the hours, minutes, and seconds parts of the time. TIME values
an

often can be thought of as time-of-day values, but MySQL actually treats them
as elapsed time. Thus, they may be greater than 23:59:59 or even negative.
(The actual range is -838:59:59 to 838:59:59.)
sh

• DATETIME values are represented as combined date-and-time strings in


CCYY-MM-DD hh:mm:ss format.
• TIMESTAMP values also include date and time parts, but are represented as
strings in CCYYMMDDhhmmss format.
Date and Time Functions:
Name Description
ADDDATE() Add time values (intervals) to a date value
ADDTIME() Add time
CURDATE() Return the current date
CURTIME() Return the current time
DATE_ADD() Add time values (intervals) to a date value
DATE_FORMAT() Format date as specified
DATE_SUB() Subtract a time value (interval) from a date
DATE() Extract the date part of a date or datetime expression
DATEDIFF() Subtract two dates
shannu93.blogspot.in

DAYNAME() Return the name of the weekday


DAYOFMONTH() Return the day of the month (0-31)
DAYOFWEEK() Return the weekday index of the argument
DAYOFYEAR() Return the day of the year (1-366)
EXTRACT() Extract part of a date
FROM_DAYS() Convert a day number to a date
FROM_UNIXTIME() Format UNIX timestamp as a date
GET_FORMAT() Return a date format string
HOUR() Extract the hour
LAST_DAY Return the last day of the month for the argument
MAKEDATE() Create a date from the year and day of year
MAKETIME MAKETIME()
MICROSECOND() Return the microseconds from argument
MINUTE() Return the minute from the argument
MONTH() Return the month from the date passed
MONTHNAME() Return the name of the month

n
NOW() Return the current date and time.

.i
ot
ADDTIME(expr1,expr2):
ADDTIME() adds expr2 to expr1 and returns the result. expr1 is a time or datetime
expression, and expr2 is a time expression.
sp
mysql> SELECT ADDTIME('2007-12-31 23:59:59.999999', '11:1:1.000002'); ->
og
'2008 01 02 01:01:01.000001'
mysql> SELECT ADDTIME('01:00:00.999999', '02:00:00.999998');-> '03:00:01.999997'
-> '2004-01-01 13:00:00'
bl

CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending
3.

on whether the function is used in a string or numeric context. mysql> SELECT


CURDATE(); -> '2008-06-13'mysql> SELECT CURDATE() + 0; -> 2008 06 13
9

CURTIME():
nu

Returns the current time as a value in 'HH:MM:SS' or HHMMSS.uuuuuu format, depending


on whether the function is used in a string or numeric context. The value is expressed in the
an

current time zone. mysql> SELECT CURTIME();-> '23:50:26' mysql> SELECT


CURTIME() + 0; -> 235026.000000· DATE(expr)
Extracts the date part of the date or datetime expression expr.mysql> SELECT
sh

DATE('2003-12-31 01:02:03'); -> '2003-12-31'· DATEDIFF(expr1,expr2) DATEDIFF()


returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and
expr2 are date or date-and-time expressions. Only the date parts of the values are used in the
calculation. mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30'); -> 1
mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31'); -> -31
· DATE_FORMAT(date,format) Formats the date value according to the format string.
The following specifiers may be used in the format string. The “%” character is required
before format specifier characters.
Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
shannu93.blogspot.in

%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
Specifier Description
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)

n
%U Week (00..53), where Sunday is the first day of the week

.i
%u Week (00..53), where Monday is the first day of the week

ot
%V Week (01..53), where Sunday is the first day of the week; used with %X
%v Week (01..53), where Monday is the first day of the week; used with %x
%W
%w sp
Weekday name (Sunday..Saturday)
Day of the week (0=Sunday..6=Saturday)
og
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used
with %V
%x Year for the week, where Monday is the first day of the week, numeric, four
bl

digits; used with %v


%Y Year, numeric, four digits
3.

%y Year, numeric (two digits)


%% A literal “%” character
9

%x x, for any “x” not listed above


nu

mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y'); -> 'Sunday


October 2009'
an

mysql> SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s'); -> '22:23:00'


mysql> SELECT DATE_FORMAT('1900-10-04 22:23:00', -> '%D %y %a %d %m %b
%j'); -> '4th 00 Thu 04 10 Oct 277'
sh

mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', -> '%H %k %I %r %T %S


%w'); -> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V'); -> '1998 52'
mysql> SELECT DATE_FORMAT('2006-06-00', '%d'); -> '00' · DAYNAME(date)
Returns the name of the weekday for date. The language used for the name is controlled by
the value of the lc_time_names system variable mysql> SELECT DAYNAME('2007-02-
03'); -> 'Saturday'
DAYOFMONTH(date):
Returns the day of the month for date, in the range 1 to 31, or 0 for dates such as '0000-00-
00' or '2008-00-00' that have a zero day part.
mysql> SELECT DAYOFMONTH('2007-02-03'); -> 3· DAYOFWEEK(date)
Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday).
These index values correspond to the ODBC standard.
mysql> SELECT DAYOFWEEK('2007-02-03');
-> 7
shannu93.blogspot.in

· DAYOFYEAR(date) Returns the day of the year for date, in the range 1 to 366.
mysql> SELECT DAYOFYEAR('2007-02-03'); -> 34· EXTRACT(unit FROM date)
The EXTRACT() function uses the same kinds of unit specifiers as DATE_ADD() or
DATE_SUB(), but extracts parts from the date rather than performing date arithmetic.
mysql> SELECT EXTRACT(YEAR FROM '2009-07-02'); -> 2009
mysql> SELECT EXTRACT(YEAR_MONTH FROM '2009-07-02 01:02:03'); -> 200907
mysql> SELECT EXTRACT(DAY_MINUTE FROM '2009-07-02 01:02:03'); -> 20102
mysql> SELECT EXTRACT(MICROSECOND -> FROM '2003-01-02
10:30:00.000123'); -> 123

Sorting Query Results:


Using ORDER BY to Sort Query Results
ORDER BY has the following general characteristics:
• You can sort using a single column of values or multiple columns
• You can sort any column in either ascending order (the default) or descending order

n
• You can refer to sort columns by name, by their position within the output column list, or
by using an alias

.i
Here are animal birthdays, sorted by date:

ot
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
sp
og
| Buffy | 1989-05-13 |
| Bowser | 1989-08-31 |
| Fang | 1990-08-27 |
bl

| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
3.

| Slim | 1996-04-29 |
9

The default sort order is ascending, with smallest values first. To sort in reverse (descending)
order, add the DESC keyword to the name of the column you are sorting by:
nu

mysql> SELECT name, birth FROM pet ORDER BY birth DESC;


+----------+------------+
an

| name | birth |
+----------+------------+
| Puffball | 1999-03-30 |
sh

| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
+----------+------------+

Sorting in User-Defined Orders


You want to define the sort order for all values in a column.
Use FIELD( ) to map column values onto a sequence that places the values in the desired
order.
FIELD( ) compares its first argument to the following arguments and returns a number
indicating which one of them it matches. The following FIELD( ) call compares value to str1,
str2, str3, and str4, and returns 1, 2, 3, or 4, depending on which one of them value is equal
to: FIELD(value,str1,str2,str3,str4)

Sorting Values ENUM:


shannu93.blogspot.in

ENUM values don't sort like other string columns.


ENUM is considered a string column type, but ENUM values have the special property that
they are stored numerically with values ordered the same way they are listed in the table
definition. These numeric values affect how enumerations are sorted, which can be very
useful. Suppose you have a table named weekday containing an enumeration column day that
has weekday names as its members:
CREATE TABLE weekday
(
day ENUM('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday')
);

Generating Summaries:
GROUP BY (Aggregate) Functions
Aggregate (GROUP BY) Functions
Name Description

n
AVG() Return the average value of the argument

.i
BIT_AND() Return bitwise and

ot
BIT_OR() Return bitwise or
BIT_XOR() Return bitwise xor
COUNT(DISTINCT)
COUNT()
GROUP_CONCAT()
sp
Return the count of a number of different values
Return a count of the number of rows returned
Return a concatenated string
og
MAX() Return the maximum value
MIN() Return the minimum value
bl

STDDEV() Return the population standard deviation


SUM() Return the sum
3.

VARIANCE() Return the population standard variance


9

Generating and Using Sequences:


nu

Using AUTO_INCREMENT To Set Up a Sequence Column


You want to include a sequence column in a table.
an

Use an AUTO_INCREMENT column. Unique IDs would be helpful to make the records
distinct and to provide values that make each record easy to refer to. An
AUTO_INCREMENT column is good for this purpose, so a better insect table has a
sh

structure like this:


CREATE TABLE insect
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
name VARCHAR(30) NOT NULL, # type of insect
date DATE NOT NULL, # date collected
origin VARCHAR(30) NOT NULL # where collected
);
To generate a new sequence value, insert NULL into the column, or just omit it from your
INSERT statement. Either way, MySQL will create a new sequence number for you.
mysql> INSERT INTO insect (id,name,date,origin) VALUES
-> (NULL,'housefly','2001-09-10','kitchen'),
-> (NULL,'millipede','2001-09-10','driveway'),
mysql> SELECT * FROM insect ORDER BY id;
+----+-------------------+------------+------------+
shannu93.blogspot.in

| id | name | date | origin |


+----+-------------------+------------+------------+
| 1 | housefly | 2001-09-10 | kitchen |
| 2 | millipede | 2001-09-10 | driveway |

Using LAST_INSERT_ID( ) to Obtain AUTO_INCREMENT Values:


MySQL provides a LAST_INSERT_ID( ) function as a simpler way to obtain the proper
value. It returns the most recent AUTO_INCREMENT value that you generated during the
time you've been connected to the server. For example, you can insert a record into the insect
table, then retrieve its id value like this:
mysql> INSERT INTO insect (name,date,origin) -> VALUES('cricket','2001-09-
11','basement');
mysql> SELECT LAST_INSERT_ID( );
+------------------+
| last_insert_id( ) |
+------------------+

n
|9|

.i
+------------------+

ot
Reasons to Avoid Resequencing
Before deciding to resequence an AUTO_INCREMENT column, consider whether you really

sp
want or need to do so. It's unnecessary in most cases. In fact, renumbering a sequence
sometimes can cause you real problems. For example, you should not resequence a column
og
containing values that are referenced by another table. Renumbering the values destroys their
correspondence to values in the other table, making it impossible to properly relate records in
the two tables to each other.
bl

Reasons that people have for resequencing include the following:


• Aesthetics.
3.

Sometimes the desire to renumber a column is for aesthetic reasons. People seem to prefer
unbroken sequences to sequences with holes in them. If this is why you want to resequence,
9

there's probably not much I can say to convince you otherwise. Nevertheless, it's not a
nu

particularly good reason.


• Performance.
The impetus for resequencing may stem from the notion that doing so "compacts" a sequence
an

column by removing gaps and allows MySQL to run queries more quickly. This is not true.
MySQL doesn't care whether or not there are holes, and there is no performance gain to be
sh

had by renumbering an AUTO_INCREMENT column. In fact, resequencing affects


performance negatively in the sense that the table remains locked while MySQL performs the
operation—which may take a non-trivial amount of time for a large table. Other clients can
read from the table while this is happening, but clients that
are trying to insert new rows must wait until the operation is complete.
• Running out of numbers.
The upper limit of a sequence column is determined by the column's data type. If an
AUTO_INCREMENT sequence is approaching the upper limit of its column type,
renumbering packs the sequence and frees up more values at the top. This may be a
legitimate reason to resequence a column, but it is still unnecessary in many cases to do so.
You may be able to expand the column's range to increase its upper limit, without changing
the values stored in the column.
Starting a Sequence at a Particular Value
Sequences start at 1, but you want to use a different starting value. Add an
AUTO_INCREMENT clause to your CREATE TABLE statement when you create the table.
shannu93.blogspot.in

If the table has already been created, use an ALTER TABLE statement to set the starting
value. By default, AUTO_INCREMENT sequences start at one:
mysql> CREATE TABLE t
-> (id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id));
mysql> INSERT INTO t (id) VALUES(NULL);
mysql> INSERT INTO t (id) VALUES(NULL);
mysql> INSERT INTO t (id) VALUES(NULL);
mysql> SELECT id FROM t ORDER BY id;
+----+
| id |
+----+
|1|
|2|
|3|
+----+

.i n
ot
sp
og
bl
9 3.
nu
an
sh

Vous aimerez peut-être aussi