09.09.2013, 04:10
(
Last edited by Eyce; 11/09/2013 at 09:12 AM.
)
Creating a MySQL database tables using Text Editors
IntroductionThis simple tutorial is for servers that use MySQL database for saving data such as player stats, vehicles, and the like. These kinds of databases would more often than not require a lot of fields that could take up much of your time if you do it manually on your host or localhost itself like filling out the fields, scrolling towards the right, you name it. In this tutorial, you'll learn the basics on setting up your database using any text editor that you have.
What is a database?
As described on Wikipedia, a database is an organized collection of data - the simplest way that it could be explained if you have no idea on what it is. MySQL is used for but not limited to retrieving and storing data.
Let's get started
Before anything else, you just need a few things:
* Any text-editor like notepad (I prefer using Notepad++)
* Your localhost or host with a ready-made database (phpMyAdmin)
Things you need to know about creating a MySQL database that will be used in this tutorial:
If you would like to know more about data types and a whole of information about MySQL, especially about storage requirements simply visit this link. Now for the structure of the database, I'll be using player accounts as an example.DATA TYPES
TABLES This is where columns or fields are stored, think of it as a shelf for your books. FIELDS OR COLUMNS The holders of your data (e.g. "username" or "password") TYPES The type of data stored into the fields such as integers, strings, float values. LENGTH OR VALUES This determines the limit of the data you store. DEFAULT Basically, the default value for each field. If used, it would automatically set the value when inserting data if not specified. NULL OR NOT NULL Indicates whether a data value exists or not. AUTO_INCREMENT Automatically increases the value of data stored in a field. Commonly used for IDs that are set as primary index.
I won't even try to explain much about data types but just to as far as I know and have used them. Just to give you an idea on what type should be used for a specific value.
- NUMERIC - used for numeric values such as integers
Code:TINYINT, INT, BIGINT, DECIMAL, FLOAT, DOUBLE, and more...- STRING - obviously for string and text values (e.g. player name)
Code:CHAR, VARCHAR, TINYTEXT, TEXT, LONGTEXT, and more...- DATE and TIME - used for date and time values (haven't tried using)
Code:DATE, DATETIME, TIMESTAMP, TIME, YEAR
Step 1 : The text-editor
Open your notepad or any text-editor.
Step 2 : Starting off
Start of with this code:
PHP Code:
CREATE TABLE IF NOT EXISTS `accounts` (
PHP Code:
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;
Step 3 : Adding fields or columns
Now we can start putting in the different fields for the table, just like so:
PHP Code:
CREATE TABLE IF NOT EXISTS `accounts` (
// FIELDNAME DATATYPE(LENGTH/VALUE) NULL DEFAULT OR AUTO_INCREMENT
`ID` int(11) NOT NULL AUTO_INCREMENT,
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;
Then you can add a line for the player's name, password, level, skin, and all that.
PHP Code:
`playername` varchar(25) NOT NULL,
`password` varchar(150) NOT NULL,
`playerlevel` int(10) NOT NULL DEFAULT '0',
`playerskin` int(5) NOT NULL DEFAULT '271',
Step 4 : Setting the primary key
Setting the primary key, this indicates the field that would automatically increase (which is ID) as defined. After the last field that of your table, add this line in:
PHP Code:
PRIMARY KEY (`id`)
When you're done adding all the fields you need, you would have something like this:
PHP Code:
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`playername` varchar(25) NOT NULL,
`password` varchar(150) NOT NULL,
`playerlevel` int(10) NOT NULL DEFAULT '0',
`playerskin` int(5) NOT NULL DEFAULT '271',
`money` int(11) NOT NULL DEFAULT '500',
`bankaccount` int(11) NOT NULL DEFAULT '100',
`gender` varchar(20) NOT NULL,
`age` int(5) NOT NULL DEFAULT '18',
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;
Step 6 : Uploading and importing
If you don't have a database yet, then follow this basic tutorial for creating one: Basic MySQL Tutorial
Uploading your table/s by simply using the code or text (running query on the database):
- Copy your text or code for the table to be created.
- Select your database where you want the table to be in.
- Click on SQL at the upper menu and a text-box would appear.
- Paste it and should look like the image below.
- Click on GO to run the query.
- Click on IMPORT at the upper menu.
- Choose your .SQL file from your computer.
- Make sure that the FORMAT is set to "SQL" before you
- Click on GO
Notes
- This is the first tutorial that I made so any comments or suggestions would be appreciated for the benefit of people reading and also for the tutorial itself.
- I'm not an EXPERT when it comes to MySQL, just experienced enough to suffice my needs for using it.
- I hope this tutorial helps and good luck!