Académique Documents
Professionnel Documents
Culture Documents
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.
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
Principle2:
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.
The Open Source Definition is used by the Open Source Initiative to determine
an
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.
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
endeavor. For example, it may not restrict the program from being used in a business, or from
9
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
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.
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
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
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.
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:
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
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
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.
Scheduling Parameters:
nu
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
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
Linux Personalities:
sh
Personalities:
SYNOPSIS:
(or)
#include <sys/personality.h>
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
The main use of clone() is to implement threads: multiple threads of control in a program that
9
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)
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.
To any process can send any other process a signal as long as it has permission.
nu
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
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
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
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
mysql>
9
You can also terminate the session by issuing an EXIT statement or (under Unix) by typing
Ctrl-D.
an
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
connection explicitly allows the server to perform an orderly close on its end immediately.
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 |
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
+---------------------+---------+---------+
| t | srcuser | srchost |
9
+---------------------+---------+---------+
nu
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.
• 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
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.
CURTIME():
nu
%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
· 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
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
| name | birth |
+----------+------------+
| Puffball | 1999-03-30 |
sh
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
+----------+------------+
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
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
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
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
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
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