Class 12 | MySQL

Accessing Database in MYSQL:

In mysql before starting with sql commands, you need to open a database for use. For this after logging into MYSQL, you need to use a command at mysql prompt :

mysql> Use <database name>;

For example if we want to work on our sample database namely test, so we shall write the command
mysql> USE test;
Database changed
mysql>

Creating tables in MYSQL:

Command:Create table

Helps us to create a table, while creating a table we have to specify name of the table, names of columns their data types and sizes.

Syntax:
mysql> Create table tablename
             (colname1 datatype(size),
             colname2 datatype(size),
             colname3 datatype(size));

Example:1
create a table named student with attributes roll, name and age.
Sol:
mysql> create table student(roll int,name char(20),age int);

Example:2
Create a table named employee with attributes as empno,name,sal.
Sol:
mysql> create table employee(empno int,name char(20),sal decimal);

Example:3
Create a table named bank with attributes as accno,name,bal.
Sol:
mysql> create table student(accno int,name char(20),bal decimal);

To display the structure of the table

Command: Desc / describe
Helps us to display the structure of an existing table.

syntax:
desc/describe tablename;

Example:
desc student;
desc employee;
desc bank;