MySQL/scripts
From Christoph's Personal Wiki
Revision as of 07:52, 25 August 2007 by Christoph (Talk | contribs) (MySQL/Scripts moved to MySQL/scripts)
- Find duplicate entries
SELECT accession,count(*) AS n FROM mydb WHERE kingdom!='Bacteria' GROUP BY accession HAVING n>2;
- Padding integers
Say you have a series of integers for your id
column in a table and you wish to pad them with zeros if they are less than 1000 (i.e., "1
" -> "001
"), then:
SELECT RIGHT(1000 + id,3) AS id FROM mydb;
Relational algebra (RA)=
Note: Taken from the MIT course in Database Management Systems.
Consider the following relations and query:
student(_sname_,major,year) class(_cnum_,building) enrolled(_sname_,_cnum_,_time_)
QUESTION: "The majors of students who take at least one class in the physics building"
which could be written in SQL as:
SELECT DISTINCT major FROM student S,class C,enrolled E WHERE S.sname = E.sname AND E.cnum = C.cnum AND C.building = 'physics'
The RA could be written as:
PI_{major}( SIGMA_{building='physics'}((student JOIN enrolled) JOIN class) (where _{x} means subscripted x)
HW: Write three relational algebra trees showing different ways of evaluating this query (you do not need to annotate your diagram with choices of implementation for each operation, i.e. just draw the trees, not full evaluation plans).