Archive:PHP and MySQL Programming/Basic Queries: Difference between revisions

wikademia>Jguk
mNo edit summary
wikademia>Wikademia
 
(21 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Programming:PHP and MySQL|TOC]]
== Introduction ==
== Introduction ==


Here are a few basic queries to get you going. For more detailed information, have a look at the online documentation on the MySQL website http://dev.mysql.com/doc/refman/5.1/en/index.html .
Here are a few basic queries to get you going. For more detailed information, have a look at the online documentation on the MySQL website http://dev.mysql.com/doc/refman/5.1/en/index.html .
 
{{?}}
== SELECT ==
== SELECT ==


The SELECT query is the standard means by which to retrieve information from the database. The syntax for the SELECT query is as follows:
The SELECT query is the standard means by which to retrieve information from the database. The syntax for the SELECT query is as follows:


  mysql> SELECT `field1`, `field2`, ...  
  mysql> SELECT field1, field2, ...  
         FROM `table_name`
         FROM table_name
         [WHERE condition1
         [WHERE condition1
             AND condition2
             AND condition2
             ...
             ...
         [ORDER BY `field1`, `field2`, etc...]
         [ORDER BY field1, field2, etc...]
         [LIMIT number]];
         [LIMIT number]];


Example :  
Example :  


  mysql> SELECT * FROM `books`;
  mysql> SELECT * FROM books;
   
   
  mysql> SELECT `Title`, `Author`
  mysql> SELECT Title, Author
         FROM `books`
         FROM books
         WHERE `Year` > 2001
         WHERE Year > 2001
         AND `Year` < 2006
         AND Year < 2006
         ORDER BY `Author`
         ORDER BY Author ASC
         LIMIT 100;
         LIMIT 100;


Line 32: Line 30:
The INSERT statement is used to insert data into a particular table of a database. The Syntax is as follows :
The INSERT statement is used to insert data into a particular table of a database. The Syntax is as follows :


  mysql> INSERT INTO `table_name`
  mysql> INSERT INTO table_name
         (`field1`, `field2`, etc...)
         (field1, field2, etc...)
         VALUES ('val1', 'val2', etc...);
         VALUES ('val1', 'val2', etc...);


Example :
Example :


  mysql> INSERT INTO `books`
  mysql> INSERT INTO books
         (`ISBN`, `Title`, `Author`, `Year`)
         (ISBN, Title, Author, Year)
         VALUES ('1234567890', 'Programming PHP and MySQL', 'Poisson, R.W', 2006);
         VALUES ('1234567890', 'Programming PHP and MySQL', 'Poisson, R.W', 2006);


Line 46: Line 44:
The DELETE statement is used to remove row(s) from a table. The syntax is as follows :
The DELETE statement is used to remove row(s) from a table. The syntax is as follows :


  mysql> DELETE FROM `table_name`
  mysql> DELETE FROM table_name
         [WHERE condition1, condition2 etc...];
         [WHERE condition1, condition2 etc...];


Example :
Example :


  mysql> DELETE FROM `books`
  mysql> DELETE FROM books
         WHERE `ISBN` = '1234567890';
         WHERE ISBN = '123456789ĞÁ0';


== ALTER ==
== ALTER ==
Line 102: Line 100:
   | REBUILD PARTITION partition_names
   | REBUILD PARTITION partition_names
   | REPAIR PARTITION partition_names
   | REPAIR PARTITION partition_names
   | REMOVE PARTITIONING  
   | REMOVE PARTITIONING


== DROP ==
== DROP ==
Line 108: Line 106:
DROP is used to completely remove a table or database. The syntax is as follows :
DROP is used to completely remove a table or database. The syntax is as follows :


  mysql> DROP `table_name`;
  mysql> DROP table_name;


== TRUNCATE ==
== TRUNCATE ==
Line 114: Line 112:
TRUNCATE is used when you want to remove the data, but not the structure of a table. The syntax is as follows :
TRUNCATE is used when you want to remove the data, but not the structure of a table. The syntax is as follows :


  mysql> TRUNCATE `table_name`;
  mysql> TRUNCATE table_name;
 
[[../Creating a Table/|Previous: Creating a Table]] | [[../|TOC]] | [[../Common Functions and Operators/|Next: Common Functions and Operators]]


[[Category:PHP]]
[[Category:PHP and MySQL Programming|{{SUBPAGENAME}}]]
[[Category:MySQL]]