- This is an advanced treatment of Database Control using queries instead of SQLite Manager tabs.
Enter the data directly
Show students the SQLite Manager
Execute SQL
tab.
CREATE TABLE
- name table
- list field names and data types
- may constrain data values
Null
Unique
- value length
- may constrain data values
CREATE TABLE Experiment(
LoginID TEXT,
Project TEXT,
Experiment INTEGER,
Hours REAL,
ExperimentDate TEXT
);
- We realized we want to add some more specific configuration to the table.
- Restart requires:
DROP TABLE Experiment
- Add unique
RecordID
field to be the primary key - Prevent important values from being
NULL
CREATE TABLE Experiment(
RecordID INTEGER PRIMARY KEY AUTOINCREMENT,
LoginID TEXT NOT NULL,
Project TEXT NOT NULL,
Experiment INTEGER NOT NULL,
Hours REAL,
ExperimentDate TEXT(10)
);
- Add new records
INSERT INTO
- If values for all fields are being added:
INSERT INTO Experiment
VALUES (1, 'ipav', 'Conditioning', 1, 3.2, '1910-07-05');
- Add incomplete records
- If you are only adding values for some of the possible fields
- Indicate which fields the values correspond to:
- If you are only adding values for some of the possible fields
INSERT INTO Experiment (LoginID, Project, Experiment, Hours,
ExperimentDate)
VALUES ('jane', 'Great Apes', 1, 7, '1967-04-13');
Modifying existing records
UPDATE
- name table
SET
values of any or all of the fieldsWHERE
conditions are met- Don’t forget the
WHERE
clause or the update statement will update all of the records in the database.
- Don’t forget the
UPDATE Experiment
SET Hours=7.5, ExperimentDate='1967-04-19'
WHERE RecordID=2;
Deleting records
DELETE
- name table
WHERE
conditions are met- Don’t forget the
WHERE
clause or the update statement will update all of the records in the database.
- Don’t forget the
DELETE FROM Experiment
WHERE LoginID='ipav';