Download as pdf
Download as pdf
You are on page 1of 8
Database: B Adatabase is a collection ofinterrelated data It's acollec that isorganised sa that ican be easily access A schools database Advantages of Database 2 ORACLE } MySQL gGiserver @mongoDB rooctsa Relational Data Model Inrelational data models, datais organised in tablest These tables are-called relation Shrcea Terminologies 1. Relation: Atablestoring logically related data 2. Bom Pool of values from which the actual values appearing in agivencolumn are drawn, 3. Twple: row 4A. Attribute: Column 5, Degree: —no.of rowsituples. & Cardinality: n0.of eolumns/attributes, Terminologies View jrtual table that doesn't really exist, but it's used to view the base table or tables derived from one or more base tables Primary: Qneormare attributes that can uniquely identify tuples within key the table, 3. Candidate: Allattribute combinationsinside a table that are candidates to key become primary key. Alternate: Acandidate key which isnot primary key. key Forelgn: — Ancn-key attribute whose walues are derived from the primary key key of somecther table, called foreign key in its current table. Myst: SL server ‘onguoge) isa database language that is recognised by nearly Jotional DateBese Management Systen smmands that enables us tocreate and operate on relational DLs fast. very hish level platform indegendent, standardized portable language which requires almost na coating skis DDL Commands DDL or Data Definition Language Itcontains set of commands that allows us to perf ksrelated todata definition, especie Structure and access methods for database syster pou: ‘+ Creating, altering & dropping. ‘+ Granting. revoking privileges androles. ‘= Maintenance comand DML Commands DML or Data Manipulation Language eon 3 camwnanls that alles DML consists of ‘= Insertion = Deletion = Modification = Retrieval TCL Commands ‘TCLor Transaction Control Language treo ws us Lo manage work iwalving ToL consictsof + Making changes to database, permanent, + Undoing changes ta database, permanent, + Creatingsevepoints, + Setting properties for current trancaetions. "dL and DE are beyond cops of silat, wel tater sete Important Data Types in MySQL Date Type Description [Date Type Dereription cHak string 285) DOUBLE | ecmali wo digits) ‘vaRCHAR string 255) DECIMAL | "DOUBLE stowed as sting Tiwi | inener-128%0 127) Are vere oo SUUALLINT | ineger-8376800.39767) | DATETIME | Y¥¥AMW.00 HMMS nt integer-2t02"-4) [TIMESTAMP | _YvYYMReDOHMMS SIGINT ingest 2t0 22) TIME roses FLomT | Decimatprecise ta. 23 it) Varchar ‘The differenca hetween a char an varchar is of fied length and variable length, ‘When acolumm is given datatype char{n), then walues under this attribute are strings of mbytes. Ifa values less tharin bytes, then empty spaces are arlded tll nth byte In varchar(n}, values are still strings of n bytes. No spaces are added even if size is ‘smaller than n bytes. ‘Hey’ chart5)——_varchart5) Hey" Hey Varehar Varchar2 Itean identity NULLandempty NULL and empty string are same. © It’s anexternal data type. © Minimumsize= 1 Maximum size - 2000 Maximum size 4000 Creating table CREATE TABLE { «data type>. .. CREATE TABLE student {mame chart20), rollin int, mars int Inserting data into table INSERT INTO (, , .}; INSERT INTO student WALUES (Harsh; 141.97); INSERT INTO studentiName, marks) WALUES( Raghav, 99); INSERT INTO student WALUES Hemant’, 146, NULL): Using DATE (CREATE TABLE ( date, CREATE TABLE transac{Amount int, Date date} INSERT INTO transac WALUES 110000, "2021-17-02; INSERT INTO transac{Amount Date} = WALUES (-3000, "2021-19-01 2021-19-91 INSERT INTO transac WALUES (2, 2021-24-01); 2021-24-01 ‘CREATETABLE ctucent Anka; «(Name cartaorwornuee [= [| Making Queries. strain. rage [aw [ow ema [oe [owt The SELECT command is used to pull information from atable. SELECT what to select FROM which table Name WHERE conditions. to. satisfy; 7 SELECT Name FROM student Raghav Making Queries SELECT Name.roting SELECT Name FROM student FROM student where marks>95 ore Name awe Ragiae ash ALL, DISTINCT, BETWEEN SELECTALLName SELECT DISTINCT ma FROM student. FROM student ranks Null, ‘ORDER BY Rach SELECTName.roling SELECTName,ralino SELECT * FROM student FROM student FROMstudent WHERE marks IS null WHERE marks IS notrull ORDER B¥ marks ASC Name [ore Name we Hara Raghav z GROUP BY, count, c= ao DESCRIBE, DISTINCT = = SaLect mana coun) SELECTDISTINCT mara ae” Soo racer See ns [ea = ef oe fe ae 7 > afi 7 i IN, HAVING SELECT rellno, marks FROM student WHERE Name IN (Harsh, "Suraj Khushi) LIKE SELECT name FROMstudent WHERE name LUKE "Hi Tis ua rane ale, ole ih fer, @ SELECT marks, count{marks) FROM student GROUP BY marks HAVING countimarks)> 1. = SELECT name FROM studer nt WHERE name LUKE" mse? SELECT name SELECT name FROM student FROMSstudent WHERE name LIKE"St” WHERE name: ORname LIKE gh LIKE Soa. si! cc = SQL functions SELECT SUM(mark FROM student nome 23-25 / 32 rere [oa [oo ELECT AVG(marks) SELECT productid, maxiquantity), FROM st ident urineeks) 203 =e) ‘GROUP BY productid having avglquantity)>»100 SQL Joins ‘An SQ. Join is a query that fetches data from two or more tables whase records are joined with one anoth SELECT FROM clable1>,,ctabled>, ‘WHERE «join condition of tables» “ ‘SELECT Name, marks, AdmNo FROM student, ist WHERE student rame=lictname This will join tables student and list on the basis of condition student nome=list.nome ased on acondition. Types of Joins eo ‘Cartesian Product/Cross Join: Without any condition, returns all joined ecard Equi Join Joins two ar more tables based ona condition using equality operator, Natural Join ‘Attype of equijoin where the join condition compares all the sarve names olumasin bath tal Primary key Aprimary key isused to identify each row inthe table. It canbe either one attribute or anartifii ember) ‘CREATE TABLE student Name (Name char(20) Roling int PRIMARY KEY basil Marks int Raghav INSERT INTO student VALUES('Bhanu!, 141, 33) Laine Composite key ‘A-compasite key or primary key composition more columns in.atable thst can be used to uniquely identify each rawin the table, ‘CREATE TABLE student Name [Glass | RallNo (Name chart20} are Harsh [9 80 Marks int Raghav [10 80 PRIMARY KEY{Class, Marks ) Hemant [10 a7 FOREIGN key CREATE TABLE subject marks ( rollnoint, Maths int, Science int, Hindi int, English int, FOREIGN KEY (rolino) REFERENCES student(rollna) ); Name rolino marks Harsh 144 Raghav 143 Hemant 146 Hindi 100 97 ALTER TABLE CREATE TABLE student ( Name char(20) Rollna int Marks int ); ALTER TABLE student ADD PRIMARY KEY (Rollno); ADD class int DROP COLUMN Name MODIFY COLUMN class char(5) marks | class

You might also like