You are on page 1of 26

Constraint

Unsigned: This means the value in that column are


Positive. This clause should be written immediate after
the data type.
Eg. roll_no int unsigned
NOT NULL: Not null means that the column value
cannot be empty.
Default: Default statement is used to give a column a
fixed value, if no value is assigned to it.
Auto_Increment: the Auto_increment is used to
indicate that MYSQL should automatically generate a
number for that column as the value of the previous
row+1 (R2=R1+1,R3=R2+1).
It can only be used with integer data types.
There can be only one Aut_Increment column in a
table.
Primary Key: The primary key constraint uniquely
identifies each record in a table. Primary key must
contains unique values, and cannot contains null values.
A table can have only one primary key.
Foreign key: A foreign key is a key used to link two
tables together.
The relationship between two tables matches the
primary key in one table with a foreign key in the second
table.
Table1:student
P F

ID Name Age city

1 Sachin 23 2

2 Virat 19 1

3 Rohit 20 2
Table 2:City table
P
F

cid city

1 delhi

2 Mumbai
F

FOREIGN KEY (COL_NAME) REFERENCES


TABLE2_NAME(COL_NAME)

P
empid empname depti city mgrid salary doj
d

1 Nitin Joshi 102 Mumbai 512 23000.00 6/10/1995


2 Jignesh shas 101 Jaipur 515 45000.00 5/3/1990
3 Deepak 102 Mumbai 512 35000.00 2/9/1999
Majundar

4 Fridosh Mehta 101 Pune 515 29000.00 8/9/2002

5 Suresh Kumar 103 Pune 511 40000.00 2/3/95

6 Dinkar 103 Jaipur 511 48500.00 9/5/2001


7 Anil 102 Pune 512 33000.00 1/12/2002
8 Sachin 101 Mumbai 515 43500 12/1/2000
Student1
Roll no Name Age Marks

1 SACHIN JAIN 21 234

2 VIMAL JOSHI 22 180

3 RIYA PATEL 19 200

4 KIRAN MEHTA 23 210


Pay
empid name age city salary dob
1 pinaz 30 mumbai 450000 17\06\1979
2 sharukh 16 pune 200000 28\9\1993
3 riyaz 25 delhi 500000 02\03\1984
4 hiral 28 NULL 350000 NULL
SQL
(Structrured Query
Language)
1. Database :Collection of related tables;
2. Table : Collection of related row & columns;
3. Row : Collection of related columns makes a row
(record) e.g. Name ,address, city , dob of a person
together will make a row.
4. Column : Particular information is called a
column
SQL Statements
1. Schema statements : these are used to define the data structures storesd in the
database. Eg. CREATE TABLE , DROP TABLE,ALTER TABLE etc.
2. Data statements: Data statements are used to manipulate the data structures
previously defined using SQL schema statements eg. INSERT ,SELECT,
DELETE, UPDATE etc.
3. Transaction statements: These are used to begin , end and roll back
transactions
Data Types
1. Character Data:
a) CHAR(n)
b) VARCHAR(n)
c)TEXT
d)MEDIUMTEXT
e)LONGTEXT
2.Numeric Data :
a)SMALLINT -32,768 TO 32,767
b)INT -2147483648 TO 2147483647
c)BIGINT -9223372036854775808
d)FLOAT(w,d)
e)DOUBLE(w,d)
f)DECIMAL(w,d)

3.Date and Time:


a)Date ‘yyyy-mm-dd’
b)Time hhh:mi:ss
c)Timestam yyyy-mm-dd hh:mm:ss
4.Boolean : true or false 1 or 0
Create

Create database : will create database which will


contains tables
eg. Create database test;
Once you create database you have to go into that
database to create tables and you can go into database
by using USE statement
eg use test;
Create table : this statement is use for making new
table
Create table
Create table name_of_table
( name of col data type,
Name of col data type,

..
);
Emp table details

Table Emp
Creating table dept
Create table emp
( emp_no smallint primary key,
F_name varchar(15),
L_name varchar(15),
J_code smallint ,
H_date date,
Sal int );
Insert into Table
Insert into EMP
Values( values in the order of colmns given at the time
of creation.);
eg.
Insert into emp
Values(1, ’amit’, ’shah’ , 2 , ’1992-11-21’ ,84000);
Insert
Insert into emp
Values(1,’samir’,’desai’,1,’2004-1-12’,12000);
Inserting more values
Insert into emp
Values(2,’sandip’,’more’,2,’2006-9-21’,10000),
(3,’sarika’,’nene’,1,’2008-12-1’,8500),
(4,’sapana’,’lele’,2,’2007-10-11’,9000);
Insert into Table
If data is not provided for all columns then column
names for which the data is provided is to be mention
in the parenthesis
E.g.
Insert into emp
(emp_no,L_name,J_code,sal)
Values(5,’Mehta’,2,30000);
Update & set
Existing records can be modify
 eg.
Update student
Set name=‘sunil’
Where r_no=1;
Change surname to ‘patel’ and salary to 45000 for
emp_no 10.
Update emp
SET l_name=‘patel’,sal=45000
Where emp_no=10;
Update & Set
The update and set statements are used to modify the
data in the rows of table.
E.g. to change the age of employee id 4(hiral) to 29 in
pay table the command is
Update pay
Set age=29
Where empid=4;
Syntax
UPDATE table_name
SET column 1=value 1,column 2=value 2………
WHERE condition;
HW
Give values to the city and dob columns for empid=4
which are at present NULL
To increase age by 1 and the salary by 5000 for rows
with city Mumbai
Creating a table using another Table
CREATE TABLE table_name
SELECT column 1,column 2….
FROM table_name;
Alter table
To Add column
Alter table table_name
Add Column_name datatype;
To add multiple colums
Alter table table_name
Add (col_1 datatype

You might also like