0% found this document useful (0 votes)
93 views

It Lab Mysql

The document provides information on SQL commands and data types. It discusses the different data definition language (DDL), data manipulation language (DML), and data query language (DQL) commands in SQL. It provides examples of creating tables with columns of various data types, modifying table structures by adding, dropping and modifying columns, inserting data into tables, and querying table data by selecting columns and filtering rows.

Uploaded by

excitekarthik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

It Lab Mysql

The document provides information on SQL commands and data types. It discusses the different data definition language (DDL), data manipulation language (DML), and data query language (DQL) commands in SQL. It provides examples of creating tables with columns of various data types, modifying table structures by adding, dropping and modifying columns, inserting data into tables, and querying table data by selecting columns and filtering rows.

Uploaded by

excitekarthik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

cd c:\xampp\mysql\bin

mysql.exe -u root --password

http://www.w3schools.in/mysql/

SQL is structure query language.SQL contains different data types those


are
1. char(size)
2. varchar(size)
3. varchar2(size)
4. date
5. number(p,s) //** P-PRECISION S-SCALE **//
6. number(size)
7. raw(size)
8. raw/long raw(size)
Different types of commands in SQL:

A).DDL commands: - To create a database objects


B).DML commands: - To manipulate data of a database objects
C).DQL command: - To retrieve the data from a database.
D).DCL/DTL commands: - To control the data of a database…
DDL commands:

1. The Create Table Command: - it defines each column of the table


uniquely. Each column has minimum of three attributes, a name , data
type and size.

Syntax:

Create table <table name> (<col1> <datatype>(<size>),<col2>


<datatype><size>));

Ex:
create table emp(empno number(4) primary key, ename char(10));

2. Modifying the structure of tables.


a)add new columns

Syntax:

Alter table <tablename> add(<new col><datatype(size),<new


col>datatype(size));

Ex:
alter table emp add(sal number(7,2));
3. Dropping a column from a table.

Syntax:
Alter table <tablename> drop column <col>;

Ex:
alter table emp drop column sal;

4. Modifying existing columns.

Syntax:
Alter table <tablename> modify(<col><newdatatype>(<newsize>));

Ex:
alter table emp modify(ename varchar2(15));
5. Renaming the tables

Syntax:

Rename <oldtable> to <new table>;

Ex:
rename emp to emp1;

6. truncating the tables.

Syntax:

Truncate table <tablename>;

Ex:

trunc table emp1;

7. Destroying tables.

Syntax:

Drop table <tablename>;

Ex:

drop table emp;

MODIFYING EXISTING COLUMN

SQL> ALTER TABLE Emp252


MODIFY (phno varchar(20));
Table altered.

DROPING A COLUMN

SQL> ALTER TABLE Emp252


DROP COLUMN phno;

Table altered.

DML commands:

8. Inserting Data into Tables: - once a table is created the most


natural thing to do is load this table with data to be manipulated later.

Syntax 1:

insert into <tablename> (<col1>,<col2>…..<col n>) values(<val 1>,


<val 2>…….<val n>);

Syntax 2:

insert into <tablename> values(&<col1>,&<col2>……,&<col n>);

Syntax 3:

insert into <tablename> values(<val 1>,<val 2>…….,<val n>);

Ex 1:

Insert into skc (sname,rollno,class,dob,fee_paid)


values(‘sri’,’104B’,’cse’,’27-feb-05’,10000.00);

Ex 2:

insert into skc values(&sname,&roll no,&class);


enter sname:’sri’
enter roll no:’104B’
enter class:’cse’
1 row created.

Ex 3:

insert into skc values(‘sri’,’104B’,cse’,’27-feb-05’,10000.00);


9. Delete operations.
a) remove all rows
Syntax:
delete from <tablename>;

b) removal of a specified row/s

Syntax:

delete from <tablename> where <condition>;

10. Updating the contents of a table.

a) updating all rows

Syntax:
Update <tablename> set <col>=<exp>,<col>=<exp>;

b) updating seleted records.

Syntax:
Update <tablename> set <col>=<exp>,<col>=<exp>
where <condition>;

RENAMING THE TABLE:

SQL> RENAME Emp252


TO Emp1252;

Table renamed.

DESCRIBE A STUDENT TABLE

SQL> desc Dept252;


Name Null? Type
----------------------------------------- -------- ----------------------------
DNAME VARCHAR2(10)
DNO CHAR(5)
DLOC VARCHAR2(25)

11. Types of data constrains.

a) not null constraint at column level.

Syntax:

<col><datatype>(size)not null
b) unique constraint

Syntax:

Unique constraint at column level.


<col><datatype>(size)unique;

c) unique constraint at table level:

Syntax:

