Handy MySql Commands for Starters

No comments
Creating a database:
mysql>CREATE DATABASE phonebook;
create database [databasename];

We created a database and named it as phonebook.
MySql is not case sensitive, so you don’t have to write commands in capital letters.

Make sure you include a semicolon (;) after every command.

mysql>show databases;
This command lists the available databases on the servers.
mysql>use phonebook;
In order to start creating tables we have to select a databases form the above list.
If we haven’t used the show and use commands and instead tried continuing creating tables for our databases MySql would give us an error.
Creating a table inside our database:
mysql>create table friends (id int not null primary key auto_increment, fName varchar(15), lName varchar(15), phone varchar(20));
We created a table with the name “friends”
We created 3 “columns” in our table (id, fname, lname), assigned attributes to these columns (id column is integer, null values are not accepted, its values are auto incrementing and we assigned the primary key to that column), we assigned only one attribute (varchar) for fname and lname columns; varchar is used for text values and by 15 we mean the maximum length of the text would be 15 characters.
Inserting values to the table:
mysql>insert into friends (id, fname, lname, phone) values (null, “Albert”, “Kane”, “123-456789”), (null, “Brian”, “Foster”, “321-98765”);
Querying a Database:
mysql>select * from friends;
Asterix ( * ) means everything
Deleting a Database:
mysql>drop database phonebook;
Deleting a Table:
mysql>drop table friends;


Bonus:
How to view data dictionary

mysql>SELECT table_name, table_type, engine FROM information_schema.tables;

No comments :

Post a Comment