Learn SQL With SQLite

Embed Size (px)

Citation preview

  • 7/28/2019 Learn SQL With SQLite

    1/26

    Pgina 1arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    Home - - A2 Databases - - A2 Scripts - - A2 Problems - - AS Hardware - -Database Tutorials - - Lazarus Tutorials - - Lazarus - - Linux - -

    Learn SQL with SQLite

    Version 1

    Contents

    IntroductionPreparationsCREATE TABLEDROP TABLEAdding FieldsINSERT INTO ... VALUES ...SELECT ... FROM ... WHEREHarder QueriesSecond TableOne-to-many relationshipThird TableMany-to-many relationshipCOUNT FunctionUPDATE, DELETE and ALTER TABLESave as a databaseForeign KeyImprovementsSQLite Database BrowserFull SQL

    Top

    Introduction

    SQL

    SQL stands for Structured Query Language. It is a text based programminglanguage that lets you create and interrogate databases using databaseprograms such as SQLite or MySQL. If you need help with SQL syntax then theW3 schools website is very helpful - W3schools website

    SQLite3

    SQLite is a popular, small and fast database management system. It is publicdomain so you can legally use it at school or home. It will run from a memorystick. It has a command-line user interface, although the SQLite databasebrowser (see the tutorial) offers a well-designed GUI interface.

    SQLite can be downloaded from as a 'precompiled binary' for Windows, Mac orLinux from the SQLite website.. Although it is public domain it is sponsored byMozilla and Google.

    Top

    Preparations

    Download

  • 7/28/2019 Learn SQL With SQLite

    2/26

    Pgina 2arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    You should download the program and then install it. To install it, double-clickon the downloaded folder and extract the compressed files. You should thendouble-click on the SQLite installer program which will let you create SQLiteanywhere you choose, including a memory stick. This all only takes about 2minutes!

    Run

    Many schools do not allow students to access the command prompt. That is nota 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 inthe tutorials). SQLite can also be run at the command prompt in MS-DOS. Inthat case, by typing

    SQLite3 my.db

    at the command prompt, you will create a completely new database calledmy.db or open the existing one.

    Top

    CREATE TABLE

    CREATE TABLE

    Type the following into the text file called MyClass.txt that you have justcreated.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> CREATE TABLE Student (StudentID integer PRIMARY KEY NOT NULL,);>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    This is a perfectly valid SQL command. However it is easier to read if it iswritten in a less compressed way. Write it like below using the Enter key andtab key.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> CREATE TABLE Student ( StudentID integer PRIMARY KEY NOT NULL, );>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    All SQLCommands in SQLite end in a semi-colon (;). That is not necessarily truein other SQL programs like MySQL.

    This particular SQL command will create a table called Student which has onefield, StudentID, which will be the primary key. It will store integers. If this fieldis blank (null) then you can not save the record that is what NOT NULL means.

    Loading the table

    Save MyClass.txt Open SQLite by either clicking on the SQLite window or typingSQLite3 at the DOS prompt. You should see the output below:

    $ sqlite3 SQLite version 3.6.10Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite>

    Look at the next two SQLite commands written below. They begin with a full-stop. This means that they are SQLite commands, not SQL. The first commandimports the file you have just written into SQLite and runs the command foryou. Of course, creating a file has no output. To show that it really has worked,the second command shows you all the tables that have been created.

    .read MyClass.txt

    .tables

  • 7/28/2019 Learn SQL With SQLite

    3/26

    Pgina 3arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    Enter the commands into SQLite prompt You can then see that the table hasbeen created from the output below:

    sqlite> .read MyClass.txtsqlite> .tablesStudentsqlite>

    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 canrecycle commands. This saves a lot of work.

    Top

    DROP TABLE

    DROP TABLE

    In the SQLite window, press the up arrow until you get the command

    .read MyClass.txt

    and press enter

    sqlite> .read MyClass.txtSQL error near line 1: table Student already exists

    This error occurs because you are trying to create a table that already exists.Therefore we will first delete the student table by typingDROP TABLE student;.tables

    to confirm that it really has gone.

    sqlite> DROP TABLE student;sqlite> .tablessqlite>

    No tables are there, so now we can reload the file and recreate the tableswithout errors.

    Top

    Adding fields

    More fields

    Open MyClass.txt and add three more fields to the table. Each student has afirst name, second name and the form group (form) that they are registered inat the start of the day.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CREATE TABLE Student(StudentID integer PRIMARY KEY NOT NULL,First VARCHAR(10),Second VARCHAR(10),Form VARCHAR(4));>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    >>>>

    VARCHAR is string or text. Make sure that you recycle commands. Go intoSQLite and check that the table can be created

  • 7/28/2019 Learn SQL With SQLite

    4/26

    Pgina 4arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    sqlite> .read MyClass.txtsqlite> .tablesStudent

    The next SQLite command .schema shows the SQL commands that we haveentered so far:

    sqlite> .schemaCREATE 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 ofthe 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.

    SQL instruction or SQLite command?

    SQL is a computer language implemented by various programs like SQLite,MySQL and MS Access. It is the same relationship as the Pascal language andPascal compilers.

    Commands starting with a dot like .read and .tables and .schema are SQLitecommands. If you used a different database program like MySQL they would notbe available. (although similar ones would be).

    Instructions that end in a semi colon like CREATE TABLE, DROP TABLE, INSERTINTO are part of the SQL language. Its syntax is fairly standard everywhere(although not all database programs insist on semi-colons!).

    Top

    INSERT INTO ... VALUES

    INSERT INTO . . . VALUES

    In the MyClass.txt file we will add some data into the Student table by using theINSERT INTO . . . VALUES command from SQL. Use Copy and Paste!

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CREATE TABLE Student(StudentID integer PRIMARY KEY NOT NULL,First VARCHAR(10),Second VARCHAR(10),

  • 7/28/2019 Learn SQL With SQLite

    5/26

    Pgina 5arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    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 canhave repeats - like 'Michael' for instance.

    Notice that the strings are in quotes and integers are not. Also, there is no needto 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.txtsqlite> .tablessqlite>

    Top

    SELECT ... FROM ... WHERE

    SELECT . . . FROM . . . WHERE . . .

    So far we have created a table called student and populated it with student databut not been able to see the data. Running queries on a database is done inSQL using the SELECT . . . FROM command.

    Add this to near the end of your MyClass.txt file -

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT * FROM Student;

    DROP TABLE Student;

  • 7/28/2019 Learn SQL With SQLite

    6/26

    Pgina 6arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Now save MyClass.txt, and then run it In SQLite

    sqlite> .read MyClass.txt

    1|David|Beckham|13Sc2|William|Shakespeare|13Pl3|Elizabeth|Windsor|13Sp4|Reginald|Dwight|13Sp5|Albert|Einstein|13Kr6|Margaret|Simpson|13Kr7|Michael|Mouse|13Kr8|Michael|Jagger|13Kr9|Fred|Barrett|13Sc10|Madonna|Ciccone|13Wh11|Stefani|Germanotta|13Wh

    Top

    Harder Queries

    .header and .mode column

    We can tidy the output up by using two new SQLite commands at the top ofMyClass.txt. You can also see that comments are made using two hyphens .

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.headers ON-- This command gives the columns a title.mode column-- this outputs the tables in aligned columns

    CREATE TABLE Student(StudentID integer PRIMARY KEY NOT NULL,

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Run this in SQLite to get

    StudentID First Second Form---------- ---------- ---------- ----------1 David Beckham 13Sc2 William Shakespear 13Pl3 Elizabeth Windsor 13Pl4 Reginald Dwight 13Sp5 Albert Einstein 13Kr6 Margaret Simpson 13Kr

    7 Michael Mouse 13Kr8 Michael Jagger 13Kr9 Fred Barrett 13Sc10 Madonna Ciccone 13Wh11 Stefani Germanotta 13Wh

    Entering SQL directly in the SQLite window

    You can enter Selects directly in the SQLite window.

    You will need to comment out the DROP tables command in MyClass.txt firstthough, otherwise the table will be deleted before we query it. You will also need

    to run

    DROP TABLE Student;before you reload MyClass.txt.

    I will stay with running queries from within the script.

  • 7/28/2019 Learn SQL With SQLite

    7/26

    Pgina 7arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    SELECT . . . FROM . . . WHERE

    Comment out the Select * FROM Student because we don't wan to run thatevery time

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>--SELECT * FROM Student>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    >

    Then add another two queries

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT * FROM Student WHERE first = 'Madonna';

    SELECT First, Second FROM Student WHERE first LIKE 'M%';>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    You get the output below.

    sqlite> .read MyClass.txtStudentID First Second Form---------- ---------- ---------- ----------10 Madonna Ciccone 13Wh

    First Second---------- ----------Margaret SimpsonMichael MouseMichael JaggerMadonna Ciccone

    The first SELECT picks out a particular student.

    The second SELECT shows just the first and second fields of students whose firstname begins with the letter M. The % symbol is the wildcard.

    SELECT . . . FROM . . .WHERE . . . (more possibilities)

    Comment out all the previous selects and add three more

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    >>>>--SELECT * FROM Student;

    --SELECT * FROM Student WHERE first = 'Madonna';

    --SELECT first,second FROM Student WHERE first LIKE 'M%';

    SELECT StudentID, first FROM Student WHERE studentID >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    This gives

  • 7/28/2019 Learn SQL With SQLite

    8/26

    Pgina 8arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    StudentID First---------- ----------1 David2 William3 Elizabeth

    StudentID First---------- ----------3 Elizabeth6 Margaret9 Fred10 Madonna

    StudentID First---------- ----------1 David2 William3 Elizabeth4 Reginald5 Albert6 Margaret9 Fred

    10 Madonna11 Stefani

    The first query gives students with StudentID < 4The second query gives all the students with first name between the two namesin 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

    Second Table

    Second Table

    Now we will add a second table called Class which gives the details of the Classthat each student is in when they are registered. It has a primary key and 3fields to do with the class.

    First comment out ALL queries using .and then add the table definition.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CREATE TABLE Student(

    StudentID integer PRIMARY KEY NOT NULL,First VARCHAR(10),Second VARCHAR(10),Form VARCHAR(4));

    CREATE TABLE Class(ClassID VARCHAR(4) PRIMARY KEY NOT NULL,Title VARCHAR(10),Secondname VARCHAR(10),Room VARCHAR(4));

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    remember to drop the table at the end.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

  • 7/28/2019 Learn SQL With SQLite

    9/26

    Pgina 9arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    DROP TABLE Student;DROP TABLE Class;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Save the file and then check it is OK in SQLite

    -sqlite> .read MyClass.txtsqlite> .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 needto 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 sureboth 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 CL113Pl Prof Plum CL413Wh Dr Who TD213Ch Mr Spock ST213Kr Miss Krabapple SM3

    FormID Title Secondname Room---------- ---------- ---------- ----------13Pl Prof Plum CL4

    FormID Title Secondname Room---------- ---------- ---------- ----------13Sc Miss Scarlett CL113Pl Prof Plum CL4

    There is

    a query listing all the classes

  • 7/28/2019 Learn SQL With SQLite

    10/26

    Pgina 10arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    a query listing one classa query picking out 2 classes

    Comment out the queries we are finished with them for the moment.

    Top

    One-to-many relationship

    One to many relationship

    The next task is to link the students to the class that they are in.

    Each student is a member of one class. Each class is composed ofmany students. The relationship is therefore one-to-many.

    The link centres around the

    Form field of the Student table and the ClassID field of the Class table

    It is possible to pick out related data from both tables with a query...

    A suitable query might be to get the student names with their teacher namemight be:

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT First, Second, Form, Title, SecondnameFROM Student, Class;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Unfortunately this doesn't work

    sqlite> .read MyClass.txtFirst Second Title Secondname---------- ---------- ---------- ----------David Beckham Miss ScarlettDavid Beckham Prof PlumDavid Beckham Dr WhoDavid Beckham Mr SpockDavid Beckham Miss KrabappleWilliam Shakespear Miss ScarlettWilliam Shakespear Prof PlumWilliam Shakespear Dr WhoWilliam Shakespear Mr SpockWilliam Shakespear Miss Krabapple

    You can see that all students are linked to all classes.

    Instead you need to use the related field

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT First, Second, Form, Title, SecondnameFROM Student, Class;WHERE Form = ClassID>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    This links the two related fields mentioned ealier.

    sqlite> .read MyClass.txtFirst Second Title Secondname---------- ---------- ---------- ----------David Beckham Miss ScarlettWilliam Shakespear Prof Plum

  • 7/28/2019 Learn SQL With SQLite

    11/26

    Pgina 11arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    Elizabeth Windsor Prof PlumAlbert Einstein Miss KrabappleMargaret Simpson Miss KrabappleMichael Mouse Miss KrabappleMichael Jagger Miss KrabappleFred Barrett Miss ScarlettMadonna Ciccone Dr WhoStefani Germanott Dr Who

    Which is what we want.

    "Hooray!!"

    Top

    Third Table

    Third Table

    The students will study a variety of subjects. We will create the table ofsubjects. Notice the alternative way of defining the Primary key.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CREATE TABLE Subject(SubjectID VARCHAR(4) NOT NULL,SubjectTitle VARCHAR(10),PRIMARY KEY(SubjectID));>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Add the Values

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>INSERT INTO Subject(SubjectID, SubjectTitle) VALUES ('CO', 'Computing');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES ('EN', 'English');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES ('FS', 'Film Studies');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES ('GG', 'Geography');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES ('MA', 'Maths');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES ('MT', 'Music Technology');>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Don't forget to delete the table at the end

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>DROP TABLE Student;DROP TABLE Form;DROP TABLE Subject;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Using SELECT . . . FROM . . . ORDER BY . . .

    Add the three Selects and check them

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT * FROM Subject;SELECT * FROM Subject ORDER BY SubjectID;

  • 7/28/2019 Learn SQL With SQLite

    12/26

    Pgina 12arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    SELECT * FROM Subject ORDER BY Title DESC;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>-

    Here are the results of the 4 queries from SQLite

    sqlite> .read MyClass.txtSubjectID SubjectTitle---------- ----------

    CO ComputingEN EnglishFS Film StudiGG GeographyMA MathsMT Music Tech

    SubjectID SubjectTitle---------- ----------CO ComputingEN EnglishFS Film StudiGG GeographyMA MathsMT Music Tech

    SubjectID SubjectTitle---------- ----------------MT Music TechnologyMA MathsGG GeographyFS Film StudiesEN EnglishCO Computing

    The three queries list the subjects, the subjects in name order, and the subjects

    in reverse name order.

    Top

    Many to many relationship

    Many to many relationship

    The next task is to link the subjects to the students.

    Each subject is studied by many students. Each student studies manysubjects. The relationship is therefore many-to-many.

    Modelling a many-to-many relationship involves linking the two tables using a

    new table called a link table.

    Composite primary key

    The tables to be linked are Student and Subject.To create a link table between the two tables, create a new table with just twofields - the primary key from each table.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CREATE TABLE StudentSubject(SubjectID2 VARCHAR(4) NOT NULL,StudentID2 integer NOT NULL,PRIMARY KEY(SubjectID2, StudentID2));>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

  • 7/28/2019 Learn SQL With SQLite

    13/26

    Pgina 13arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    >>>>>>

    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 thanonce in the student id column, and each subjectid can occur more than once inthe subjectid column. However no student can be linked to a subject more thanonce 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 thanonce in the StudentId column. Similarly each SubjectId can occur more thanonce in the Subjectid column. However no student can be linked to a subjectmore 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;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

  • 7/28/2019 Learn SQL With SQLite

    14/26

    Pgina 14arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    >>>>>>>>

    Running the script on SQLite gives . . .

    sqlite> .read MyClass.txtSubjectID2 StudentID2---------- ----------GG 1MA 1

    MT 1CO 2EN 2FS 2EN 3GG 3MA 3CO 4FS 4MT 4CO 5EN 5FS 5GG 6MA 6EN 7FS 7CO 8FS 8MT 8CO 9GG 9MT 9CO 10EN 10GG 10MA 10

    FS 11

    If we want a query to pick out student information from the student table andsubject 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 andStudentSubject.There is also a one-to-many relationship between Subject and StudentSubject.That is what is modelled here.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT StudentID, First, Second, SubjectTitleFROM Student, Subject, StudentSubjectWHERE StudentID = StudentID2AND SubjectID = SubjectID2;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Run it on SQLite

    sqlite> .read MyClass.txt

    StudentID First Second SubjectTitle---------- ---------- ---------- ------------1 David Beckham Geography1 David Beckham Maths1 David Beckham Music Techno2 William Shakespear Computing2 William Shakespear English

  • 7/28/2019 Learn SQL With SQLite

    15/26

    Pgina 15arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    2 William Shakespear Film Studies3 Elizabeth Windsor English3 Elizabeth Windsor Geography3 Elizabeth Windsor Maths4 Reginald Dwight Computing4 Reginald Dwight Film Studies4 Reginald Dwight Music Techno5 Albert Einstein Computing5 Albert Einstein English5 Albert Einstein Film Studies6 Margaret Simpson Geography6 Margaret Simpson Maths7 Michael Mouse English7 Michael Mouse Film Studies8 Michael Jagger Computing8 Michael Jagger Film Studies8 Michael Jagger Music Techno9 Fred Barrett Computing9 Fred Barrett Geography9 Fred Barrett Music Techno10 Madonna Ciccone Computing10 Madonna Ciccone English10 Madonna Ciccone Geography10 Madonna Ciccone Maths

    11 Stefani Germanotta Film Studies

    A similar query but based around the subjects -

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT SubjectTitle, First, SecondFROM Subject, Student, StudentSubjectWHERE StudentID = StudentID2AND SubjectID = SubjectID2;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    ,p> gives this output

    sqlite> .read MyClass.txtSubjectTitle First Second------------ ---------- -----------Computing William ShakespeareComputing Reginald DwightComputing Albert EinsteinComputing Michael JaggerComputing Fred BarrettComputing Madonna CicconeEnglish William Shakespeare

    English Elizabeth WindsorEnglish Albert EinsteinEnglish Michael MouseEnglish Madonna CicconeFilm Studies William ShakespeareFilm Studies Reginald DwightFilm Studies Albert EinsteinFilm Studies Michael MouseFilm Studies Michael JaggerFilm Studies Stefani GermanottaGeography David BeckhamGeography Elizabeth WindsorGeography Margaret SimpsonGeography Fred Barrett

    Geography Madonna CicconeMaths David BeckhamMaths Elizabeth WindsorMaths Margaret SimpsonMaths Madonna CicconeMusic Techno David BeckhamMusic Techno Reginald Dwight

  • 7/28/2019 Learn SQL With SQLite

    16/26

    Pgina 16arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    Music Techno Michael JaggerMusic Techno Fred Barrett

    or a small modification will let you find who studies Computing . . .

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT SubjectTitle, First, SecondFROM Subject, Student, StudentSubject

    WHERE StudentID = StudentID2AND SubjectID = SubjectID2AND SubjectID = 'CO';>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    and prove it using SQLite

    sqlite> .read MyClass.txtSubjectTitle First Second------------ ---------- -----------Computing William Shakespeare

    Computing Reginald DwightComputing Albert EinsteinComputing Michael JaggerComputing Fred BarrettComputing Madonna Ciccone

    Foreign Keys

    Although the foreign key constraint in not working in this version on SQLite butideally you should include the foreign key constraints.

    StudentID2 is a foreign key in StudentSubject (because it is not theprimary key of that table but it is a primary key of Student)

    SubjectID2 is a foreign key in StudentSubject (because it is not the primarykey 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))

    TopCOUNT function

    COUNT function

    SQL functions do calculations on the data in other fields One such function isCOUNT which counts the number of records in a field. This function can thenbecome a separate field in a query.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    SELECT COUNT(*) FROM Student;

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    which gives

  • 7/28/2019 Learn SQL With SQLite

    17/26

    Pgina 17arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    sqlite> .read MyClass.txtCOUNT(*)----------11

    and confirms that there are 11 students.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    SELECT COUNT(Form) FROM Student;

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    which also gives

    sqlite> .read MyClass.txtCOUNT(Form)

    ----------11

    and confirms that there are 11 students.

    SELECT . . .FROM . . . GROUP BY . . .>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT COUNT(Form) FROM Student GROUP BY Form;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    This groups the data according to the Form value and works out COUNT out oneach group

    sqlite> .read MyClass.txtCOUNT(Form)-----------42212

    You can have more than one column, for example . . .

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT Form, COUNT(Form) FROM Student GROUP BY Form;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    which outputs two columns showing the number of students in each form and itsname.

    sqlite> .read MyClass.txtForm COUNT(Form)---------- -----------13Kr 413Pl 213Sc 2

  • 7/28/2019 Learn SQL With SQLite

    18/26

    Pgina 18arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    13Sp 113Wh 2

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SELECT SubjectTitle, COUNT(SubjectID)FROM Subject, StudentSubjectWHERE SubjectID = SubjectID2GROUP BY SubjectID;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    sqlite> .read MyClass.txtForm COUNT(Form)---------- -----------13Kr 413Pl 213Sc 213Sp 113Wh 2

    How good is that ? Pretty good!

    Top

    UPDATE, DELETE and ALTER TABLE

    ALTER TABLE . . . ADD COLUMN . . .

    The next few SQL commands are used to change the database. It makes moresense to enter them dynamically in SQLite window.

    Comment out the DROP TABLE commands in yout text fileRun the text file in with .read MyClass.txt

    We will add a new field to the Subject Table called ExamBoard which is text.

    ALTER TABLE Subject ADD ExamBoard VARCHAR(10);SELECT * FROM Subject;

    You should see the new column (empty).

    sqlite> SELECT * FROM Subject;

    SubjectID SubjectTitle ExamBoard---------- ------------ ----------

    CO ComputingEN EnglishFS Film StudiesGG GeographyMA MathsMT Music Techno

    UPDATE Subject SET ExamBoard = 'AQA' WHERE SubjectTitle ='Computing';This will make the ExamBoard column AQA in the Computing row.

    SELECT * FROM Subject

  • 7/28/2019 Learn SQL With SQLite

    19/26

    Pgina 19arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    Check it has been entered.

    sqlite> UPDATE Subject SET ExamBoard = 'AQA' WHERE SubjectTitle ='Computing';sqlite> SELECT * FROM Subject;SubjectID SubjectTitle ExamBoard

    ---------- ------------ ----------CO Computing AQAEN EnglishFS Film StudiesGG GeographyMA MathsMT Music Techno

    Suppose the student called 'Reginald Dwight' leaves the school.

    DELETE FROM Student WHERE first = 'Reginald' AND second ='Dwight';SELECT * FROM Student

    Check he has left the table.

    sqlite> DELETE FROM Student WHERE first = 'Reginald' AND second ='Dwight';sqlite> SELECT * FROM Student;StudentID First Second Form---------- ---------- ---------- ----------1 David Beckham 13Sc2 William Shakespear 13Pl3 Elizabeth Windsor 13Pl5 Albert Einstein 13Kr6 Margaret Simpson 13Kr

    7 Michael Mouse 13Kr8 Michael Jagger 13Kr9 Fred Barrett 13Sc10 Madonna Ciccone 13Wh11 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 . . .

  • 7/28/2019 Learn SQL With SQLite

    20/26

    Pgina 20arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    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 SQLitebackup function and create a database file called MyClassDB.dbEnter 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.

  • 7/28/2019 Learn SQL With SQLite

    21/26

    Pgina 21arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    Top

    Foreign Key

    Foreign Key

    When you have a one-to-many relationship between two tables, you candesign a SELECT query to extract information from both tables by usingthe common field. For instance with the Student and Class tables, thecommon field was the Form column in the Student table and the ClassIDcolumn of the Class column. To get a query using both tables you use thecommon field like this:

    SELECT First, Second, Title, SecondnameFROM Student, ClassWHERE Form=ClassID;

    This gives a satisfactory answer.

    However, you can enter new students and give them a form that doesn'texist in the Class table. This is not desirable. We might also try to deletea 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 isthe primary key of a different (foreign) table. The field is indicated as aforeign 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)

    );

    If I try to enter a new student into 13Zz which doesn't exist

    sqlite> Insert INTO Student Values(12,'Fred','Jones','13Zz');sqlite> select * from Student;StudentID First Second Form---------- ---------- ---------- ----------1 David Beckham 13Sc

    2 William Shakespear 13Pl3 Elizabeth Windsor 13Pl4 Reginald Dwight 13Sp5 Albert Einstein 13Kr

  • 7/28/2019 Learn SQL With SQLite

    22/26

    Pgina 22arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    6 Margaret Simpson 13Kr7 Michael Mouse 13Kr8 Michael Jagger 13Kr9 Fred Barrett 13Sc10 Madonna Ciccone 13Wh11 Stefani Germanotta 13Wh12 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 CL413Wh Dr Who TD213Ch Mr Spock ST2

    Eek! This should not be possible!!!

    That is correct - the version of SQLite that we are using does notsupport 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 foreignkeys.

    Top

    Improvements

    Normalisation

    This database is not quite normalised. The Class table should be split intotwo tables Class and Staff. This is because (for instance) Title andSecondname are not strictly dependent on ClassID. This would be abetter setup -

    CREATE TABLE Class(FormID VARCHAR(4) PRIMARY KEY NOT NULL,Room VARCHAR(4),StaffMember VARCHAR(2),FOREIGN KEY (StaffMember) REFERENCES Staff(StaffID));

  • 7/28/2019 Learn SQL With SQLite

    23/26

    Pgina 23arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    CREATE TABLE Staff(StaffID VARCHAR(2) PRIMARY KEY NOT NULL,Title VARCHAR(10),Second VARCHAR(10));

    Lean and mean. :-)

    Top

    SQLite Database Browser

    SQLite Database Browser

    The SQLite Database Browser software lets you use create SQLitedatabases, tables and queries through a GUI (Graphical User Interface).

    It can also accept our text file of SQL commands and convert them intoa database that is visible through its GUI which is what we will do next.

    It is open-source software that can be downloaded from http://sourceforge.net/projects/sqlitebrowser

    First, open your MyClassDB.txt file and delete all the SQLite commands.You can tell which they are because they begin with a full-stop (period).

    Select File/Import/Database From SQL file

    FileName = 'MyClass.txt'Name the new database as 'MyClass.db

    Then you can

    look at database structuremodify the database structure

    run any SQL statements you want for instance -insert datacreate new tables

    create new databases

    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

  • 7/28/2019 Learn SQL With SQLite

    24/26

    Pgina 24arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    (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');

    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');

  • 7/28/2019 Learn SQL With SQLite

    25/26

    Pgina 25arn SQL with SQLite

    04/04/2013 22:44:16p://www.alevel-computing.x10.mx/TutorialSQLite.php

    INSERT INTO Subject(SubjectID, SubjectTitle) VALUES('EN', 'English');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES('FS', 'Film Studies');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES('GG', 'Geography');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES

    ('MA', 'Maths');INSERT INTO Subject(SubjectID, SubjectTitle) VALUES('MT', 'Music Technology');

    CREATE TABLE StudentSubject(

    SubjectID2 VARCHAR(4) NOT NULL,StudentID2 integer NOT NULL,PRIMARY KEY(SubjectID2, StudentID2)

    );

    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);

  • 7/28/2019 Learn SQL With SQLite

    26/26

    Pgina 26arn SQL with SQLite

    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);