0507241547318213
0507241547318213
Example:-
Add
Modify
Delete(Drop)
Rename
the columns in an existing tables
ALTER TABLE - ADD
Column
ALTER TABLE table_name
ADD column_name datatype;
Example:
Example:
Example:
Example:
After column
definition
EmpCode Ename Gender Grade GrossSal
1101 Brain M A 9000
1102 Calvin M B 8000
1103 Tom M A 9000
1104 Jerry F E 3000
CREATE TABLE EmpNoCons
(
Empcode int ,
Ename varchar(12),
Gender char(1) ,
Grade char(1) ,
GrossSal int
);
EmpCode Ename Gender Grade GrossSal
1101 Brain M A 9000
1102 Calvin M B 8000
1103 Tom M A 9000
1104 Jerry F E 3000
Example:-
CREATE TABLE EMP (
Empcode int Primary key,
Ename varchar(12) not null,
Gender char(1) not null,
Grade char(1) Default ‘E’,
GrossSal int check GrossSal>3000
);
Comparison before and after constraints:
SQL DELETE Statement
The DELETE statement is used to
delete existing records in a table.
DELETE FROM table_name WHERE
condition;
DELETE FROM studetails WHERE adno=101;
DELETE FROM studetails WHERE name =‘anu’;
UPDATE Customers
SET ContactName='Julie'
WHERE Country=‘India';
SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new
records in a table
1. Specify both the column names and the
values to be inserted:
Parent Table
SQL FOREIGN KEY on CREATE TABLE
CREATE TABLE Lib
(
BookID int NOT NULL,
BookNumber int NOT NULL,
AdNo int,
PRIMARY KEY (BookID),
FOREIGN KEY (AdNo)
REFERENCES StuDetails(AdNo)
);
create table Lib(BID int,bno int not null, Adno
int,primary key(BID), foreign key(Adno) ref
erences s111(Adno));
Child Table
insert into StuDetails values
(101,'Kaira',13),
(102,'Galina',14);
insert into lib values
(111,1234,101),
(112,1235,102),
Will throw an error
(113,1236,103); as 103 not in parent
◾ MySQL allows us to give names to
constraints to that when error occurs due to
constraint violation then this name will
appears to help us in identifying for which
column this error occurs.
Ex:
create table training (empno int, traName varchar(20),
startdate date, enddate date,
constraint myfkey
foreign key(empno) references ABCLtd(empno));
UNIQUE Constraint on CREATE TABLE
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID) UNIQUE constraint
);
desc abc
check PRIMARY KEY is by inserting duplicate eno
Insert unsuccessful
as matching eno is
not found in ABC
◾ Note: after foreign key is applied, we cannot
delete any record or update primary key value
in master table because its related records
will be in foreign key table