<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://vwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=SQL_Syntax_%28MySQL%29</id>
	<title>SQL Syntax (MySQL) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://vwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=SQL_Syntax_%28MySQL%29"/>
	<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=SQL_Syntax_(MySQL)&amp;action=history"/>
	<updated>2026-05-29T17:07:46Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>http://vwiki.co.uk/index.php?title=SQL_Syntax_(MySQL)&amp;diff=2430&amp;oldid=prev</id>
		<title>Sstrutt: Initial creation - content from MySQL page (now category)</title>
		<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=SQL_Syntax_(MySQL)&amp;diff=2430&amp;oldid=prev"/>
		<updated>2014-03-16T22:07:23Z</updated>

		<summary type="html">&lt;p&gt;Initial creation - content from MySQL page (now category)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== CREATE / ALTER Tables ==&lt;br /&gt;
=== CREATE ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE TABLE hware (hid INT AUTO_INCREMENT, make VARCHAR(64), model VARCHAR(64), cpu_num TINYINT UNSIGNED, mem SMALLINT UNSIGNED, sn VARCHAR(64), PRIMARY KEY (hid));&lt;br /&gt;
CREATE TABLE notes (nid INT UNSIGNED AUTO_INCREMENT, note VARCHAR(256), stamp TIMESTAMP DEFAULT current_timestamp ON UPDATE current_timestamp, PRIMARY KEY (nid));&lt;br /&gt;
CREATE TABLE hware2notes (hid INT, nid INT, PRIMARY KEY (hid, nid));                        # Primary key prevent duplicate rows&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== RENAME ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
RENAME TABLE hware to hardware;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ALTER ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
ALTER TABLE hware ADD UNIQUE KEY hid;                                         # Add additional index/key (using existing column)&lt;br /&gt;
ALTER TABLE hware ADD FULLTEXT name (name, description);                      # Add a FULLTEXT key (used for fuzzy searches)&lt;br /&gt;
ALTER TABLE hware ADD COLUMN cpu_core TINYINT UNSIGNED AFTER cpu_num;         # Add new column&lt;br /&gt;
ALTER TABLE hware MODIFY COLUMN mem INT UNSIGNED;                             # Modify column type&lt;br /&gt;
ALTER TABLE hware CHANGE COLUMN cpu_num cpu_sock TINYINT UNSIGNED;            # Change column name&lt;br /&gt;
ALTER TABLE hware DROP COLUMN cpu_sock;                                       # Drop (delete/remove) column from table&lt;br /&gt;
ALTER TABLE hware DROP KEY hid;                                               # Drop (delete/remove) hid index (not the column)&lt;br /&gt;
ALTER TABLE hware DROP PRIMARY KEY;                                           # Drop primary key from table&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== DELETE ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
DELETE FROM hware;                                         # Delete the contents if the hware table&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Keys / Indexes ===&lt;br /&gt;
There are three different types of indexing available for a table, all of which can be made against one or more columns in your table&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Primary&amp;#039;&amp;#039;&amp;#039; - compulsory, often on an &amp;lt;code&amp;gt;AUTO_INCREMENT&amp;lt;/code&amp;gt; id.  Must be unique for each row.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Unique&amp;#039;&amp;#039;&amp;#039; - optional, useful to enforce uniqueness in a table across columns not included in Primary Key&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;FullText&amp;#039;&amp;#039;&amp;#039; - optional, used to create a full text index, which you can later do fuzzy searches against.&lt;br /&gt;
&lt;br /&gt;
All keys have a name, if you don&amp;#039;t specify one at creation, the name of the first column is used.&lt;br /&gt;
&lt;br /&gt;
== INSERT / UPDATE Rows ==&lt;br /&gt;
=== Basic Examples ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
INSERT INTO hosts (name, ping_ok) VALUES (&amp;#039;ServerA&amp;#039;, 1) ON DUPLICATE KEY UPDATE ping_ok=1;&lt;br /&gt;
INSERT IGNORE INTO hware2note (hid, nid);                 # Ignores errors returned if insert would violate primary/unique key duplication&lt;br /&gt;
UPDATE hosts SET ping_ok=0, reason=&amp;#039;Time Out&amp;#039; WHERE name=&amp;#039;ServerA&amp;#039;; &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== INSERT ... SELECT ===&lt;br /&gt;
Used when you want you want include data from another table in an &amp;lt;code&amp;gt;INSERT&amp;lt;/code&amp;gt; statement (eg you want to reference a unique ID in another table)&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;# Basic insert&lt;br /&gt;
INSERT INTO table (id, make, model) SELECT id, &amp;#039;DELL&amp;#039;, &amp;#039;PE1950&amp;#039; FROM hosts WHERE ip=INET_ATON(&amp;#039;159.104.7.171&amp;#039;);&lt;br /&gt;
# With &amp;#039;ON DUPLICATE&amp;#039;, VALUES allows the result from the SELECT to re-used&lt;br /&gt;
INSERT INTO table (id, make, model) SELECT id, &amp;#039;DELL&amp;#039;, &amp;#039;PE1950&amp;#039; FROM hosts WHERE ip=INET_ATON(&amp;#039;159.104.7.171&amp;#039;) &lt;br /&gt;
    ON DUPLICATE KEY UPDATE id = VALUES(id);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== UPDATE ... SELECT ===&lt;br /&gt;
Here the &amp;lt;code&amp;gt;table&amp;lt;/code&amp;gt; table is being updated from the &amp;lt;code&amp;gt;hware&amp;lt;/code&amp;gt; table. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;UPDATE table SET make=(SELECT make FROM hware WHERE id=5) WHERE tid=2;&amp;lt;/source&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Alternatively, the example below the &amp;lt;code&amp;gt;plat&amp;lt;/code&amp;gt; table is updated from the &amp;lt;code&amp;gt;os&amp;lt;/code&amp;gt; table.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;UPDATE plat,os SET plat.osid=os.osid WHERE os.os_supplied=&amp;#039;Microsoft(R) Windows(R) Server 2003, Standard Edition&amp;#039;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== SELECT Rows ==&lt;br /&gt;
=== AS ===&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;lt;code&amp;gt;AS&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;&amp;#039; renames the column heading for a query result.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt; SELECT INET_NTOA(ip) AS ip, name FROM hosts; &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ORDER BY ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT INET_NTOA(ip) AS ip, name FROM hosts ORDER BY ip;&lt;br /&gt;
SELECT INET_NTOA(ip) AS ip, name FROM hosts ORDER BY ip DESC;            # Reverse sorting order (descending)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NULL&amp;#039;s can be a bit of pain as they tend to end up at the top, to force then to the bottom insert an additional &amp;lt;code&amp;gt;ISNULL(column)&amp;lt;/code&amp;gt; for column your sorting by which has NULL values...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt; SELECT INET_NTOA(ip) AS ip, name FROM hosts ORDER BY ISNULL(ip), ip; &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JOIN ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT make, model FROM hware JOIN hosts ON table.id=hosts.id WHERE hosts.ip=INET_ATON(&amp;#039;10.10.255.253&amp;#039;);                 # Join with ON&lt;br /&gt;
SELECT make, model FROM hware JOIN hosts USING (id) WHERE hosts.ip=INET_ATON(&amp;#039;10.10.255.253&amp;#039;);                           # Join with USING&lt;br /&gt;
SELECT make, model, info FROM hware JOIN hosts USING (id) JOIN notes ON (id) WHERE hosts.ip=INET_ATON(&amp;#039;10.10.255.253&amp;#039;);  # Multiple join&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Different types of join will yield differing results, depending how different rows match up.  It can be a bit flummoxing to start with, but its actually fairly simple once you&amp;#039;ve got the basic idea straight in your head.  The LEFT and RIGHT joins allow you control whether or not you want to see NULL&amp;#039;s appearing in your results where rows don&amp;#039;t always have counterparts in two tables you are JOIN&amp;#039;ing together.&lt;br /&gt;
&lt;br /&gt;
See this site for a very clear and concise walk-through - http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html&lt;br /&gt;
&lt;br /&gt;
=== COUNT ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT COUNT(*) FROM hware WHERE make=&amp;#039;IBM&amp;#039;;                                                  # Counts number of rows in selection&lt;br /&gt;
SELECT make, COUNT(*) AS count FROM hware GROUP BY make;                                      # Counts number of occurrences of &amp;#039;make&amp;#039;&lt;br /&gt;
SELECT COUNT(*) AS total, SUM(IF(model=&amp;#039;x336&amp;#039;,1,0)) AS x336 FROM hware WHERE make=&amp;#039;IBM&amp;#039;;      # Additional counts number of rows where model=&amp;#039;x336&amp;#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== DISTINCT ===&lt;br /&gt;
Only selects completely distinct rows (ie on all columns), not to be confused with &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt; (see below).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT DISTINCT * FROM hware JOIN notes ON (id);  &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== GROUP BY ===&lt;br /&gt;
Select rows, grouped by a particular column (so effectively only shows one row for duplicates on that column)&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM hware JOIN notes ON (id) GROUP BY model;  &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Events ==&lt;br /&gt;
Events are scheduled occurrences, either where you&amp;#039;re running a simple command, or a stored procedure.&lt;br /&gt;
&lt;br /&gt;
If you need to create a stored procedure, use the MySQL Workbench software (http://dev.mysql.com/downloads/workbench/), its a lot easier than creating via command line.&lt;br /&gt;
&lt;br /&gt;
To create an event...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;mysql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE EVENT do_update_cluster_stats ON SCHEDULE EVERY 1 DAY STARTS TIMESTAMP(CURRENT_DATE, &amp;#039;23:30:00&amp;#039;) DO CALL update_cluster_stats();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may need to enable the event scheduler&lt;br /&gt;
* &amp;lt;code&amp;gt; SET GLOBAL event_scheduler = 1; &amp;lt;/code&amp;gt;&lt;br /&gt;
* The event_scheduler should now be visible in the process list (&amp;lt;code&amp;gt;show processlist&amp;lt;/code&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
[[Category:MySQL]]&lt;/div&gt;</summary>
		<author><name>Sstrutt</name></author>
	</entry>
</feed>