Professional Documents
Culture Documents
-- DDL : create,alter,truncate,drop
-- DML : insert,update,delete
-- CONSTRAINTS : rules/limitations
-- not null,unique,primary key,foreign key,check,default,auto_increment
-- UNIQUE : restricts the entry of duplicates, but null values are allowed
create table emp_unk(empid int UNIQUE,emp_name varchar(10));
desc emp_unk;
insert into emp_unk values(1001,'rahul');
select * from emp_unk;
insert into emp_unk values(1001,'rahul');
insert into emp_unk values(null,'rahul');
-- PRIMARY KEY :
-- uniquely identify a row
-- restrict the entry of null as well as duplicates
-- FOREIGN KEY :
select * from student;
/*
student course
roll_no,stud_name,address,course_id course_id,course_name
,roll_no
101 rahul pune 10 10 data science
,101
102 gaurav. mumbai. 20. 30. java.
,103
roll_no: pk rollno: FK
parent table child table
referenced table referencing table
*/
-- can not insert a value in child table which is not present in parent
table
insert into course values(10,'data science',106);
-- DEFAULT
create table emp_dflt (empid int,emp_name varchar(10),loc varchar(10)
DEFAULT 'pune');
insert into emp_dflt values(1001,'rahul','mumbai');
select * from emp_dflt;
insert into emp_dflt(empid,emp_name) values(1002,'gaurav');
-- CHECK
create table emp_chk (empid int, emp_age int check(emp_age>20));
insert into emp_chk values(1001,25);
select * from emp_chk;
insert into emp_chk values(1001,19);
-- AUTO_INCREMENT:
create table emp_auc (empid int AUTO_INCREMENT PRIMARY KEY,empname
varchar(10));
insert into emp_auc (empname) values('rahul');
select * from emp_auc;
insert into emp_auc (empname) values('gaurav');
desc student;
insert into student values (107,'priya','mumbai');
select * from student;
insert into student values (null,'priya','mumbai');
insert into student (stud_name,address) values ('sneha','mumbai');