Learn SQL With SQLite
Learn SQL With SQLite
Learn SQL With SQLite
Pgina 1
Home - - A2 Databases - - A2 Scripts - - A2 Problems - - AS Hardware - Database Tutorials - - Lazarus Tutorials - - Lazarus - - Linux - -
Top
Introduction SQL
SQL stands for Structured Query Language. It is a text based programming language that lets you create and interrogate databases using database programs such as SQLite or MySQL. If you need help with SQL syntax then the W3 schools website is very helpful - W3schools website
SQLite3
SQLite is a popular, small and fast database management system. It is public domain so you can legally use it at school or home. It will run from a memory stick. It has a command-line user interface, although the SQLite database browser (see the tutorial) offers a well-designed GUI interface. SQLite can be downloaded from as a 'precompiled binary' for Windows, Mac or Linux from the SQLite website.. Although it is public domain it is sponsored by Mozilla and Google.
Top
Preparations Download
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite You should download the program and then install it. To install it, double-click on the downloaded folder and extract the compressed files. You should then double-click on the SQLite installer program which will let you create SQLite anywhere you choose, including a memory stick. This all only takes about 2 minutes!
Pgina 2
Run
Many schools do not allow students to access the command prompt. That is not a problem - SQLite can be run simply by double-clicking on the SQLite icon. This creates a database in memory which you can then save (as you will see later in the tutorials). SQLite can also be run at the command prompt in MS-DOS. In that case, by typing SQLite3 my.db at the command prompt, you will create a completely new database called my.db or open the existing one.
Top
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite Enter the commands into SQLite prompt You can then see that the table has been created from the output below: sqlite> .read MyClass.txt sqlite> .tables Student sqlite> If there is a problem then press .q and you will exit SQLite. Amend MyClass.txt, save it and try again. Notice that if you press up and down arrow you can recycle commands. This saves a lot of work.
Pgina 3
Top
Top
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite sqlite> .read MyClass.txt sqlite> .tables Student The next SQLite command .schema shows the SQL commands that we have entered so far: sqlite> .schema CREATE TABLE Student ( StudentID integer PRIMARY KEY NOT NULL, First VARCHAR(10), Second VARCHAR(10), Form VARCHAR(4) ); Now delete the table again sqlite> DROP TABLE student; sqlite> .tables Instead of having to delete the tables every time, we will add this to the end of the MyClass.txt file so it now reads >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> CREATE TABLE Student ( StudentID integer PRIMARY KEY NOT NULL, First VARCHAR(10), Second VARCHAR(10), Form VARCHAR(4) ); DROP TABLE student; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>> Ordinarily you wouldn't want to delete what you have just created immediately but it makes sense here.
Pgina 4
Top
Learn SQL with SQLite Form VARCHAR(4) ); INSERT INTO Student(StudentID,First,Second,Form) VALUES (1,'David','Beckham','13Sc'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (2,'William','Shakespeare','13Pl'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (3,'Elizabeth','Windsor','13Pl'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (4,'Reginald','Dwight','13Sp'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (5,'Albert','Einstein','13Kr'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (6,'Margaret','Simpson','13Kr'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (7,'Michael','Mouse','13Kr'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (8,'Michael','Jagger','13Kr'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (9,'Edison','Arantes','13Sc'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (10,'Madonna','Ciccone','13Wh'); INSERT INTO Student(StudentID,First,Second, Form) VALUES (11,'Stefani','Germanotta','13Wh'); DROP TABLE Student; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> StudentID is the first column and that is the primary key of the Student table so all the values must be different in the first column. The other columns can have repeats - like 'Michael' for instance. Notice that the strings are in quotes and integers are not. Also, there is no need to specify the fields when all of them are required - this is the default situation. Save the file and then run it in SQLite to check for errors. sqlite> .read MyClass.txt sqlite> .tables sqlite>
Pgina 5
Top
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> Now save MyClass.txt, and then run it In SQLite sqlite> .read MyClass.txt 1|David|Beckham|13Sc 2|William|Shakespeare|13Pl 3|Elizabeth|Windsor|13Sp 4|Reginald|Dwight|13Sp 5|Albert|Einstein|13Kr 6|Margaret|Simpson|13Kr 7|Michael|Mouse|13Kr 8|Michael|Jagger|13Kr 9|Fred|Barrett|13Sc 10|Madonna|Ciccone|13Wh 11|Stefani|Germanotta|13Wh Top
Pgina 6
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Pgina 7
Pgina 8
StudentID First ---------- ---------1 David 2 William 3 Elizabeth StudentID First ---------- ---------3 Elizabeth 6 Margaret 9 Fred 10 Madonna StudentID First ---------- ---------1 David 2 William 3 Elizabeth 4 Reginald 5 Albert 6 Margaret 9 Fred 10 Madonna 11 Stefani The first query gives students with StudentID < 4 The second query gives all the students with first name between the two names in the alphabet (i.e. between E and M) The third query lists everyone who is not called 'Michael'. All the queries use only the two fields StudentID and First
Top
Learn SQL with SQLite DROP TABLE Student; DROP TABLE Class; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> Save the file and then check it is OK in SQLite sqlite> .read MyClass.txt sqlite> .tables sqlite> There are no tables there, because they have both been dropped. Use INSERT to add the following 5 records they could go anywhere in the file the most logical place is after the INSERT commands for the student table. Notice that when you are inserting data into all the fields that there is no need to specify them. We could have done this with Student earlier too. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> INSERT INTO Class VALUES ('13Sc','Miss','Scarlett','CL1'); INSERT INTO Class VALUES ('13Pl','Prof','Plum','CL4'); INSERT INTO Class VALUES ('13Wh','Dr','Who','TD2'); INSERT INTO Class VALUES ('13Ch','Mr','Spock','ST2'); INSERT INTO Class VALUES ('13Kr','Miss','Krabapple','SM3'); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> Now add three SELECTs to make sure all is well with the table. Then make sure both tables are deleted at the end. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> SELECT * FROM Class; SELECT * FROM Class WHERE room = 'CL4'; SELECT * FROM Class WHERE room = 'CL4' OR room = 'CL1'; DROP TABLE Student; DROP TABLE Class; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> Run the file in SQLite and you get the result of the final select as sqlite> .read MyClass.txt FormID Title Secondname Room ---------- ---------- ---------- ---------13Sc Miss Scarlett CL1 13Pl Prof Plum CL4 13Wh Dr Who TD2 13Ch Mr Spock ST2 13Kr Miss Krabapple SM3 FormID Title Secondname Room ---------- ---------- ---------- ---------13Pl Prof Plum CL4 FormID Title Secondname Room ---------- ---------- ---------- ---------13Sc Miss Scarlett CL1 13Pl Prof Plum CL4 There is a query listing all the classes
Pgina 9
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite a query listing one class a query picking out 2 classes Comment out the queries we are finished with them for the moment. Top
Pgina 10
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite Elizabeth Windsor Prof Plum Albert Einstein Miss Krabapple Margaret Simpson Miss Krabapple Michael Mouse Miss Krabapple Michael Jagger Miss Krabapple Fred Barrett Miss Scarlett Madonna Ciccone Dr Who Stefani Germanott Dr Who Which is what we want. "Hooray!!" Top
Pgina 11
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite SELECT * FROM Subject ORDER BY Title DESC; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>> Here are the results of the 4 queries from SQLite sqlite> .read MyClass.txt SubjectID SubjectTitle ---------- ---------CO Computing EN English FS Film Studi GG Geography MA Maths MT Music Tech SubjectID SubjectTitle ---------- ---------CO Computing EN English FS Film Studi GG Geography MA Maths MT Music Tech SubjectID SubjectTitle ---------- ---------------MT Music Technology MA Maths GG Geography FS Film Studies EN English CO Computing The three queries list the subjects, the subjects in name order, and the subjects in reverse name order. Top
Pgina 12
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite >>>>>> There are 2 fields. The primary key is a composite key made from the two fields. Neither field is a key by itself because each studentid can appear more than once in the student id column, and each subjectid can occur more than once in the subjectid column. However no student can be linked to a subject more than once so for instance (2,'GG') can only occur once. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('GG', 1); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('MA', 1); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('MT', 1); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('CO', 2); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('EN', 2); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('FS',2); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('EN', 3); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('GG', 3); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('MA', 3); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('CO', 4); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('FS', 4); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('MT', 4); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('CO', 5); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('EN', 5); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('FS', 5); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('GG', 6); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('MA', 6); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('EN', 7); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('FS', 7); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('CO', 8); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('FS', 8); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('MT', 8); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('CO', 9); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('GG', 9); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('MT', 9); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('CO', 10); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('EN', 10); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('GG', 10); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('MA', 10); INSERT INTO StudentSubject(SubjectID2, StudentID2) VALUES ('FS',11); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> Neither field is a key by itself because each Studentid can appear more than once in the StudentId column. Similarly each SubjectId can occur more than once in the Subjectid column. However no student can be linked to a subject more than once so for instance (2,'GG') can only occur once. Now we should delete the StudentSubject Table >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>> DROP TABLE Student; DROP TABLE Form; DROP TABLE Subject; DROP TABLE StudentSubject; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>> Check the table with a query >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>> SELECT * FROM StudentSubject; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Pgina 13
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Learn SQL with SQLite >>>>>>>> Running the script on SQLite gives . . . sqlite> .read MyClass.txt SubjectID2 StudentID2 ---------- ---------GG 1 MA 1 MT 1 CO 2 EN 2 FS 2 EN 3 GG 3 MA 3 CO 4 FS 4 MT 4 CO 5 EN 5 FS 5 GG 6 MA 6 EN 7 FS 7 CO 8 FS 8 MT 8 CO 9 GG 9 MT 9 CO 10 EN 10 GG 10 MA 10 FS 11 If we want a query to pick out student information from the student table and subject information from the subject table then THREE tables are used Student, Subject and the link between the two, StudentSubject. Compare it with the one-to-many relationship. Effectively there is a one-to-many relationship between Student and StudentSubject. There is also a one-to-many relationship between Subject and StudentSubject. That is what is modelled here. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> SELECT StudentID, First, Second, SubjectTitle FROM Student, Subject, StudentSubject WHERE StudentID = StudentID2 AND SubjectID = SubjectID2; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> Run it on SQLite sqlite> .read MyClass.txt StudentID First Second SubjectTitle ---------- ---------- ---------- -----------1 David Beckham Geography 1 David Beckham Maths 1 David Beckham Music Techno 2 William Shakespear Computing 2 William Shakespear English http://www.alevel-computing.x10.mx/TutorialSQLite.php
Pgina 14
04/04/2013 22:44:16
Learn SQL with SQLite 2 William Shakespear Film Studies 3 Elizabeth Windsor English 3 Elizabeth Windsor Geography 3 Elizabeth Windsor Maths 4 Reginald Dwight Computing 4 Reginald Dwight Film Studies 4 Reginald Dwight Music Techno 5 Albert Einstein Computing 5 Albert Einstein English 5 Albert Einstein Film Studies 6 Margaret Simpson Geography 6 Margaret Simpson Maths 7 Michael Mouse English 7 Michael Mouse Film Studies 8 Michael Jagger Computing 8 Michael Jagger Film Studies 8 Michael Jagger Music Techno 9 Fred Barrett Computing 9 Fred Barrett Geography 9 Fred Barrett Music Techno 10 Madonna Ciccone Computing 10 Madonna Ciccone English 10 Madonna Ciccone Geography 10 Madonna Ciccone Maths 11 Stefani Germanotta Film Studies A similar query but based around the subjects >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> SELECT SubjectTitle, First, Second FROM Subject, Student, StudentSubject WHERE StudentID = StudentID2 AND SubjectID = SubjectID2; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>> ,p> gives this output sqlite> .read MyClass.txt SubjectTitle First Second ------------ ---------- ----------Computing William Shakespeare Computing Reginald Dwight Computing Albert Einstein Computing Michael Jagger Computing Fred Barrett Computing Madonna Ciccone English William Shakespeare English Elizabeth Windsor English Albert Einstein English Michael Mouse English Madonna Ciccone Film Studies William Shakespeare Film Studies Reginald Dwight Film Studies Albert Einstein Film Studies Michael Mouse Film Studies Michael Jagger Film Studies Stefani Germanotta Geography David Beckham Geography Elizabeth Windsor Geography Margaret Simpson Geography Fred Barrett Geography Madonna Ciccone Maths David Beckham Maths Elizabeth Windsor Maths Margaret Simpson Maths Madonna Ciccone Music Techno David Beckham Music Techno Reginald Dwight http://www.alevel-computing.x10.mx/TutorialSQLite.php
Pgina 15
04/04/2013 22:44:16
Learn SQL with SQLite Music Techno Michael Jagger Music Techno Fred Barrett or a small modification will let you find who studies Computing . . . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>> SELECT SubjectTitle, First, Second FROM Subject, Student, StudentSubject WHERE StudentID = StudentID2 AND SubjectID = SubjectID2 AND SubjectID = 'CO'; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>> and prove it using SQLite sqlite> .read MyClass.txt SubjectTitle First Second ------------ ---------- ----------Computing William Shakespeare Computing Reginald Dwight Computing Albert Einstein Computing Michael Jagger Computing Fred Barrett Computing Madonna Ciccone
Pgina 16
Foreign Keys
Although the foreign key constraint in not working in this version on SQLite but ideally you should include the foreign key constraints. StudentID2 is a foreign key in StudentSubject (because it is not the primary key of that table but it is a primary key of Student) SubjectID2 is a foreign key in StudentSubject (because it is not the primary key of that table but it is a primary key of Subject) These should be added to the definition of StudentSubject to give CREATE TABLE StudentSubject ( SubjectID VARCHAR(4) NOT NULL, StudentID integer NOT NULL, PRIMARY KEY(SubjectID, StudentID), FOREIGN KEY (StudentID) REFERENCES Student(StudentID), FOREIGN KEY (SubjectID) REFERENCES Subject(SubjectID) ) Top
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Pgina 17
sqlite> .read MyClass.txt COUNT(*) ---------11 and confirms that there are 11 students. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>> SELECT COUNT(Form) FROM Student; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>> which also gives sqlite> .read MyClass.txt COUNT(Form) ---------11 and confirms that there are 11 students.
Learn SQL with SQLite 13Sp 1 13Wh 2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > SELECT SubjectTitle, COUNT(SubjectID) FROM Subject, StudentSubject WHERE SubjectID = SubjectID2 GROUP BY SubjectID; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > sqlite> .read MyClass.txt Form COUNT(Form) ---------- ----------13Kr 4 13Pl 2 13Sc 2 13Sp 1 13Wh 2 How good is that ? Pretty good! Top
Pgina 18
UPDATE Subject SET ExamBoard = 'AQA' WHERE SubjectTitle = 'Computing'; This will make the ExamBoard column AQA in the Computing row.
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Pgina 19
sqlite> UPDATE Subject SET ExamBoard = 'AQA' WHERE SubjectTitle = 'Computing'; sqlite> SELECT * FROM Subject; SubjectID SubjectTitle ExamBoard ---------- ------------ ---------CO Computing AQA EN English FS Film Studies GG Geography MA Maths MT Music Techno
DELETE FROM Student WHERE first = 'Reginald' AND second = 'Dwight'; SELECT * FROM Student
sqlite> DELETE FROM Student WHERE first = 'Reginald' AND second = 'Dwight'; sqlite> SELECT * FROM Student; StudentID First Second Form ---------- ---------- ---------- ---------1 David Beckham 13Sc 2 William Shakespear 13Pl 3 Elizabeth Windsor 13Pl 5 Albert Einstein 13Kr 6 Margaret Simpson 13Kr 7 Michael Mouse 13Kr 8 Michael Jagger 13Kr 9 Fred Barrett 13Sc 10 Madonna Ciccone 13Wh 11 Stefani Germanotta 13Wh
The ExamBoard column is added. The entry for Computing is AQA. Reginald Dwight is missing (StudentID=4). Where has Reg gone? To become a star . . .
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Pgina 20
Top
Save as a database
Saving the tables as a database file Before you try to save the tables as a database REMOVE the SQLite references from MyClass.txt Make sure the four DROP TABLE commands are commented out. SAVE the file with a DIFFERENT name
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> --.headers ON -- This gives the columns a title --.mode column -- this outputs the tables in aligned columns >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
then
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> --DROP TABLE Student; --DROP TABLE Form; --DROP TABLE Subject; --DROP TABLE StudentSubject; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
First save the text file as MyClassDB.txt. This file is still a file of SQL commands. So we will use the SQLite backup function and create a database file called MyClassDB.db Enter sqlite3 and enter
.backup MyClass.db
Check that you have this file in the folder The database can later be reopened with
.restore MyClassDB.txt
You should look at the database file (Myclass.db not MyClass.txt in a text editor like Notepad. Yuo will see the contents along with lots of 'weird' characters. Spooky.
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Pgina 21
Top
Foreign Key
Foreign Key When you have a one-to-many relationship between two tables, you can design a SELECT query to extract information from both tables by using the common field. For instance with the Student and Class tables, the common field was the Form column in the Student table and the ClassID column of the Class column. To get a query using both tables you use the common field like this:
SELECT First, Second, Title, Secondname FROM Student, Class WHERE Form=ClassID; This gives a satisfactory answer. However, you can enter new students and give them a form that doesn't exist in the Class table. This is not desirable. We might also try to delete a teacher the Form column - which ideally should not be possible. To prevent both of these, use the foreign key constraint. Form is known as a 'foreign' key within the Student table because it is the primary key of a different (foreign) table. The field is indicated as a foreign key within the Student table with
CREATE TABLE Student ( StudentID integer PRIMARY KEY NOT NULL, First VARCHAR(10), Second VARCHAR(10), Form VARCHAR(4), FOREIGN KEY (Form) REFERENCES Class(ClassID) );
sqlite> Insert INTO Student Values(12,'Fred','Jones','13Zz'); sqlite> select * from Student; StudentID First Second Form ---------- ---------- ---------- ---------1 David Beckham 13Sc 2 William Shakespear 13Pl 3 Elizabeth Windsor 13Pl 4 Reginald Dwight 13Sp 5 Albert Einstein 13Kr
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Pgina 22
6 Margaret Simpson 13Kr 7 Michael Mouse 13Kr 8 Michael Jagger 13Kr 9 Fred Barrett 13Sc 10 Madonna Ciccone 13Wh 11 Stefani Germanotta 13Wh 12 Fred Jones 13Zz
This should not be possible!! Equally we should not be able to remove a teacher sqlite> Delete from Class where Secondname='Krabapple'; sqlite> select * from Class; CLassID Title Secondname Room ---------- ---------- ---------- ---------13Sc Miss Scarlett CL1 13Pl Prof Plum CL4 13Wh Dr Who TD2 13Ch Mr Spock ST2
That is correct - the version of SQLite that we are using does not support Foreign key constraints. Nonetheless, you know what should happen. Here is a nice explanation: SQLite Foreign Key The database management system called MySQL does support foreign keys. Top
Improvements
Normalisation This database is not quite normalised. The Class table should be split into two tables Class and Staff. This is because (for instance) Title and Secondname are not strictly dependent on ClassID. This would be a better setup -
CREATE TABLE Class ( FormID VARCHAR(4) PRIMARY KEY NOT NULL, Room VARCHAR(4), StaffMember VARCHAR(2), FOREIGN KEY (StaffMember) REFERENCES Staff(StaffID) );
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Pgina 23
CREATE TABLE Staff ( StaffID VARCHAR(2) PRIMARY KEY NOT NULL, Title VARCHAR(10), Second VARCHAR(10) );
Top
Top
Full SQL
CREATE TABLE Student ( StudentID integer PRIMARY KEY NOT NULL, First VARCHAR(10), Second VARCHAR(10), Form VARCHAR(4) ); INSERT INTO Student(StudentID,First,Second,Form) VALUES
http://www.alevel-computing.x10.mx/TutorialSQLite.php 04/04/2013 22:44:16
Pgina 24
(1,'David','Beckham','13Sc'); INSERT INTO Student(StudentID,First,Second, (2,'William','Shakespeare','13Pl'); INSERT INTO Student(StudentID,First,Second, (3,'Elizabeth','Windsor','13Pl'); INSERT INTO Student(StudentID,First,Second, (4,'Reginald','Dwight','13Sp'); INSERT INTO Student(StudentID,First,Second, (5,'Albert','Einstein','13Kr'); INSERT INTO Student(StudentID,First,Second, (6,'Margaret','Simpson','13Kr'); INSERT INTO Student(StudentID,First,Second, (7,'Michael','Mouse','13Kr'); INSERT INTO Student(StudentID,First,Second, (8,'Michael','Jagger','13Kr'); INSERT INTO Student(StudentID,First,Second, (9,'Edison','Arantes','13Sc'); INSERT INTO Student(StudentID,First,Second, (10,'Madonna','Ciccone','13Wh'); INSERT INTO Student(StudentID,First,Second, (11,'Stefani','Germanotta','13Wh');
Form) VALUES Form) VALUES Form) VALUES Form) VALUES Form) VALUES Form) VALUES Form) VALUES Form) VALUES Form) VALUES Form) VALUES
CREATE TABLE Class ( ClassID VARCHAR(4) PRIMARY KEY NOT NULL, Title VARCHAR(10), Secondname VARCHAR(10), Room VARCHAR(4) ); INSERT INTO Class VALUES ('13Sc','Miss','Scarlett','CL1'); INSERT INTO Class VALUES ('13Pl','Prof','Plum','CL4'); INSERT INTO Class VALUES ('13Wh','Dr','Who','TD2'); INSERT INTO Class VALUES ('13Ch','Mr','Spock','ST2'); INSERT INTO Class VALUES ('13Kr','Miss','Krabapple','SM3'); CREATE TABLE Subject ( SubjectID VARCHAR(4) NOT NULL, SubjectTitle VARCHAR(10), PRIMARY KEY(SubjectID) ); INSERT INTO Subject(SubjectID, SubjectTitle) VALUES ('CO', 'Computing');
http://www.alevel-computing.x10.mx/TutorialSQLite.php 04/04/2013 22:44:16
Pgina 25
INSERT INTO Subject(SubjectID, ('EN', 'English'); INSERT INTO Subject(SubjectID, ('FS', 'Film Studies'); INSERT INTO Subject(SubjectID, ('GG', 'Geography'); INSERT INTO Subject(SubjectID, ('MA', 'Maths'); INSERT INTO Subject(SubjectID, ('MT', 'Music Technology');
SubjectTitle) VALUES SubjectTitle) VALUES SubjectTitle) VALUES SubjectTitle) VALUES SubjectTitle) VALUES
CREATE TABLE StudentSubject ( SubjectID2 VARCHAR(4) NOT NULL, StudentID2 integer NOT NULL, PRIMARY KEY(SubjectID2, StudentID2) ); INSERT INTO ('GG', 1); INSERT INTO ('MA', 1); INSERT INTO ('MT', 1); INSERT INTO ('CO', 2); INSERT INTO ('EN', 2); INSERT INTO ('FS', 2); INSERT INTO ('EN', 3); INSERT INTO ('GG', 3); INSERT INTO ('MA', 3); INSERT INTO ('CO', 4); INSERT INTO ('FS', 4); INSERT INTO ('MT', 4); INSERT INTO ('CO', 5); INSERT INTO ('EN', 5); INSERT INTO ('FS', 5); INSERT INTO ('GG', 6); StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16
Pgina 26
INSERT INTO ('MA', 6); INSERT INTO ('EN', 7); INSERT INTO ('FS', 7); INSERT INTO ('CO', 8); INSERT INTO ('FS', 8); INSERT INTO ('MT', 8); INSERT INTO ('CO', 9); INSERT INTO ('GG', 9); INSERT INTO ('MT', 9); INSERT INTO ('CO', 10); INSERT INTO ('EN', 10); INSERT INTO ('GG', 10); INSERT INTO ('MA', 10); INSERT INTO ('FS',11);
StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES StudentSubject(SubjectID2, StudentID2) VALUES
http://www.alevel-computing.x10.mx/TutorialSQLite.php
04/04/2013 22:44:16