Sunday, August 26, 2012

Views


Views
- a VIEW is a virtual relation based on the result-set of a SELECT statement.
- it has a column and rows that comes from the real tables.

Create Table
Command:
            CREATE VIEW view_name AS SELECT column_name FROM table_name;
Example:
            CREATE VIEW sample AS SELECT first_name FROM student;

            You can select a multiple column in view and from different table. All the select query can use in view. Also, you can use the WHERE clause.

Example:
            CREATE VIEW sample AS SELECT first_name, section_name
            FROM student, section
            WHERE student.id=section.stud_id;

            You can use the concatenation to place a value of multiple columns to one column in views.

Example:
            CREATE VIEW sample AS SELECT concat(first_name,min_name,last_name)
            FROM student;

Insert value into Views
            You can insert a value/data into a view table. Just like the insertion in the typical table in mysql.

Command:
            INSERT INTO sample
VALUES (‘sheryll’, ‘BSIT’);

Drop View
You can also drop a view table. If you drop the view table, it does not affect the original table, but if you drop the original table that the column in the view comes from, the view table can be deleted.

Command:
            DROP VIEW student;

Monday, August 13, 2012

Running MYSQL in Batch Mode


The MySQL software also allows you to run queries without entering the mysql client itself. To do so is to use mysql in what is called batch mode. Instead of entering queries directly, you can store them in a text file and then run the text file through mysql.

Command:
mysql -u username -p databasename < '/path/to/filename.txt'

example:
mysql –u root sampledb <c:\query.txt

If you want to save the result of a batch file as its own text file, add > '/path/to/output.txt to your code. The line bin/mysql -u username -p < '/path/to/input.txt' > '/path/to/output.txt'

example:
mysql –u root rhams <c:\query.txt > d:\output.txt


Batch files can also be run from within the mysql client, if you'd prefer. Just log in, select your database, and then type:

source/path/to/filename


Querying MySQL


SELECT - this term is used to return rows of records that meet certain criteria.
Command: SELECT whatcolumns FROM whattable

Example of SELECT QUERY
            SELECT name FROM student

ASTERISK
The asterisk (*) select all the data from the chosen table.
Command: SELECT * from student

We can list a set of columns we would like to return. You should specify the column names as a list of comma-separated values.
Command: SELECT column1, column2, FROM table_name

Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block.
SELECT column_name FROM table_name WHERE condition_statement.

You can used symbols in condition such as =, !=, < >, <, <=, >, >=. + and - sign is used in incrementing like age++ or age--.

AND, OR, NOT
            This is used in WHERE conditions. AND is used if you have one or more conditions, OR is used if you want to show only one. Not is used if you don’t want to show a particular data.
Command:
            SELECT column_name FROM table_name WHERE condition1 AND/OR/NOT condition2
Example:
            SELECT course FROM student WHERE degree=”BSIT” AND degree=”COMSCI”

LIKE
            This is pattern matching. This is used in text only. It used a % symbol.
Command:
            SELECT column_name FROM table_name WHERE column_name LIKE ‘%char’.
Example:
            SELECT name FROM student WHERE name LIKE ‘s%’.
                        -it means that all names start from the letter S will show.
            %s = all names that end in a letter s
            S%m = all names that starts in a letter S and ends with a letter m

AGGREGATE OPERATIONS

SUM() = this will sum all the value in the column.
            Ex. SELECT SUM(age) FROM student

AVERAGE = this will compute the average of all the value in the column.
            Ex. SELECT AVERAGE(age) FROM student

MIN() = it means minimum.
            Ex. SELECT MIN (age) FROM student
            - this command shows the youngest age in the table student.

MAX() = maximum
            Ex. SELECT MAX(age) FROM student
            - this command shows the oldest age in the table student.

COUNT()
            Ex. SELECT COUNT(age) FROM student
            - this will count all the number of age in a table student

DISTINCT
If you don’t want to show a duplicate data, you can use distinct. It can show the unique data
Command:
            SELECT DISTINCT(column_name) FROM table_name
Example:
            SELECT DISTINCT(degree) FROM student

GROUP BY
            This is for grouping a data in a selected data.
Command:
            SELECT * FROM table_name GROUP BY column_name
Example:
            SELECT * FROM student GROUP BY age

HAVING
            This is used in grouping data, this is accompanied by GROUP BY query.
Command:
            SELECT column_name FROM table_name GROUP BY column_name HAVING condition
Example:
            SELECT age FROM student GROUP BY age HAVING age>10

ORDER BY
            This is used in sorting data. You can use ASC in ascending order or desc in descending order.
Command:
            SELECT column_name FROM table_name ORDER BY column_name
Example:
            SELECT name FROM student ORDER BY age desc
- this command means that it will sort the name by age in descending order meaning oldest to youngest.