Você está na página 1de 5

Lovely Professional University

‘Phagwaa’

Homework #I
CAP304: Open Source Technology

Submitted To: Submitted By:


Mr. Akash Bhardwaj Chirag Thakkar
Roll No. RE37D1B52
Reg. No 3010070196
BCA-MCA (int.)

Date 31-Jan-2011
Lovely Professional University
‘Phagwaa’

Part-A

Q1. How can you compare SQL and MySQL?

Ans. MySQL is a relational database management system (RDBMS)[1] that runs as a server
providing multi-user access to a number of databases. It is named after developer Michael
Widenius' daughter, My. The SQL phrase stands for Structured Query Language.[3]

The MySQL development project has made its source code available under the terms of
the GNU General Public License, as well as under a variety of proprietary agreements. MySQL
was owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now
owned by Oracle Corporation.

SQL often referred to as Structured Query Language, is a database computer language designed
for managing data in relational database management systems (RDBMS), and originally based
upon relational algebra and calculus.[2] Its scope includes data insert, query, update and
delete, schema creation and modification, and data access control. SQL was one of the first
commercial languages for Edgar F. Codd's relational model, as described in his influential 1970
paper, "A Relational Model of Data for Large Shared Data Banks".[3] Despite not adhering
to the relational model as described by Codd, it became the most widely used database
language

Q2. How can you manage multi-user access in MySQL through locking?

Ans. When a user votes, the script makes a query to check if someone has already voted for the
same ID/product. If nobody has voted, then it makes another query and insert the ID into a
general ID votes table and another one to insert the data into a per user ID votes table. And this
kind of behaviour is repeated in other kind of scripts.

Q3. Why is it said that MySQL works in a layered structure?

Ans. When you want to reverse engineer an existing MySQL database into DeZign for
Databases you can connect to the database directly or you can make a database structure dump
and import that structure dump. A dump will contain SQL statements to create the table and/or
populate the table.

Date 31-Jan-2011
Lovely Professional University
‘Phagwaa’
There are several ways to create a MySQL database structure dump. You can use the MySQL
utility "mysqldump" or you can use PHPMyAdmin.

The mysqldump utility is a utility to dump a database or a collection of the database for backup
or for transferring the data to another SQL server (not necessarily a MySQL server).

shell> mysqldump [OPTIONS] database [tables]

OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]

OR mysqldump [OPTIONS] --all-databases [OPTIONS]

If you don't give any tables or use the --databases or --all-databases, the whole database(s) will
be dumped.

Part-B

Q4. Elaborate ACID concept of MySQL. What are these features and also discuss there
advantages.

Ans. The addition of MySQL transactions was a major step towards making MySQL a serious
option for mission critical database applications, because it allowed developers to ensure
consistency in accounting type database tables in the way that they were accustomed to doing it
in the large commercial databases like Oracle and DB2, and Microsoft SQL.

Using transactions, queries that modify interdependent values in one or more database tables
can be bound together as a unit, and can either succeed or fail as a unit. The most desirable
aspect of this is that if any query in the set fails, all tables involved are restored, or “rolled
back” to their original state (before the transaction began).

Transactions are widely used in the banking and financial industry to maintain the integrity of
financial records in applications that may be performing many thousands or millions of
operations at any given moment. Transactions ensure that if there is a power outage or a server
crashes, those thousands of interrupted operations will not leave the bank's database tables in an
unresolvable mess. n

As is the case with many operations that take place within the database server, such as triggers
and stored procedures, it would be possible to code this functionality into the application itself.
However, used correctly, transactions offer advantages over application code. They need only
be written once, and then can be called quickly from any place in the application code.

They are portable and platform independent, and can execute more quickly than application
code.

Date 31-Jan-2011
Lovely Professional University
‘Phagwaa’
MySQL transactions can be rolled back when MySQL errors are detected at the application
level. Transactions can also be rolled back in response to conditional statements within the
application code, as we will demonstrate in our PHP example.

Before looking at the PHP code, lets talk a bit about how transactions work in MySQL.
Transactions in MySQL, used with InnoDB tables, are ACID compliant. This means that
transaction have the following properties:

1. Atomicity. Transactions are atomic. This means that they succeed or fail as a unit.
2. Consistency. Your database table will be in a consistent state before after transactions
execute.
3. Isolation. Transactions occur in isolation from one another. InnoDB tables use row
level locking so that data is not modified as a transaction is taking place.
4. Durability. Once a transaction is committed, it is recorded permanently in the database.

Q5. Write the role of InnoDB component of MySQL.

Ans. InnoDB storage engine releases are numbered with version numbers independent of
MySQL release numbers. The initial release of the InnoDB storage engine is version 1.0, and it
is designed to work with MySQL 5.1. Version 1.1 of the InnoDB storage engine is for MySQL
5.5 and up.

• The first component of the InnoDB storage engine version number designates a major
release level.

• The second component corresponds to the MySQL release. The digit 0 corresponds to
MySQL 5.1. The digit 1 corresponds to MySQL 5.5.

• The third component indicates the specific release of the InnoDB storage engine (at a
given major release level and for a specific MySQL release); only bug fixes and minor
functional changes are introduced at this level.

• Once you have installed the InnoDB storage engine, you can check its version number
in three ways:

• In the error log, it is printed during startup

• SELECT * FROM information_schema.plugins;


• SELECT @@innodb_version;

Q6. How can we limit the number of rows in a MySQL query?

Date 31-Jan-2011
Lovely Professional University
‘Phagwaa’
Ans. Every now and then you need to limit the number of rows MySQL returns, i.e. use the
LIMIT clause. Result set pagination is by far the most often usage of LIMIT clause, since you
usually want to select only rows you’ll be displaying on certain page.
The problem is that for pagination you also need total number of rows in a result set, so you
know how many pages you’ll have. This usually means that you need to execute query two
times. First query is for counting total number of rows without LIMIT. Second query is exactly
the same as the first, just without LIMIT and it will actually retrieve required data. You would
need two queries like these:
SELECT COUNT(*) FROM users WHERE name LIKE 'a%';

SELECT name, email FROM users WHERE name LIKE 'a%' LIMIT 10;

Now, this is not such a big problem when you have small result sets and/or simple queries.
But if you have a complex query that joins several tables and takes a while to execute – well,
you probably wouldn’t want to execute it twice and waste server resources.
Luckily since MySQL 4.0.0 you can use SQL_CALC_FOUND_ROWS option in your query
which will tell MySQL to count total number of rows disregarding LIMIT clause. You still
need to execute a second query in order to retrieve row count, but it’s a simple query and not
as complex as your query which retrieved the data.
Usage is pretty simple. In you main query you need to add SQL_CALC_FOUND_ROWS
option just after SELECT and in second query you need to use FOUND_ROWS() function to
get total number of rows. Queries would look like this:
SELECT SQL_CALC_FOUND_ROWS name, email FROM users WHERE name LIKE 'a%'
LIMIT 10;

SELECT FOUND_ROWS();

The only limitation is that you must call second query immediately after the first one because
SQL_CALC_FOUND_ROWS does not save number of rows anywhere.

Date 31-Jan-2011

Você também pode gostar