0% found this document useful (0 votes)
2 views4 pages

Database

The document outlines the fundamentals of Relational Database Management, focusing on Data Definition Language (DDL) and Data Manipulation Language (DML). It provides examples of creating tables, defining attributes and keys, as well as manipulating data through various SQL commands such as SELECT, INSERT, UPDATE, and DELETE. Additionally, it includes information on setting up foreign and primary keys, along with basic functions for data aggregation.

Uploaded by

Bishal Tiwari
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)
2 views4 pages

Database

The document outlines the fundamentals of Relational Database Management, focusing on Data Definition Language (DDL) and Data Manipulation Language (DML). It provides examples of creating tables, defining attributes and keys, as well as manipulating data through various SQL commands such as SELECT, INSERT, UPDATE, and DELETE. Additionally, it includes information on setting up foreign and primary keys, along with basic functions for data aggregation.

Uploaded by

Bishal Tiwari
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/ 4

1

Relational Database
DDL - Data Definition Language
Table Creation
Attributes add
Define keys
DML - Data Manipulation Language
Manipulate Table
Update
Add
Remove

Data Definition Language:

CREATE TABLE student (


id CHAR(10) PRIMARY KEY, -- Every id must be of 10 characters, unique
and not null
name varchar(50), -- 1 to 50 characters
age number NOT NULL, -- Number
phone_number UNIQUE,
FOREIGN Key (classId) REFERENCES class(Id)
)

CREATE TABLE class(


id PRIMARY KEY,
description varchar(200)
)

ER - Diagram:

erDiagram
student ||--o{ class : "has"
student {
string id
string name
int age
string classId
}
class {
string id
string description
}

Primary key:
unique

[email protected]
2

not null

Data Manipulation Language


SELECT <col1, col2, col3> FROM <table_name> (Retrieves data from table)

SELECT name FROM student; -- Gives value of name column from table student

SELECT * (Retrieves data of all column from a table)

SELECT * FROM student; -- Retrieves all data from table student

WHERE <condition> (Checks condition before displaying data)

SELECT * FROM student


WHERE age > 16; -- Retrieves all data of students whose age is greater than
16

ORDER BY <col name> <asc/desc> (Arranges the data based on value of a column)

SELECT * FROM student


WHERE age > 16
ORDER BY age DESC; -- Retrieves student > 16yrs, sorts by age in
descending(decreasing)

GROUP BY <col name> (GROUPS data)

SELECT classId, AVG(age) FROM student


GROUP BY classId; -- Groups by class and displays average age of each group

INSERT INTO <table_name> (<col1, col2, ...>) VALUES (<value1, value2, ...>)

INSERT INTO Student (id, age, name, phone_number)


VALUES (1, 23, Bishal, 98080); -- Choose column

INSERT INTO Student


VALUES (2, Sagar, 18, 980); -- Compulsorily every column

INSERT INTO Student


VALUES (3, Ram, 19, 980807),
(4, Hari, 21, 985454); -- For multiple values
[email protected]
3

UPDATE <table_name> SET <col_name> = <value>,... WHERE <condn>;

UPDATE Student
SET phone_number = 9808076305
WHERE id = 1; -- Changes phone number of student with id 1

UPDATE Student
SET phone_number = 1; -- Changes phone number of all students to 1 (We get
error here!)

DELETE FROM <table_name> WHERE <condition>;

DELETE FROM Student


WHERE id = 4;

DELETE FROM Student; -- Removes all values from Student table

Functions
Must be in group
AVG(), COUNT(), MIN(), MAX()

SELECT COUNT(*) as COUNT -- For alias (nickname)


FROM Student; -- Student table is single group

SELECT classID, COUNT(*), AVG(age)


FROM Student
GROUP BY classId; -- Displays count of student in each class

Setting up foreign key and primary key

CREATE TABLE STUDENT_TEST (


StudentID INTEGER,
TestID VARCHAR(10),
Mark INTEGER,
PRIMARY KEY (StudentID, TestID),
FOREIGN KEY (StudentID) REFERENCES STUDENT(StudentID),
FOREIGN KEY (TestID) REFERENCES TEST(TestID)
);

[email protected]
4

ALTER TABLE

[email protected]

You might also like