Saturday, 2 August 2014

Installing MariaDB

You can know what is MariaDB from my previous blog

Now let’s see the installation of MariaDB
Step 1
First make sure that required packages are installed and  add apt-get key for Mariadb repository using following command
$ sudo apt-get install software-properties-common
$ sudo apt-key adv --recv-keys --keyserver hkp:// 0xcbcb082a1bb943db

Add apt-get repository as per your Ubuntu version
For Ubuntu 13.10
$ sudo add-apt-repository 'deb saucy main'
For Ubuntu 13.04
$ sudo add-apt-repository 'deb raring main'
For Ubuntu 12.10
$ sudo add-apt-repository 'deb quantal main'
For Ubuntu 12.04 LTS
$ sudo add-apt-repository 'deb precise main'

Step 2
Install MariaDB using the following commands
$ sudo apt-get update
$ sudo apt-get install mariadb-server

Provide the root account password as given below

Step 3

Login to MariaDB using the following command after installation
mysql -u root -p

Creating a database in MariaDB
Entering the account administrator password set up during installation you will be given a MariaDB prompt.

We will create a database to learn on called students using the following command
We will switch to the new database
USE  students;
Now the database is created and we can create a table.
CREATE TABLE details(student_id int(5) NOT NULL AUTO_INCREMENT,
                        name varchar(20) DEFAULT NULL,
                        age int(3) DEFAULT NULL,
                        marks int(5) DEFAULT NULL,
                        PRIMARY KEY(student_i)d
To accomplish what we have done, use the following command,
show columns in details;

Each column in table creation command is separated by comma and follows this fashion,
Column_Name Data_Type[(size_of_data)] [NULL or NOT NULL] [DEFAULT default_value]

The values of each column definition are,
* Column Name: Describes the attribute being assigned.
* Data Type: Specifies the type of data in the column.
* Null: Defines whether null is a valid value for that field. Can be "null" or "not null".
* Default Value: Sets the initial value of all newly created records that do not specify a value.
* auto_increment: MySQL will handle the sequential numbering internally of any column marked with this   option, in order to provide a unique value for each record.

Ultimately before closing the table definition we need to use primary key by typing PRIMARY KEY(column name).It guarantees that , this column will serve as a unique field.

Inserting data into MariaDB table
INSERT INTO details(name,age,marks) values ("anu",15,450);

INSERT INTO details(name,age,marks) VALUES("Bob",15,400);

We need not add values in student_id.It is automatically incremented.All other values are given in quotes.

Deleting a table
To delete a table, type the following command
DROP TABLE table_name;
Once the table is deleted, the data inside it cannot be recovered.

Now we can view the current table using “show tables” command. This command gives all the tables inside the database.
SHOW tables;
Then, after deleting the table.
DROP TABLE details;
Query OK, 0 rows affected (0.02 sec)

SHOW tables;

No comments:

Post a Comment