SQL pdf-3
SQL pdf-3
Contents:
How to open MySQL.
Database commands.
Creating tables in MySQL.
Inserting records in tables.
HOW TO OPEN MYSQL
Start All programs MySQL MySQL Server MySQL command Line Client.
A screen asking password will appear. Give password( if any) and press Enter key.
After pressing Enter Key, screen will appear like this ( image given below).
To Delete a database
Syntax: drop database databasename;
Eg: drop database stkhs;
Creating Table in MySQL
Syntax:
Create table tablename
(
Column_name datatype(size)key/constraint,
Column_name datatype(size)key/constraint,
.
. Note:
); 1. You must use / be inside a
Eg: database before creating a table.
2. Execute the command after
Create table student writing the complete code.
( 3. To execute commands in MySQL
Billno char(10) primary key, semicolon (;) is used.
Name varchar(40) not null,
Class int,
Sec char(3)
);
To view/describe tables
To view list of tables in a database
Syntax:
Show tables;
To view the structure of table:
Syntax:
desc tablename;
or
Describe tablename;
Eg: desc student;
output
Inserting Records in a Table
In tables we can insert records in two ways:
1. Without using column names
2. By using column names
Without using column names ( also known as full insertion):
Syntax:
Insert [into] table_name values(value1,value2,….);
Eg: insert into student values(‘B101’ , ’Ajay’ , 12 , ’D’);
Or
Insert student values(‘B101’ , ’Ajay’ , 12 , ’D’);
Note:
1. Keyword “into” is optional.
2. Values must be in the sequence of columns created in table.
3. No. of values must be equal to the no. of columns present in the table.
4. Other than numeric data type, all values must be enclosed between single
quotes(‘ ‘) or double quotes (“ “).
Inserting Records in a Table
By using column names
Syntax:
Insert into table_name (colname,colname,…) values (value1,value2,….);
Eg:
insert into student (billno, name, class, sec) values(‘B101’ , ’Ajay’ , 12 , ’D’);
Note:
Values must be in the sequence of columns written in query. Although
record will be inserted according to the sequence of columns created
in table.
Eg:
insert into student (name, class, sec, billno) values(’Ajay’ , 12 , ’D’, ‘B101’);
Inserting Partial data in a Table
Syntax:
Insert into table_name (colname,colname,…) values (value1,value2,….);
Eg:
insert into student (billno, name, class) values(‘B101’ , ’Ajay’ , 12);
Note:
1. In the above query value for ‘sec’ column is not given.
2. You can not skip the values of those columns on which primary key,
not null or any check constraint is applied.
Inserting records from existing table
Syntax: (for all rows and columns)
Insert into new_table_name values (select * from existing table_name);
Eg:
insert into student2 select * from student1;
Note:
1. New table must be created before inserting the values.
2. No. of columns must be same in both the tables( for full insertion).
Note:
Column names can be different.