MY SQL
Relation
Tabular representation of data in rows and columns where each row represents about an entity and each column
represents about an attitude of entities which come from common domain.
Tuple
Each row in a table is called tuple.
Degree of a relation (ARITY)
Number of Columns presents in the table.
Cardinality of Relation
The number of tuple presents for a particular instances of time.
Super key
The super key is a set of a relation by which we can identify a tuple from therelation uniquely.
Candidate key
The minimal super key of a relation is called candidate key.
Primary key
Primary key is a candidate key chosen by DBA (Database Administration).
Alternate key
The candidate keys other the primary key.
Foreign key
The primary key of a relation appears as a non-key attribute of some other associated relation. Such a non-key attribute is
called a foreign key.
Student
Roll Name
1231 Jhon
1232 Lata
1233 Rabi
1234 Khan
1235 Pratap
1236 Saroj
1237 Amit
1238 Suresh
1239 Peter
1240 Mary
Subject
Paper ID Paper Name
1 PROG CONCEPTS
2 DATA STRUCTURE
3 OPERATING SYSTEM
4 NETWORKS
RESULTS
Roll Paper ID Marks
1231 1 30
1231 3 43
1232 1 45
1232 4 51
1233 3 76
1233 4 69
1234 1 59
1234 2 75
1238 2 62
1238 3 57
1239 2 36
1240 1 57
Query
[i] Show marks of all subjects of all students
[ii] Show roll, name, paper ID, paper name and marks of students whose roll is 1231.
[iii] Show roll, name, paper ID, paper name and marks of students whose marks between 50 to 70
[iv] Show roll, name, paper ID, paper name and marks of students whose marks less than 40 or greater than 70.
[v] Show roll, name and marks of students whose marks are greater than 50 in paper ID 1.
[vi] Show roll, name and total marks of all students
[vii] Show roll, name, paper ID, paper name and highest marks of subjects
[viii] Show roll, name, paper ID, paper name and marks of students whose marks greater than average marks
[ix] Show roll, name and number of subjects of all students
[x] Show roll, name, paper ID, paper name and highest marks of operating system
[xi] Show roll, name and highest total
STUDENTS [ROLL, NAME]
Super key – [Roll]
[Roll, Name]
Candidate Key – [Roll]
Primary key – [Roll]
Alternate key – [X]
Subjects – [Paper ID, Paper name]
Super key – [Paper ID]
[Paper name]
[Paper ID, Paper Name]
Candidate Key – [Paper ID]
[Paper Name]
Primary Key – [Paper ID]
Alternate Key – [Paper Name]
Result – [Roll, Paper ID, Marks]
Super Key – [Roll, Paper ID]
[Roll, Paper ID, Marks]
Candidate Key – [Roll, Paper ID]
Primary Key – [Roll, Paper ID]
Alternate Key – [X]
Foreign key – [Roll] Reference students
Foreign key – [Paper ID] Reference subjects
Student [Roll, name]
Super key- (Roll)
(Roll, Name)
Candidate key- (Roll)
Primary key-(Roll)
Alternate key-x
Subjects – [Paper ID, Paper name]
Super key – [Paper ID]
[Paper name]
[Paper ID, Paper Name]
Candidate Key – [Paper ID]
[Paper Name]
Primary Key – [Paper ID]
Alternate Key – [Paper Name]
Result – [Roll, Paper ID, Marks]
Super Key – [Roll, Paper ID]
[Roll, Paper ID, Marks]
Candidate Key – [Roll, Paper ID]
Primary Key – [Roll, Paper ID]
Alternate Key – [X]
Foreign key – [Roll] Reference students (Roll)
Foreign key – [Paper ID] Reference subjects (Paper ID)
HOW TO CREATE DATABASE IN SQL
CREATE DATABASE (name)
Use (database name)
Creating table
Create table(name)
Roll int,
Name varchar(20),
Primary key(roll));
Some commands
DESC STUDENT ---- IT WILL SHOW HOW MANY TUPLES ARE THERE.
SHOW TABLE ----- IT WILL SHOW HOW MANY TABLES ARE THERE IN THE DATABASE.
INSERT INTO (NAME) VALUES ( ) ------ IT WILL HELP IN THE IN INSERTING THE
VALUES INTO THE TABLE.
Select*from student --------- It will display the table.
Update student Where roll =( write the values that you want to change)
Truncate table (tablename)
Drop table (tablename)
Delete from student where roll=(=( write the values that you want to change)
Alter table student add DOB (IT WILL ADD A COLOUMN)
ALTER TABLE STUDENT DROP (DOB)
ALTER TABLE STUDENT ADD PRIMARY KEY (ROLL)
ALTER TABLE STUDENT DROP PRIMARY KEY
ALTER TABLE STUDENT ADD CONSTRAINTS*1 CHECK(ROLL>0)
ALTER TABLE STUDENT DROP CONSTRAINTS*1
RESULT
SELECT*FROM RESULT WHERE MARKS>=50 AND MARKS<=70
SELECT*FROM RESULT WHERE MARKS<40 OR MARKS>70
SELECT*-FROM RESULT WHERE MARKS BETWEEN 50 AND 70
SELECT *FROM RESULT WHERE MARKS IN(40,50,60,70)
SELECT*FROM RESULT ORDER BY MARKS
SELECT*FROM RESULT ORDER BY MARKS DESC
SELECT MAX(MARKS), MIN(MARKS), COUNT(*),AVG(MARKS)FROM RESULT
SELECT ROLL, SUM (MARKS) FROM THE RESULTGROUP BY ROLL HAVING SUM (MARKS)>120
SELECT*FROM RESULT WHERE MARKS>(SELECT MAX(MARKS FROM RESULT)
SELECT ROLL, NAME FROM STUDENT WHERE NAME LIKE ‘A%’
SELECT ROLL, NAME FROM STUDENT WHERE NAME LIKE ‘_A%’
SELECT ROLL, NAME FROM STUDENT WHERE NAME LIKE’%A%’
JOINING OF TABLES
CLICK HERE