0% found this document useful (0 votes)
6 views8 pages

All in One

The document outlines the structure and commands related to database management using SQL, including creating databases and tables, defining constraints, and manipulating data. It covers various SQL commands such as DDL, DML, and DRL, along with examples for creating, altering, and deleting tables and records. Additionally, it explains key concepts like primary keys, foreign keys, and data types essential for understanding relational databases.

Uploaded by

paliwalvaibhav52
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

All in One

The document outlines the structure and commands related to database management using SQL, including creating databases and tables, defining constraints, and manipulating data. It covers various SQL commands such as DDL, DML, and DRL, along with examples for creating, altering, and deleting tables and records. Additionally, it explains key concepts like primary keys, foreign keys, and data types essential for understanding relational databases.

Uploaded by

paliwalvaibhav52
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

S.

NO TOPIC MARKS
1 Python Programming (Logic) 40
2 Computer Network ( Theory) 10
3 Database Management ( SQL) 20
4 Project + Report File 30
5 Practical -
TOTAL 100
Sumita Arora – Dhatpat Rai

1. Create database wps;


(Note : Database Created if the same name database not exist.)

2. use wps;
(We can use wps database container to create table, views, procedure, triggers, cursor, etc.)
3. Create table;
Table is also known as Relation.
Definitions :
Database : A Database may be defined as a collection of interrelated data stored together to serve multiple
applications.
interrelated data = Relational Database Management System.
BDMS, RDBMS
Table : Relation. It is Collection of Rows and Columns. Or It is Collection of Tuples and Attributes
Tuples : The Row of Tables(relations) are generally referred to as Tuples.
Attributes : The Columns of table are generally referred as Attributes.
Cardinality : The number of Tuples (Rows) in a table is Called Cardinality.
Degree : The Number of attribute in a table is called Degree.

Primary Key : Unique + Not Null, A set of data which is uniquely identify.
Candidate key : All attribute (column) combinations inside a relation that can be serves as primary key they are
called candidate key.
Alternate Key : A candidate key that is not the primary key is called an alternate key.
Foreign Key : A Non-key attribute , whose values are derived from primary key of some other table, is known as
Foreign Key.
SERVER : MySQL Server : On which data can be store or retrieve.
CLIENT : Are programs which can retrieve data from the server as well as it can save data onto the server.

Step 1 :
How to start MYSQL Server -> Start -> All Programs -> MYSQL -> MYSQLServer5.2 -> MYSQL command Line
Client
(Note : MySQL provide us system as default USERNAME – no need to type any username)
Step 2 : entered Password :
My SQL > root (yellow color text will be appeared as ****)

Step 3 : If Database not exist so we can create a new data base , if it is already exist than we can use the same
a. If Database not exist so we can create a new data base:
Command to create database
 Create database databasename;
Example:
 Create database wisdom;
b. If Database exist so we can create a new data base:
 Use Database name
Example :
 Use wisdom;
Step 4 : We assume that we are in our database.
Create table .
1. Data Definition Language : DDL
a) Create b) Alter c) Drop
2. Data Retrieval Language : DRL
a) Select
3. Data Manipulation Language : DML
a) Insert b) Update c) Delete
4. Transaction Control Language: TCL
a) Commit b) RollBack c) Savepoint
5. Data Control Language : DCL
a) Grant b) Revoke

CONSTRAINTS are the rules enforced on data columns on table. These are used to limit the type
of data that can go into a table. This ensures the accuracy and reliability of the data in the
database.

Constraints could be column level or table level. Column level constraints are applied only to one
column, whereas table level constraints are applied to the whole table.

Following are commonly used constraints available in SQL. These constraints have already been
discussed in SQL - RDBMS Concepts chapter but its worth to revise them at this point.
1. NOT NULL Constraint: Ensures that a column cannot have NULL value.
2. DEFAULT Constraint: Provides a default value for a column when none is specified.
3. UNIQUE Constraint: Ensures that all values in a column are different.
4. PRIMARY Key: Uniquely identified each rows/records in a database table.
5. FOREIGN Key: Uniquely identified a rows/records in any another database table.
6. CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy
certain conditions.
7. INDEX: Use to create and retrieve data from the database very quickly.

DATA TYPES :
1. Numeric(mysql), Number(Oracle)
2. Varchar(Mysql), varchar2(Oracle)
3. Date (mm,dd,yyyy)(dd,mm,yyyy),(yyyy,mm,dd) MYSQL, dd/mmm/yyyy - > Oracle
4. Int, double, long ( Numeric)
5. Char, character, text ( varchar)

Data Definition Language : DDL


1. Create :
a. Name of table
b. Number of Columns(attribute/fields)
c. Name of attributes
d. define / allot constraint to attribute
e. Define / allot data types to attribute.

Code to Create table :


> Create table login (uid1 varchar(20), pwd1 numeric(10), role varchar(20));
> Create table if not exists login1 (uid1 varchar(20), pwd1 numeric(10), role varchar(20));
> DESC - stands for Description/ used to view the structure of table

> Drop table login;


(this code will delete table from database/ Means it Drops the structure (existence of Table))
> Create table login (uid1 varchar(20) primary key, pwd1 numeric(10), role varchar(20));
> DESC

Point to remember : No space will be there in field name, to special symbol can be used to use a identifier(field
name) except underscore.

Example :
(This command will show error : identify the erros);
create table personal details (admno numeric(4), father's name varchar(20),
varchar(20), mother's name varchar(20), dob date);
date)

