TEMP1 = (CONTAINS[FRAG,GENE] times GENE[ID,NAME]) where GENE.ID=CONTAINS.GENE and GENE.NAME='AMP'
TEMP2 = (TEMP1 join PRODUCES)[RCP]*
ANSWER = (TEMP2 join RECIPES) where TEMP2.RCP=RECIPES.RCP
CREATE DATABASE database_name
CREATE TABLE table_name( column_name1 data_type, column_name2 data_type, ...... [PRIMARY KEY (column_name),] [FOREIGN KEY (column_name)REFERENCES table_name(column_name),])
CREATE [UNIQUE ]INDEX index_nameON table_name( column_name)
CREATE TABLE "GENE"
(
ID char(16),
NAME varchar(20),
PROTEIN varchar,
START int,
PRIMARY KEY (ID)
);
CREATE TABLE "PRODUCES"
(
RCP char(16),
FRAG char(16),
DATE date,
FOREIGN KEY (RCP) REFERENCES RECIPE(RCP),
FOREIGN KEY (FRAG) REFERENCES FRAG(ID)
);
CREATE UNIQUE INDEX on GENE (ID);
INSERT INTO "tablename" (first_column,...last_column)VALUES (first_value,...last_value);
INSERT INTO GENE (ID, NAME, PROTEIN, START)
VALUES ('G1', 'AMP', 'MAKK...', -5);
SELECT [DISTINCT ] column1[,column2]FROM table1[,table2] [WHERE "conditions"] [GROUP BY "column-list"] [HAVING "conditions] [ORDER BY "column-list" [ASC |DESC ] ]
SELECT column1[,column2]FROM table1INNER JOIN table2ON join_predicate;
SELECT column1[,column2]FROM table1INNER JOIN table2USING ( column_name) ;
SELECT column1[,column2]FROM table1NATURAL JOIN table2;
SELECT column1[,column2]FROM table1LEFT OUTER JOIN table2ON join_predicate;
SELECT * FROM PRODUCES NATURAL JOIN;
R1|F1|1985-09-09|r1|r1,cr|scooter R1|F2|1985-09-09|r1|r1,cr|scooter R2|F3|1985-10-05|r2|r2.cr|ckw
SELECT * FROM PRODUCES INNER JOIN RECIPE ON PRODUCES.RCP=RECIPE.RCP;
R1|F1|1985-09-09|R1|r1|r1,cr|scooter R1|F2|1985-09-09|R1|r1|r1,cr|scooter R2|F3|1985-10-05|R2|r2|r2.cr|ckw
SELECT * FROM PRODUCES JOIN RECIPE USING(RCP);
R1|F1|1985-09-09|r1|r1,cr|scooter R1|F2|1985-09-09|r1|r1,cr|scooter R2|F3|1985-10-05|r2|r2.cr|ckw
SELECT * FROM PRODUCES LEFT OUTER JOIN RECIPE ON PRODUCES.RCP=RECIPE.RCP;
R1|F1|1985-09-09|R1|r1|r1,cr|scooter R1|F2|1985-09-09|R1|r1|r1,cr|scooter R2|F3|1985-10-05|R2|r2|r2.cr|ckw
SELECT * FROM RECIPE LEFT OUTER JOIN PRODUCES ON PRODUCES.RCP=RECIPE.RCP;
R1|r1|r1,cr|scooter|R1|F1|1985-09-09 R1|r1|r1,cr|scooter|R1|F2|1985-09-09 R2|r2|r2.cr|ckw|R2|F3|1985-10-05 R3|r3|r3.cr|rst|||
CREATE TABLE TEMP1 AS
SELECT CONTAINS.FRAG,CONTAINS.GENE,GENE.NAME FROM CONTAINS,GENE
WHERE GENE.ID=CONTAINS.GENE AND GENE.NAME="AMP";
CREATE TABLE TEMP1 AS
SELECT CONTAINS.FRAG,CONTAINS.GENE,GENE.NAME FROM CONTAINS
INNER JOIN GENE ON GENE.ID = CONTAINS.GENE WHERE GENE.NAME="AMP";
CREATE TABLE TEMP2 AS
SELECT PRODUCES.RCP FROM TEMP1,PRODUCES WHERE TEMP1.FRAG=PRODUCES.FRAG;
CREATE TABLE TEMP2 AS
SELECT PRODUCES.RCP FROM TEMP1
INNER JOIN PRODUCES ON TEMP1.FRAG = PRODUCES.FRAG;
SELECT DISTINCT RECIPE.RCP, RECIPE.NAME, RECIPE.FILE, RECIPE.OWNER
FROM TEMP2, RECIPE WHERE TEMP2.RCP = RECIPE.RCP;
SELECT DISTINCT RECIPE.RCP, RECIPE.NAME, RECIPE.FILE, RECIPE.OWNER
FROM GENE, CONTAINS, PRODUCES, RECIPE
WHERE GENE.NAME = 'AMP' AND GENE.ID = CONTAINS.GENE
AND CONTAINS.FRAG = PRODUCES.FRAG
AND PRODUCES.RCP = RECIPE.RCP;
UPDATE tablenameSET columnname="newvalue"[,nextcolumn="newvalue2"...]WHERE columnnameOPERATOR "value" [AND |OR columnOPERATOR "value"];
UPDATE GENE SET NAME='AMP' WHERE ID='G1';
SELECT COUNT(*) FROM RECIPE,PRODUCES WHERE RECIPE='scooter' AND RECIPE.RCP=PRODUCES.RCP;
#! /usr/bin/python
import sys
import sqlite3
try:
# Open a connection to the database
conn = sqlite3.connect ('bmi219.db')
cursor = conn.cursor()
# Execute an SQL statement -- can be pretty much any SQL
cursor.execute("SELECT NAME, PROTEIN from GENE")
# fetchall returns a list of lists
rows = cursor.fetchall()
for row in rows:
print "%s, %s"%(row[0], row[1])
# Close the cursor and commit any changes to the database
cursor.close()
conn.commit()
conn.close()
except sqlite3.Error, e:
# Handle any errors
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
AMP, MAKK... TET, MYAK... NGF, MYAK...
#! /usr/bin/python
import sys
import sqlite3
try:
conn = sqlite3.connect ('bmi219.db')
# Get a cursor we can work with
cursor = conn.cursor()
# Use the execute method to pass SQL commands to the database
cursor.execute("DROP TABLE IF EXISTS `GENE`")
# Note that we use triple quotes when we need multiple lines
cursor.execute("""
CREATE TABLE 'GENE'
(
'ID' char(16),
'NAME' varchar(20),
'PROTEIN' longtext,
'START' int,
PRIMARY KEY (`ID`)
)
""")
cursor.execute("""
INSERT INTO 'GENE' VALUES
('G1','AMP','MAKK...',-5)
""")
cursor.execute("""
INSERT INTO 'GENE' VALUES
('G2','TET','MYAK...',-10)
""")
cursor.execute("""
INSERT INTO 'GENE' VALUES
('G3','NGF','MYAK...',-1)
""")
print "Number of rows inserted: %d"%cursor.rowcount
# OK, now lets try to get some data out
cursor.execute("SELECT NAME, PROTEIN from GENE")
while (1):
row = cursor.fetchone ()
if row == None:
break
print "%s, %s"%(row[0],row[1])
print "Number of rows returned: %d"%cursor.rowcount
# Another way to do the same thing
cursor.execute("SELECT NAME, PROTEIN from GENE")
rows = cursor.fetchall ()
for row in rows:
print "%s, %s"%(row[0],row[1])
print "Number of rows returned: %d"%cursor.rowcount
cursor.close()
conn.commit ()
conn.close()
except sqlite3.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)