Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.0 kB
15
Indexable
Never
27/02/2024
=================================================
Contraints:-
contraints are the rules that we can apply the types of data in the table.
Commonly used contraints

1:-Not Null:-Ensure that the column cannot have null value.
2:Unique :-ensure that all values in a column are different.
3.Default:-set a default value for column if no value is specified.
4.primary key:-combination of not null and unique
5.foreign key :-a foreign key is a column used to link between two more tables together.
      a table have any number of forein key ,can contain duplicate and null values

6.check :- ensure thet the value in a column satisfies a specific condition


Syntax:-
create table tablename(colum1 datatype contraint, column2 datatype containt,..........)


=====================================================================================================
foreign key:
---------------------------
A foreign key is a concept that is often used in relational databases. 
It is a way to create a link between two different tables. 
A foreign key is a field that refers to another table's primary key

foreingn key contain duplicate and null value.

syntax:-
create table childtable(column1 value1,contraint,
col2 val2, cont2...............
pcoloumn datatype ,
foreign key(pclolumn) references parenttable(pcolumn));
------------------------------------------------------
exa:-
create table company( comp_id int primary key ,comany_name varchar(20) not null, address varchar(20));

this syntax work on mysql

create table Employee1(emp_id int  ,
first_name varchar(20) not null ,
last_name varchar(22) unique ,
age int check (age >22),
salary int,
addr varchar(20) default "pune",
comp_id int ,
primary key(emp_id),
foreign key(comp_id) references company(comp_id)
);


This syntax is not working on mysql

create table Employee1(emp_id int primary key auto_increment ,
first_name varchar(20) not null ,
last_name varchar(22) unique ,
age int check (age >22),
salary int,
addr varchar(20) default "pune",
comp_id int foreign key references company(comp_id)
);


insert into company values(101,"infocys","pune"),(102,"wipro","nashik"),(123,"cognizent","pune"),(112,"tata","nagpur");
select * from  company ;
select * from employee1;

insert into employee1 values(13,"ankush","jadhw",45,40000,"pune",Null);

create table person(p_id int primary key auto_increment,P_name varchar(20),age int ,addr varchar(20));

insert into person(P_name,age,addr) values("suyash",25,"pune"),("rahul",22,"nagpur"),("neha",25,"nashik"),("pranali",30,"chandrapur");
insert into person value(101,"ratna",22,"pune");
select * from person

==============================================================================================================================================

Assignment 
create 2 table customer and order
customer is a parent table:
feild:
cust_id 
cust_name
addr
gmail


order table :- child table
ord_id,
item
price,
cust_id


Leave a Comment