MYSQL
MySQL is a database.The data in MySQL is stored in database objects called tables.A table is a collections of related data entries and it consists of columns and rows.Databases are useful when storing
information categorically. A company may have a database with the following tables: "Employees", "Products", "Customers" and "Orders".
INSTALLATION
It's simple to install MySQL on Linux using the RPM file.
1. Become the superuser if you are working in your account. (Type "su" and the prompt and give the root password).
2. Change to the directory that has the RPM download.
3. Type the following command at the prompt:
rpm -ivh "mysql_file_name.rpm"
Similarly you can also install the MySQL client and MySQL development RPMs if you've downloaded them.
Alternatively, you can install the RPMs through GnoRPM (found under System).
4.Now we'll set a password for the root user. Issue the following at the prompt:
mysqladmin -u root password mysqldata
5.It is now time to test the programs. Typing the following at the prompt starts the mysql client program. The system asks for the the
password.Type the root password (mysqldata).If you don't get the prompt for password, it might be because MySQL Server is not running. To start
the server, change to /etc/rc.d/init.d/ directory and issue the command ./mysql start (or mysql start depending on the value of the PATH variable on your system). Now invoke mysql client program.
6.Once MySQL client is running, you should get the mysql> prompt. Type
the following at this prompt:show databases;
7. You should now get a display similar to:
We've successfully installed MySQL on your system
I
The MySQL server configuration normally started during installation process . The MySQL configuration wizard can be launched by clicking the MySQL server instance configuration wizard entry in the
MySQL section of the window start menu. Another way to launch the configuration wizard, you can launch the MySQLInstanceConfig.exe file directly from bin directory of your MySQL installation. The MySQL server configuration wizard sets configuration variables values in my.ini file in the installation
directory for the MySQL server. --defaults-file="C:\Program Files\MySQL\MySQL Server 5.0\my.ini"
Path "C:\Program Files\MySQL\MySQL Server 5.0" is installation directory of MySQL server . The --defaults-file option instructs the MySQL server to read the specified file for configuration options when
it starts. The MySQL client and utilities like mysql and mysqldump command-line client are unable to locate the
my.ini file locate in the server installation directory. The MySQL use window server 2003, window server 2000 and window XP, MySQL server configuration wizard will configure with MySQL quickly
without having to make many decisions about server configuration.
CONFIGURATION
The MySQL server
[mysqld]
port= 3306
socket= /opt/lampp/var/mysql/mysql.sock
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
Where do all the plugins live
plugin_dir = /opt/lampp/lib/mysql/plugin/
Don't listen on a TCP/IP port at all. This can be a security enhancement,
if all processes that need to connect to mysqld run on the same host.
All interaction with mysqld must be made via Unix sockets or named pipes.
Note that using this option without enabling named pipes on Windows
(via the "enable-named-pipe" option) will render mysqld useless!
REPLICATION SLAVE
Two methods to configure the host as a replication slave
1) Use the CHANGE MASTER TO command (fully described in our manual) -
The syntax is:
CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
where you replace <host>, <user>, <password> by quoted strings and
<port> by the master's port number (3306 by default).
Example:
CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
MASTER_USER='joe', MASTER_PASSWORD='secret';
Set the variables below. However, in case you choose this method, then start replication for the first time (even unsuccessfully, for example
if you mistyped the password in master-password and the slave fails to connect), the slave will create a master.info file, and any later
change in this file to the variables' values below will be ignored and overridden by the content of the master.info file, unless you shutdown
the slave server, delete master.info and restart the slaver server.
QUERIES
A query is a question or a request.
With MySQL, we can query a database for specific information and have a recordset returned.
EXAMPLE:
SELECT LastName FROM Persons
CREATE
The Create command is used to create a table by specifying the tablename, fieldnames and constraints as shown below.
Syntax:
$createSQL=("CREATE TABLE tblName");
Example:
$createSQL=("CREATE TABLE tblstudent(fldstudid int(10) NOTNULL AUTO_INCREMENT PRIMARY KEY,fldstudName VARCHAR(250) NOTNULL,fldstudentmark int(4) DEFAULT '0' ");
SELECT
The Select command is used to select the records from a table using its field names. To select all the fields in a table, '*' is used in the command.
Syntax:
$selectSQL=("SELECT field_names FROM tablename");
Example:
$selectSQL=("SELECT * FROM tblstudent");
DELETE
The Delete command is used to delete the records from a table using conditions as shown below:
Syntax:
$deleteSQL=("DELETE * FROM tablename WHERE condition");
Example:
$deleteSQL=("DELETE * FROM tblstudent WHERE fldstudid=2");
INSERT
The Insert command is used to insert records into a table. The values are assigned to the field names as shown below:
Syntax:
$insertSQL=("INSERT INTO tblname(fieldname1,fieldname2..) VALUES(value1,value2,...) ");
Example:
$insertSQL=("INSERT INTO Tblstudent(fldstudName,fldstudmark)VALUES(Baskar,75) ");
DELETE
The Update command is used to update the field values using conditions. This is done using 'SET' and the fieldnames to assign new values to them.
Syntax:
$updateSQL=("UPDATE Tblname SET (fieldname1=value1,fieldname2=value2,...) WHERE fldstudid=IdNumber");
Example:
$updateSQL=("UPDATE Tblstudent SET (fldstudName=siva,fldstudmark=100) WHERE fldstudid=2");
DROP
The Drop command is used to delete all the records in a table using the table name as shown below:
Syntax:
$dropSQL=("DROP tblName");
Example:
$dropSQL=("DROP tblstudent");
UPDATE
The update statement is used to change values that are already in a table.
SYNTAX:
UPDATE <table name>
SET <attribute> = <expression>
WHERE <condition>;
Example:
UPDATE STUDENT SET Name = ‘Amar’
WHERE StudID=1001;
TRUNCATE
It is used to delete the table.
SYNTAX:
truncatae table table_name
AS
Syntax:
SELECT <columns>FROM <existing_table_name>AS <new_table_name>
Example: SELECT t1.name -> FROM artists -> AS t1;
Explanation: It is used to create a shorthand reference to elements with long names to make the SQL statements shorter and reduce the chance of typos in the longer names.
JOINS
SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.Tables in a database are often related to each other with keys.A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
DIFFERENT SQL JOINS
JOIN: Return rows when there is at least one match in both tables
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
FULL JOIN: Return rows when there is a match in one of the tables
INNER JOIN
The INNER JOIN keyword return rows when there is at least one match in both tables.
SQL INNER JOIN Syntax
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name;
EXAMPLE:
LASTNAME FIRST NAME ORDER NO.
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
LEFT JOIN
The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).
SQL LEFT JOIN Syntax:
SELECT column_name(s) FROM table_name1
LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
RIGHT JOIN
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).
SQL RIGHT JOIN Syntax:
SELECT column_name(s) FROM table_name1
RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
FULL JOIN
The FULL JOIN keyword return rows when there is a match in one of the tables.
SQL FULL JOIN Syntax:
SELECT column_name(s) FROM table_name1
FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name
CONSTRAINTS
Constraints are used to limit the type of data that can go into a table.Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement).
We have the following constraints:
* NOT NULL
* UNIQUE
* PRIMARY KEY
* FOREIGN KEY
* CHECK
* DEFAULT
FUNCTIONS
AGGERATE FUNCTIONS SCALAR FUNCTIONS
AGGERATE FUNCTIONS
SQL aggregate functions return a single value, calculated from values in a column.
AVG() - Returns the average value COUNT() - Returns the number of rows FIRST() - Returns the first value LAST() - Returns the last value MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum
SCALAR FUNCTIONS
SQL scalar functions return a single value, based on the input value.
* UCASE() - Converts a field to upper case
* LCASE() - Converts a field to lower case
* MID() - Extract characters from a text field
* LEN() - Returns the length of a text field
* ROUND() - Rounds a numeric field to the number of decimals specified
* NOW() - Returns the current system date and time
* FORMAT() - Formats how a field is to be displayed