Create table
tablename(col=format,col=format,unique(<col1>,<col2>);

d) primary key constraint at column level

Syntax:

<col><datatype>(size)primary key;

e) primary key constraint at table level.

Syntax:

Create table tablename(col=format,col=format


primary key(col1>,<col2>);

f) foreign key constraint at column level.

Syntax:

<col><datatype>(size>) references <tablename>[<col>];

g) foreign key constraint at table level

Syntax:

foreign key(<col>[,<col>]) references


<tablename>[(<col>,<col>)

h) check constraint

check constraint constraint at column level.

Syntax: <col><datatype>(size) check(<logical expression>)

i) check constraint constraint at table level.


Syntax: check(<logical expression>)

CREATING A TABLE WITH ‘PRIMARY KEY’ CONSTRAINT:

SQL> CREATE TABLE mdept252


(dno NUMBER(5),
dname CHAR(10),
dloc VARCHAR(10),
PRIMARY KEY (dno));

Table created.

ADDING A PRIMARY KEY TO AN EXISTING TABLE:

SQL> ALTER TABLE student252 ADD PRIMARY KEY (sid);

CREATING A TABLE WITH ‘FORIEGN KEY’ CONSTRAINT:

SQL> CREATE TABLE detailemp252


(eid NUMBER(5) REFERENCES mdept230 (dno),
ename VARCHAR(10),
esal NUMBER(7));

Table created

SQL> CREATE TABLE itm252


(ino NUMBER(3),
iname VARCHAR(10),
iprice NUMBER(4,3),
qtyonhand VARCHAR(5),
CONSTRAINT itm252_ino_pkkey PRIMARY KEY (ino),
CONSTRAINT itm230_qtyoh_chk CHECK (qtyonhand>1));

DQL Commands:

12. Viewing data in the tables: - once data has been inserted into a
table, the next most logical operation would be to view what has been
inserted.

a) all rows and all columns

Syntax:
Select <col> to <col n> from tablename;
Select * from tablename;

13. Filtering table data: - while viewing data from a table, it is rare
that all the data from table will be required each time. Hence, sql must
give us a method of filtering out data that is not required data.

a) Selected columns and all rows:


Syntax:
select <col1>,<col2> from <tablename>;
b) selected rows and all columns:
Syntax:
select * from <tablename> where <condition>;

c) selected columns and selected rows


Syntax:
select <col1>,<col2> from <tablename> where<condition>;

1) To list the first name and last name of persons in Karnataka.


SELECT first_name, last_name
FROM Customer
WHERE state = ‘KARNATAKA’;

2) To sort in the descending order.


SELECT state, first_name, last_name, pin
FROM Customer
ORDER BY state DESC;

14. Sorting data in a table.

Syntax:
Select * from <tablename> order by <col1>,<col2> <[sortorder]>;
CREATING,ALTERING AND DROPPING TABLES AND INSERTING ROWS INTO A
TABLE (USE CONSTRAINTS WHILE CREATING TABLES) EXAMPLES USING
SELECT COMMAND .

EXAMPLE 1:

CREATING A STUDENT RELATION TABLE WITH ALL DATATYPES:

SQL> create table student252(


sid number(5),
sname varchar(20),
sbranch char(5),
dob date,
spercent number(3,2));

Table created.

RELATIONAL SCHEMA FOR STUDENT RELATION :


SQL> desc student252;
Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(5)
SNAME VARCHAR2(20)
SBRANCH CHAR(5)
DOB DATE
SPERCENT NUMBER(5,2)

INSERT THE RECORDS INTO STUDENT RELATION:

METHOD 1:
SQL>Insert into
Student252(sid,sname,sbranch,dob,spercent) values(104,‘sri’,,’cse’,’27-
feb-05’,70);
1 row created.

METHOD 2:
SQL>Insert into
Student252 values(104,‘sri’,,’cse’,’27-feb-05’,70);
1 row created.

METHOD 3:
SQL>Insert into
Student252(sid,sname,sbranch,dob,spercent)
values(&sid, &sname,&sbranch,&dob,&spercent);
1 row created.
METHOD 4:
SQL>Insert into
Student252(sid,sname,sbranch,dob,spercent)
values(&sid, ‘&sname’,’&sbranch’,’&dob’,&spercent);
1 row created.

QUERY THE TABLE VALUES:

ALL ROWS AND ALL COLUMNS:

SQL> select * from student252;


SID SNAME SBRANCH DOB SPERCENT
------ --------------- --------------------- --------------- --------------------
130 ravi it 30-1-95 60
131 teja cse 21-07-87 55
129 kiran mech 12-05-92 60
104 sri cse 30-07-90 70
133 sajith eee 12-06-89 55
137 ram ece 07-07-85 40

You might also like