<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.splurt.space/mediawiki/index.php?action=history&amp;feed=atom&amp;title=MySQL</id>
	<title>MySQL - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.splurt.space/mediawiki/index.php?action=history&amp;feed=atom&amp;title=MySQL"/>
	<link rel="alternate" type="text/html" href="https://wiki.splurt.space/mediawiki/index.php?title=MySQL&amp;action=history"/>
	<updated>2026-05-15T15:33:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>https://wiki.splurt.space/mediawiki/index.php?title=MySQL&amp;diff=1627&amp;oldid=prev</id>
		<title>imported&gt;SpaceManiac: Remove &quot;Game Resources&quot; category</title>
		<link rel="alternate" type="text/html" href="https://wiki.splurt.space/mediawiki/index.php?title=MySQL&amp;diff=1627&amp;oldid=prev"/>
		<updated>2020-12-04T03:46:38Z</updated>

		<summary type="html">&lt;p&gt;Remove &amp;quot;Game Resources&amp;quot; category&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;MySQL is a language you use with databases. Queries are however not interpreted &amp;#039;one line at a time&amp;#039;, instead they are best described as a single instruction, with lots of arguments, some of which can be conditions.&lt;br /&gt;
&lt;br /&gt;
== Software ==&lt;br /&gt;
&lt;br /&gt;
#Xampp ([http://www.apachefriends.org/en/xampp.html link]) (See our [[Downloading the source code#Setting up the database|Setting up the database]] guide)&lt;br /&gt;
#MySQL Workbench ([http://www.mysql.com/products/workbench/ link])&lt;br /&gt;
&lt;br /&gt;
Set up MySQL Workbench:&lt;br /&gt;
&lt;br /&gt;
*Once you have the database set up, open MySQL Workbench&lt;br /&gt;
*Select &amp;#039;New Connection&amp;#039;&lt;br /&gt;
:*Connection name: My Database (or whatever you want)&lt;br /&gt;
:*Hostname: 127.0.0.1&lt;br /&gt;
:*Port: 3306&lt;br /&gt;
:*Username: root&lt;br /&gt;
*Ok&lt;br /&gt;
*Doubleclick the new connection in the list&lt;br /&gt;
&lt;br /&gt;
== Types of queries ==&lt;br /&gt;
&lt;br /&gt;
Their names describe what you want to do.&lt;br /&gt;
&lt;br /&gt;
SELECT - Selects data from the database&amp;lt;br&amp;gt;&lt;br /&gt;
INSERT - Inserts data into a table&amp;lt;br&amp;gt;&lt;br /&gt;
UPDATE - Updates existing data in a table&amp;lt;br&amp;gt;&lt;br /&gt;
DELETE - Deletes existing data from a table&lt;br /&gt;
&lt;br /&gt;
There are more, but this guide doesn&amp;#039;t deal with them:&amp;lt;br&amp;gt;&lt;br /&gt;
CREATE - Creates databases, tables, views, procedures, triggers, etc. Whatever you define &amp;lt;br&amp;gt;&lt;br /&gt;
ALTER - Changes the properties of something you created (for example, which columns a table has)&amp;lt;br&amp;gt;&lt;br /&gt;
DROP - Deletes something you created (DELETE only deletes the data, DROP deletes the table itself.)&lt;br /&gt;
&lt;br /&gt;
== Table Naming Convention ==&lt;br /&gt;
&lt;br /&gt;
Tables are usually named in lower case, they use the underscore as a word separator and the first word is usually the name of the project the table is a part of. Most tables in the tgs database are called ss13_X. The last common thing is to always use singular. So &amp;#039;user_reports&amp;#039; is not a good name, &amp;#039;user_report&amp;#039; is. These naming conventions are there to help you down the road, so you will not be in doubt whether a table is called &amp;#039;ss13_players&amp;#039; or &amp;#039;ss13_player&amp;#039;, etc. Examples: ss13_player, ss13_connection_log, ss13_poll_question, etc.&lt;br /&gt;
&lt;br /&gt;
== SELECT ==&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s select something. This assumes you have the tgs database set up and that you played a few rounds with it enabled.&lt;br /&gt;
&lt;br /&gt;
=== Everything ===&lt;br /&gt;
&lt;br /&gt;
Execute the following statement:&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&lt;br /&gt;
&lt;br /&gt;
You now have a list of players who played on your server so far. I can&amp;#039;t provide you with a screenshot for privacy reasons, but you can see what you get in your output. The * in the statement defines that you want to select all columns.&lt;br /&gt;
&lt;br /&gt;
=== Filter columns ===&lt;br /&gt;
&lt;br /&gt;
The problem is that when you are selecting something from the database in programs, you&amp;#039;ll want as little data transfer as possible. So selecting all columns is rarely needed. Let&amp;#039;s suppose we want a list of players and what their last admin rank was:&lt;br /&gt;
&lt;br /&gt;
SELECT ckey, lastadminrank&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&lt;br /&gt;
&lt;br /&gt;
Voila. Once you execute this, you will get a table, which has a list of all the players who played on your server. The returned table will contain two columns: Their ckey and the admin rank they held when they last connected.&lt;br /&gt;
&lt;br /&gt;
=== Filter rows ===&lt;br /&gt;
&lt;br /&gt;
Well, now that we managed to isolate a few columns, let&amp;#039;s see about filtering through rows. We&amp;#039;ll return to selecting all columns tho. Conditions for which rows you want returned and which you don&amp;#039;t are defined in the WHERE portion of the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
==== Strings ====&lt;br /&gt;
&lt;br /&gt;
Execute this:&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE lastadminrank = &amp;quot;Player&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This will return a list of people, who have their &amp;#039;lastadminrank&amp;#039; column set to &amp;quot;Player&amp;quot;. &amp;#039;&amp;#039;&amp;#039;Note the single =&amp;#039;&amp;#039;&amp;#039; So no admins will be in the returned list. So how do we get admins? Simple.&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE lastadminrank != &amp;quot;Player&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you know != means &amp;quot;not equal&amp;quot;. It is however case sensitive. It also doesn&amp;#039;t allow you to do any clever filtering. Let&amp;#039;s look at LIKE:&lt;br /&gt;
&lt;br /&gt;
==== LIKE ====&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE ckey LIKE &amp;quot;%abc%&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This will return a list of players, whose ckey contains the letters &amp;#039;abc&amp;#039;. The % signs before and after abc mean that anything can be located before or after abc. LIKE also ignores case, so all of the following will work: &amp;quot;&amp;#039;&amp;#039;&amp;#039;abc&amp;#039;&amp;#039;&amp;#039;n&amp;quot;, &amp;quot;aocwegijaw&amp;#039;&amp;#039;&amp;#039;abc&amp;#039;&amp;#039;&amp;#039;maobr&amp;quot;, &amp;quot;&amp;#039;&amp;#039;&amp;#039;abc&amp;#039;&amp;#039;&amp;#039;&amp;quot;, &amp;quot;&amp;#039;&amp;#039;&amp;#039;ABC&amp;#039;&amp;#039;&amp;#039;&amp;quot;, &amp;quot;&amp;#039;&amp;#039;&amp;#039;AbC&amp;#039;&amp;#039;&amp;#039;&amp;quot;, &amp;quot;E&amp;#039;&amp;#039;&amp;#039;Abc&amp;#039;&amp;#039;&amp;#039;&amp;quot;, &amp;quot;aWAEGaewgaWEG&amp;#039;&amp;#039;&amp;#039;aBc&amp;#039;&amp;#039;&amp;#039;aegawe&amp;quot;. Your example might require you to replace the string abc with a different character combination, depending on which ckeys you have logged.&lt;br /&gt;
&lt;br /&gt;
==== Numbers ====&lt;br /&gt;
&lt;br /&gt;
Numbers use what you&amp;#039;d expect. &amp;gt;, &amp;gt;=, &amp;lt;=, &amp;lt;, =, !=&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE id &amp;lt; 50&lt;br /&gt;
&lt;br /&gt;
will return all lines where the id is lower than 50.&lt;br /&gt;
&lt;br /&gt;
==== Date and time ====&lt;br /&gt;
&lt;br /&gt;
The ss13_player table contains two fields which are of type &amp;#039;datetime&amp;#039;: firstseen and lastseen.&lt;br /&gt;
&lt;br /&gt;
You can compare them directly with &amp;gt;, &amp;lt;, =, !=. &amp;#039;&amp;#039;&amp;#039;Note the single =&amp;#039;&amp;#039;&amp;#039;. For example: If you want to only select people who have only logged in once, and then never again:&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE firstseen = lastseen&lt;br /&gt;
&lt;br /&gt;
===== DATE() =====&lt;br /&gt;
&lt;br /&gt;
You can however also select people who were first seen on a particular date. You&amp;#039;ll need to use a function for that tho:&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE DATE(firstseen) = &amp;quot;2013-04-19&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This will return a list of players who were first seen on 19 April 2013. If you want to get people who joined after April 17 2013, execute the following: (NOTE, This will only show people who joined on 18 April or later! Use &amp;gt;= if you want to include 17 April.)&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE DATE(firstseen) &amp;gt; &amp;quot;2013-04-17&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===== DATEDIFF(), NOW() =====&lt;br /&gt;
&lt;br /&gt;
Okay, so now you know how to select people who joined after a constant date. But what about if you want to only select people who joined in the last 7 days? The date keeps changing all the time, the date in the select statement, however, remains the same. The fix is pretty easy:&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE DATEDIFF(Now(),firstseen) &amp;lt; 7&lt;br /&gt;
&lt;br /&gt;
This will select everyone who joined 7 or fewer days ago.&lt;br /&gt;
&lt;br /&gt;
=== Combining column and row filtering ===&lt;br /&gt;
&lt;br /&gt;
Easy:&lt;br /&gt;
&lt;br /&gt;
SELECT ckey, firstseen&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE DATEDIFF(Now(),firstseen) &amp;lt; 7&lt;br /&gt;
&lt;br /&gt;
Does the same as the statement a few lines above, but also filters columns. Note that you don&amp;#039;t have to select all the columns you use in your where section. For example, even if you are not returning &amp;#039;firstseen&amp;#039;, but are using it in the WHERE section, this will still work:&lt;br /&gt;
&lt;br /&gt;
SELECT ckey&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE DATEDIFF(Now(),firstseen) &amp;lt; 7&lt;br /&gt;
&lt;br /&gt;
=== Multiple conditions, AND and OR ===&lt;br /&gt;
&lt;br /&gt;
So from here on, when we talk about conditions, we mean stuff in the WHERE section. You learned pretty much everything about filtering by columns already, so we&amp;#039;ll concentrate on how to get the data you want. Let&amp;#039;s suppose you want to select people who joined between 2 weeks ago and a week ago. So people who were already here 7 days ago, but were not yet here 14 days ago. Their DATEDIFF(Now(),firstseen) has to be higher than 7, but lower than 14. Well, the section title gave it away:&lt;br /&gt;
&lt;br /&gt;
SELECT *&amp;lt;br&amp;gt;&lt;br /&gt;
FROM ss13_player&amp;lt;br&amp;gt;&lt;br /&gt;
WHERE DATEDIFF(Now(),firstseen) &amp;gt;= 7&lt;br /&gt;
:AND DATEDIFF(Now(),firstseen) &amp;lt; 14&lt;br /&gt;
&lt;br /&gt;
You can use AND, OR, NOT and XOR. &amp;amp;&amp;amp;, || and ! also work, but it is more common to use the words, so I encourage you to do that. Brackets ( and ) also work.&lt;br /&gt;
&lt;br /&gt;
=== Joining tables ===&lt;br /&gt;
&lt;br /&gt;
This is where the magic of MySQL happens... But I&amp;#039;m still writing it.&lt;br /&gt;
&lt;br /&gt;
== INSERT ==&lt;br /&gt;
&lt;br /&gt;
INSERT is used to insert a new entry into a table.&lt;br /&gt;
&lt;br /&gt;
If you have MySQL workbench opened, click &amp;quot;create a new SQL tab for executing queries&amp;quot; in the upper left, then right click the table you wish to insert into and select &amp;quot;Send to sql editor &amp;gt; insert statement&amp;quot;. Edit the grayed out section with actual data.&lt;br /&gt;
&lt;br /&gt;
* For columns marked &amp;#039;id&amp;#039;, use null. They have auto increment enabled, so they will just add the next value.&lt;br /&gt;
* For numbers, just write the number&lt;br /&gt;
* For strings, use single or double quotes to define them. Multiline strings work.&lt;br /&gt;
* If you wish to add the current date, use Now()&lt;br /&gt;
* If you wish to do a date based on Now(), do NOW() + INTERVAL 1 DAY (You can use SECOND, MINUTE, HOUR, DAY, WEEK,... Use google.)&lt;br /&gt;
* If you wish to add a date, use the format (with quotes) &amp;quot;2013-04-19 12:05:27&amp;quot;&lt;br /&gt;
* You can also add null for columns that don&amp;#039;t need a value, for example: ss13_ban inserts null into the &amp;#039;unbanned&amp;#039;, which is set to 1 when the person gets unbanned.&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s an example of a working insert statement:&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `ss13_player`&lt;br /&gt;
(`id`,`ckey`,`firstseen`,`lastseen`,`ip`,`computerid`,`lastadminrank`)&lt;br /&gt;
VALUES&lt;br /&gt;
(null, &amp;quot;myusername&amp;quot;, Now() - INTERVAL 7 DAY, Now(), &amp;quot;123.123.123.123&amp;quot;, &amp;quot;12345&amp;quot;, &amp;quot;GameAdmin&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
=== In MySQL Workbench ===&lt;br /&gt;
&lt;br /&gt;
In the menu on the left, expand the database (probably called &amp;#039;feedback&amp;#039;), right click the table you wish to delete from, select &amp;#039;edit table data&amp;#039;, at the end of the table, there is a row of &amp;#039;null&amp;#039; values, click on it, it&amp;#039;ll change the entry you clicked on to an input field. Fill out the rest of the line, and repeat until you entered all the lines you want. Then hit &amp;#039;Apply&amp;#039; in the bottom right and execute. Look at the output you get when you hit &amp;#039;Apply&amp;#039; if you want more examples for the INSERT statement.&lt;br /&gt;
&lt;br /&gt;
== UPDATE ==&lt;br /&gt;
&lt;br /&gt;
Guide not made yet. What I want to point out is that UPDATE is very dangerous. If you do UPDATE ss13_player SET ckey = &amp;quot;someone&amp;quot;, it will set the value &amp;quot;someone&amp;quot; to the ckey column for EVERY SINGLE ROW.&lt;br /&gt;
&lt;br /&gt;
Here is an example of how to do this right, but you REALLY need to be careful with the UPDATE statement:&lt;br /&gt;
&lt;br /&gt;
UPDATE ss13_player SET ckey = &amp;quot;someone&amp;quot; WHERE id = 6&lt;br /&gt;
&lt;br /&gt;
=== In MySQL Workbench ===&lt;br /&gt;
&lt;br /&gt;
In the menu on the left, expand the database (probably called &amp;#039;feedback&amp;#039;), right click the table you wish to delete from, select &amp;#039;edit table data&amp;#039;, doubleclick any field, edit it, select something else (so the &amp;#039;Apply&amp;#039; button in the bottom right is enabled), hit the &amp;#039;Apply&amp;#039; button and execute it. If you wish to see more examples of how UPDATE works, you can see the statements it provides you with.&lt;br /&gt;
&lt;br /&gt;
== DELETE ==&lt;br /&gt;
&lt;br /&gt;
Don&amp;#039;t even use. DELETE FROM ss13_player will delete the entire player log (forever). &lt;br /&gt;
&lt;br /&gt;
Here is an example of how to use it if you ever need to:&lt;br /&gt;
&lt;br /&gt;
DELETE FROM ss13_player WHERE id = 6&lt;br /&gt;
&lt;br /&gt;
The way you should handle this when making new databases is to include a &amp;#039;deleted&amp;#039; column in a table. For example:&lt;br /&gt;
&lt;br /&gt;
ss13_player: ckey, lastadminrank, firstseen, lastseen, deleted&lt;br /&gt;
&lt;br /&gt;
Then, instead of using DELETE FROM ss13_player WHERE id = 6, you&amp;#039;d delete it by doing UPDATE ss13_player SET deleted = 1 WHERE id = 6.&amp;lt;br&amp;gt;&lt;br /&gt;
When selecting data from it, instead of doing SELECT * FROM ss13_player, you&amp;#039;d do SELECT * FROM ss13_player WHERE ISNULL(deleted)&amp;lt;br&amp;gt;&lt;br /&gt;
So you&amp;#039;d only &amp;#039;effectively&amp;#039; be deleting them, they would however still remain in the database. This allows you to fix potential issues, allowing you to restore entries by editing the &amp;#039;deleted&amp;#039; column. If you&amp;#039;d have used the DELETE statement, the data would be pretty much lost, if you hadn&amp;#039;t previously backed it up.&lt;br /&gt;
&lt;br /&gt;
=== In MySQL Workbench ===&lt;br /&gt;
&lt;br /&gt;
In the menu on the left, expand the database (probably called &amp;#039;feedback&amp;#039;), right click the table you wish to delete from, select &amp;#039;edit table data&amp;#039;, select any number of entries you wish to delete (don&amp;#039;t have to select the entire row, just have something in the row selected), right click the selected area and select &amp;#039;Delete Row(s)&amp;#039;, select something else (so the &amp;#039;Apply&amp;#039; button in the bottom right is enabled), hit the &amp;#039;Apply&amp;#039; button and execute it. If you wish to see more examples of how DELETE works, you can see the statements it provides you with.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Contribution guides}}&lt;/div&gt;</summary>
		<author><name>imported&gt;SpaceManiac</name></author>
	</entry>
</feed>