RECTIFIED :
create table personaldetails
details (admno numeric(4), fathers_name
f varchar(20), mothers_name
name varchar(20), dob date);
date)

CONSTRAINTS :
Primary Key :Create table login (uid1 varchar(20)
varchar(20) primary key, pwd1 numeric(10), role varchar(20));
Unique :Create table login (uid1 varchar(20) unique, pwd1 numeric(10), role varchar(20));
Not Null :Create table login (uid1 varchar(20) Not Null key, pwd1 numeric(10), role varchar(20));
Default :Create
te table login (uid1 varchar(20) primary key, pwd1 numeric(10) default 1111, role varchar(20));
Check :Create table Employee (empid varchar(20) primary key, pwd varchar(10), salary numeric(10,2) check
(salary>1000));

CONSOLIDATE COMMAND FOR CONSTRAINTS


create
reate table login5 (uid1 varchar2(20) primary key , pwd1 varchar2(20) not null, role varchar2(20) not null, doj date
unique not null, age number(3) default(24), salary number(9,2) not null check(salary>15000));
Create Command Completed:

Create View : It is the Mirror Image of Table(Relation).


Syntax: Create view viewname as select fieldname1,fieldname2,… from tablename
Example :Create view login_view as select uid1,role from login;

Alter : It used to make any change in attribute of table.


Syntax :
Alter tabletable name add/modify/drop column_name variable(size)
TYPE OF ALTER COMMAND :
1. ADD : it is used to add a column/field/attribute in an existing table;
Example : alter table login add dob varchar(20);
2. MODIFY : It is used to make any change in any column/field/attribute of an existing table.
Example : alter table login modify dob date;
3. DROP : It is used to delete a column/field/attribute in an existing table.
Example : alter table login drop dob; FOR MYSQL
Alter table login drop column dob ;  for ORACLE
(Note : data type and its size)

for mysql
1. alter table login add primary key(uid1);
2. alter table login drop primary key;
3. ALTER TABLE login ADD CONSTRAINT FK_uid1
FOREIGN KEY (uid1) REFERENCES login(uid1);
for oracle :
1. alter table login add constraintPk_cons primary key(uid1);
2. alter table login drop constraint pk_cons;
3. same as mysql code;
Create :Foreign key
create table sales (icodenumber(5) references inventory(icode), name varchar2(20), sale_Price
number(5), qty number(5))
Rename Table : alter table tablename rename to new_table_name;
Rename Column : alter table tablename rename column column_name to new_Column_Name;

DROP : It is very important command because this command never be revoke, revert, rollback. So always use this
command when you are very sure to use it. Means once a table delete, it delete for forever.
1. Drop table : It is used to delete entire table with all attribute and data.
Example : drop table personaldetails;
2. Drop view :It is used to delete entire table with all attribute and data.
It table drop, it doesn’t view drop. Means view will exist but it will not show you any data.
Whenever table again create then view start its working.

6. DATA RETRIEVAL LANGUAGE :


KEYWORD :Select

It is used to retrieve/show data from table/view.


It can also have some causes which I will discuss tomorrow.
This year, it is in your syllabus.

Syntax : select field_name(s) from table_name


• Select uid1,role from login;
• Select * from login;

DDL :
1 Create
2 Alter
3 Drop
I Drop Table;
Syntax : Drop <Table Table name>
Eg : drop table login;
Ii Drop View
Syntax : Drop view <View Name>
Eg : Drop view loginview;
2. DRL : Select
3. DML : : It means Data Manipulation language.

a. Insert : It always create/add new line(record/tuple) in a table. Means if there are 12 rows in a
table , after using insert command it becomes 13.
i. insert values for all fields
syntax : insert into <tablename> values(field1,field2,….)
eg : insert into login values(‘mohit’,1232, ‘user’,’2001/01/25);
Note : Remember values will be as same as present in table.

Conclusion : It gives an error, because table have 4 fields but query inserted only 3 fields.
New query

Conclusion : Error : because of order is not same as in table;

ii. insert values for selected fields : above errors can be handle through this command
syntax : insert into <tablename> < field1, field2,…>values<value1, value2, …))
eg : insert into login (uid1,pwd1,dob)values(‘Eshaj’,2512,’2004/01/05’);

Eg 2 : insert into login (pwd1,dob,uid1)values(2512,’2004/01/05’,’Guarang’);


Problem : field name and values mismatched

b. update
c. Delete
DAY 2
DML :
1. Insert
2. Update
3. Delete

2. Update : It is used to make changes in an existing record…


IMP Note : Remember no new Record will be create. Means if no. of rows are 12 then after using update
command it becomes 12.
Syntax : update <tablename> set field1=value1, field2=value2, …. Where uid1=’value1’
Original table

Eg 1 :
Update login set role=’Student’;
OUTPUT : update student in if rows of role

Query with where clause


Update login set role=’User’ where pwd1=2512;
Last Example :
Update login set pwd1=10110, role=’Admin’ where pwd1=2512 and uid1=’Eshaj’;
OUTPUT

Important note : Clauses are always optional in any query, Means any query can be run without where clause. We
use where clause to differentiate the records… otherwise it will homogeneous.
iii. Delete : This command is used to delete rows from table;.
Syntax : delete from <tablename> where field=valeue;
Eg :
Delete login where uid1=’pooja’;
OUTPUT
OLD ( 5 ROWS) NEW ( 4 Rows)

Important : Always think twice before using delete command…. We have no option to rollback(Retrieve)
OLD ( 4 ROWS) NEW ()
Select * from garv; Delete from garv;

You might also like