Uploaded by Robin Thapa

database mysql mssql

advertisement
Database Management System.
DDL (Data Definition Language) Command : DDL or Data Definition
Language actually consists of the SQL commands that can be
used to define the database schema. Different types of DDL
command are explain below with example of each.
I.
CREATE: used to create database or its objects (table,
views, store procedure, function, triggers etc.)
II.
DROP:
used
to
remove
database
or
its
object
from
database.
III.
ALTER:
used
to
alter/modify
the
structure
of
the
database
IV.
TRUNCATE: used to remove all the data along with spaces
allocate for the records.
V.
RENAME:
used
to
rename
an
object
existing
in
the
database.
CREATE Database Statement: Used to create Database (MYSQL and
MSSQL)
CREATE DATABASE tutorials;
DROP Database Statement: Use to drop the existing database
(MYSQL and MSSQL)
DROP DATABASE tutorials
CREATE Table Statement: Used to create table in Database
CREATE table student
(
id int primary key,
name varchar (40),
address varchar (40),
gender char (10)
)
DROP
Table
Database.
statement:
Used
to
drop
existing
table
from
Drop table student
ALTER Table Statement: The ALTER TABLE statement is used to
add, delete, or modify columns in an existing table. The ALTER
TABLE statement is also used to add and drop various
constraints on an existing table.
ALTER Table ADD Column (MSSQL/MYSQL)
ALTER table student
add phone varchar(30)
ALTER Table DROP Column (MSSQL/MYSQL)
ALTER table student
Drop column phone
ALTER Table Modify/Alter Column: to change the data type
of column in the table.(MYSQL)
ALTER table student
alter column name varchar (60)
ALTER Table Modify/Alter Column: to change the data type
of column in the table. (MYSQL)
ALTER table student
modify column name varchar (60)
ALTER Table Rename: to
Database.(Only in mysql.)
change
the
table
name
in
ALTER table student
RENAME to students
ALTER Table Rename: to change the table name in Database.
(In mssql sp_rename is used to change the table name.)
EXEC sp_rename 'student', 'students'
Truncate Command: to remove all the data record and space
allocate to data.
TRUNCATE table departments
SQL Constraints: used to specify the rules over the data.
Ensure the consistency, reliability and integrity of data in
the database. There are 7 different types of constraints.
a. NOT NULL: ensure that, column cannot have null value.
b. UNIQUE: ensure all the columns value must be unique.
c. PRIMARY KEY: uniquely identified each row in table.
d. FOREIGN KEY: reference of primary key from another table
to establish the relationship between two table.
e. CHECK: ensure that all the values in the column satisfy
the given condition.
f. DEFAULT: used to set default value for any column user
do not supply value.
g. INDEX: used to create index for quick retrieval of data
from database and table.
MSSQL: Constraint in alter
alter table student
alter column name varchar (60) not null;
MYSQL: Constraint in alter
alter table students
modify column name varchar (60) not null;
Constraint in table creation:
create table course
(
course_id int primary key,
course_code varchar(10) unique,
course_name varchar (70) NOT NULL unique,
credit_hours int check (credit_hours<4 and
credit_hours>0),
created_on date default getdate()
)
Use curdate() in mysql .
Constraint in table creation with constraint name:
create table teachers
(
tid int ,
name varchar(40) not null,
address varchar(50) not null,
gender char (8),
phone varchar(40),
CONSTRAINT teacher_tid primary key(tid),
CONSTRAINT gender_check check (gender='Male' or
gender='Female' or gender='Others'),
CONSTRAINT phone_unique unique (phone)
)
Constraint in table with alter command:
alter table teachers
add constraint address_check check (address='KTM' or
address='BRT' or address='BRJ')
DROP Constraint in table with alter command:
alter table teachers
drop constraint address_check
Foreign Key in Table using constraint:
create table teach
(
id int primary key,
cid int,
tid int,
constraint
references
constraint
references
teach_course_FK foreign key (cid)
course(course_id),
teach_teacher_FK foreign key (tid)
teachers(tid)
)
Foreign Key in Table without constraint:
create table study
(
id int primary key,
sid int foreign key references student(id),
cid int foreign key references course(course_id),
)
Index Creation:
create index indx_student
on student (id)
DROP Index:
MSSQL:
drop index indx_student on student
MYSQL:
alter table students
drop index indx_student
AUTO Increment in SQL
MYSQL:
create table departments
(
did int not null AUTO_INCREMENT,
dept_code char(10) not null,
dept_name varchar (40) not null,
created_on date default curdate(),
PRIMARY KEY(did)
)
MSSQL:
create table departments
(
did int not null identity(1,1),
dept_code char(10) not null,
dept_name varchar (40) not null,
created_on date default getdate(),
PRIMARY KEY(did)
)
DML (Data Manipulation Language) Command : The SQL commands
that deals with the manipulation of data present in the
database belong to DML or Data Manipulation Language and this
includes most of the SQL statements.
I.
II.
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a
table.
III. DELETE – is used to delete records from a database
table.
Insert Command (MSSQL/MYSQL): used to insert data into table.
insert into student
values
(1,'Ram Prasad Dhungel','Lalipur, Satdobato','Male','9844447236');
insert into student
values
(2,'Smiriti Bhattarai',NULL,'Female','9844007236');
insert into teachers
(tid,name,address,gender,phone)
values
(1,'Krishna Dahal','BRT', 'Male','9854037008');
insert into course
values
(1,'IT210','Computers Information System',3,'')
insert into course
(course_id,course_code,course_name,credit_hours)
Values
(2,'IT211','Digital Logic Design',3)
(Note: We will discuss more advance form of INSERT command in later topic)
Update Command (MSSQL/MYSQL): used to modify data into table.
update student
set address='Lalitpur,Satdobato'
where address='Lalipur, Satdobato';
update students
set address='Lalitpur,Satdobato'
where id='1';
update student
set address='Kathmandu,Koteshwor' , gender='Female'
where id='2';
(Note: We will discuss more advance form of UPDATE command in later topic)
Delete Command (MSSQL/MYSQL): used to delete data from table.
delete from student where id=1; //Only student id 1 will deleted.
delete from student; // all the records of student will be deleted
(Note: We will discuss more advance form of DELETE command in later topic)
DQL (Data Query Language) Command : used to query data from
the database. Only SELECT command is DQL, also called Data
manipulation language.
select * from student; //retrieve all the records from student table.
select * from teachers where tid=1; // retrieve all the records from
teacher having tid 1
select name,phone from teachers; // retrieve specific record as you
want.
(Note: We will discuss more advance form of SELECT command in later topic)
Download