This is a tutorial for creating and controlling databases with a MYSQL server.
You'll need the mysql server.
$ sudo apt-get install mysql-server
You'll be requested to choose a root password, be sure you remember it. Now, we'll login into mysqlserver, in a console:
$ mysql -uroot -p
Now you have a database management system running in your computer. From now on, you can do several thing in it:
> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
Note: the commands must end with a semicolon
> CREATE DATABASE [database_name];
> USE [database_name];
> GRANT ALL PRIVILEGES ON 'database' TO 'user'@'localhost' WITH GRANT OPTION ;
> CREATE TABLE [table_name] > ( column_name_1 type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] [COMMENT 'string'] [reference_definition] , > column_name_2 type [NOT NULL | NULL] ... , ... > column_name_n type [NOT NULL | NULL] ... ) ;
Notes:
> DESCRIBE [table_name] ;
You'll get a result similar to this:3)
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
[column_1] | [type] | [NO - YES ] | [PRI - MUL] | [value] | [comment] [reference] |
. . .
[column_n] | [type] | [NO - YES ] | [PRI - MUL] | [value] | [comment] [reference] |
* For entering a new record in a table, you can use the command INSERT, in this way:
> INSERT INTO [table_name] ( [column1] , [column2] , ... , [column_n] ) VALUES ( [value_]1] , [value_2] , .... , [value_n]);
> SELECT * FROM [table_name] ;
Note: instead of “ * ” you can specify the columns you want separeted by commas.
> SELECT * FROM [table_name] ORDER BY [column_name] [ASC | DESC] LIMIT [value];
> UPDATE TABLE [table_name] SET [column_name] = [value] WHERE . . . ;
> DROP DATABASE [database_name] ;
> DROP TABLE [table_name] ;
>TRUNCATE TABLE [table_name] ;
$ mysqldump -u[user] -p [database_name] > [file_name].sql
> SOURCE [file_path]/[file_name].sql
You can see the full syntax in: http://dev.mysql.com/doc/refman/5.7/en/index.html
In this part we'll learn how to manage the database using the python language we already know. For this, we'll need a MYSQL connector to python, named python-mysqldb:
sudo apt-get install python-mysqldb
Now we're going to start using python language. We can do this in a python enviroment like ipython or by editing a .py text file.
import MySQLdb
connection_name = MySQLdb.connect(host='localhost', user='username', passwd='password', db='database_name')
cursor = connection_name.cursor()
Let's say we have a table called 'Cars' with columns named 'model' and 'price'. We can obtain the price from a certain model by doing this:
cursor.execute("SELECT price FROM Table WHERE model = 1970;") return cursor.fetchone()
The fetchone() method fetches the first entry of the operation result. If we wanted to fetch all the results, we should use the fetchall() method.
We can define a python method for doing this operation in a shorter way:
def obtain_price(car_model): cursor.execute("SELECT price FROM Cars WHERE model = %s" % car_model) return cursor.fetchone()
The %s indicates that the data-type of the argument, and % car_model indicates which method argument it will use.
result = cursor.execute("INSERT INTO 'Cars' (model, price, brand) VALUES (1970 , 13000, 'Toyota';") connection_name.commit() return result
It's important to note that if the data is string-type, it must be between ' '. It's necessary to use the commit() method in order to make the changes permanent, we will always call it if we are inserting or updating data. The return command will indicate wether the table was modified or not.
def insert_car(car_model,car_price,car_brand): result = cursor.execute("INSERT INTO 'Cars' (model, price, brand) VALUES (%s , %s, '%s');" % (car_model,car_price,car_brand)) connection_name.commit() return result
The arguments in the last parenthesis correspond to the ones after the VALUES statement. Note also that only the brand column has ' ' associated, as price and model are integers.
result = cursor.execute("UPDATE 'Cars' SET price = 15000 WHERE brand = 'Toyota' AND model = 1970;") connection_name.commit() return result
Method version:
def update_car(car_price,car_brand,car_model): result = cursor.execute("UPDATE 'Cars' SET price = %s WHERE brand = '%s' AND model = %s;" % (car_price,car_brand,car_model)) connection_name.commit() return result
The SET statement is used to indicate which columns we want to modify.