Lesson 06 (Managing Database)

 

Managing Database

  1. Create database - To create a database 

create database database_name

create database EEC


  1. Display database - To display database 

show databases  


  1. Select database - To select a database  

select database database_name

select database EEC


  1. Removing database - To delete a database 

drop database database_name

drop database EEC


Creating Table in Sql with Constraints (बिशेष शर्त के अनुसार )


Create table emp(empid int, ename varchar(40), salary int);

insert into emp values(1, ‘a’, 2000);

select * from emp;


Various Constraints


Not null or null

This constraints forces the values of a column to non- null values only (column में value भरना अनिवार्य है )


Syntax-

Col_name data_type not null;


create table emp1(empid int, ename varchar(40), salary int not null);

insert into (empid, ename) values(1, ‘a’);

क्योंकि salary के कॉलम को खाली नहीं छोड़ सकते इसलिए error  प्रदर्शित होगी |


describe emp;


table का structure प्रदर्शित होगा | इसमें आप देखंगे की salary को not null प्रदर्शित कर रहा है |


How to change any column to not null


  • Check data

  • Replace Null value to not null values

  • Alter table column.


We want to change salary column to not null column.

  1. Check data

  2. if there is any row who has null values than replace with 0


update emp;

→ set salary=0

→ where salary is null;


alter table emp;

→ change salary salary(int) not null;


insert into emp(empid) values(4);

it will show error, you can not leave salary field blank/not null


Unique constraints

This constraints forces the column to have unique values only, means no duplicat3e data in same column.


Col_name data_type unique.


Create table emp2(empid in unique, ename varchar(40), salary int not null);

now you can’t keep same values in two or more rows.


create table emp3(empid int, ename varchar(40), salary int, constraints u1 UNIQUE(empid, ename));

now empid & ename is unique columns.


Default constraints-

This constraints us used to insert a default values in a column.

col_name data_type defaultvalue;


create table emp4(empid int, ename varchar(40), salary int DEFAULT 10000);

insert into emp4(empid) values (3);

select * from emp4;


But you can insert any value in salary column also.


Auto increment Constraints

It will increase automatic values and by default it is 1 and auto increment by 1


create table emp5(empid int auto increment, ename varchar(40) salary int);

(empid is primary key)

Comments

Popular posts from this blog

Lesson 07 (Primary Key & Foreign Key)

Lesson 03 (Filter Data)

Lesson 05(Modifying Data)