Learn SQL The Hard Way - Exercise 2 - Creating Multitable DB - .schema command - sql

I understand the sql in this exercise perfectly, but the setup type tasks are confusing to me. Zed asks you to use the following SQL to create the following tables in a new database that are related by the id key. I'm fine there.
CREATE TABLE person (
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
age INTEGER
);
CREATE TABLE pet (
id INTEGER PRIMARY KEY,
name TEXT,
breed TEXT,
age INTEGER,
dead INTEGER
);
CREATE TABLE person_pet (
person_id INTEGER,
pet_id INTEGER
);
In my windows command prompt I entered:
C:\SQLite> sqlite3 ex2.db < ex2.sql
ex2.db is my new db and ex2.sql contains the create statements listed above.
Zed then asks you to enter .schema using the sqlite command prompt. Nothing happens for me. It does not dump. Does .schema only work with Linux or OSX? I'm on windows.
The following is what he says you should get:
sqlite> .schema
CREATE TABLE person (
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
age INTEGER
);
CREATE TABLE person_pet (
person_id INTEGER,
pet_id INTEGER
);
CREATE TABLE pet (
id INTEGER PRIMARY KEY,
name TEXT,
breed TEXT,
age INTEGER,
dead INTEGER
);
sqlite>
Instead, this is what I get:
sqlite> .schema
sqlite>

You have to start the sqlite3 tool with:
C:\SQLite> sqlite3 ex2.sb
sqlite> .schema
...
Without a database file name, sqlite3 just creates a temporary database.

In the Windows command prompt type:
C:\SQLite> sqlite3 ex3.db
sqlite will open.
sqlite> .schema
Create statements will display.

Related

Creating Databases in SQLite

We were asked to create a database in sqlite3 and then create a table in it. I used this command:
$sqlite3 me5.db
and tried to create a table with this statement:
CREATE TABLE me5.petID(pet id PRIMARY KEY int(3), pet name varchar(10), pet type varchar(10), pet age int(3));
but it says that:
ERROR: near "CREATE" : syntax error
What could I have possible done wrong? Thanks.
try this
CREATE TABLE petID(pet_id int(3) PRIMARY KEY, pet_name varchar(10), pet_type varchar(10), pet_age int(3));
you dont have to specify the database name because you're already using it after the command sqlite3 me5.db.
you have spaces in the names of the fields, which is not allowed. so i've put underscores instead of spaces.
use PRIMARY KEY after int(3)

How to use soft order with JOIN

I want to use table JOIN with soft order for these tables:
CREATE TABLE ACCOUNT(
ID INTEGER NOT NULL,
USER_NAME TEXT NOT NULL,
PASSWD TEXT,
FIRST_NAME TEXT,
LAST_NAME TEXT,
E_MAIL TEXT NOT NULL,
COUNTRY TEXT,
STATE TEXT,
CITY TEXT,
ADDRESS TEXT,
STATUS INTEGER,
SECURITY_QUESTION TEXT,
SECURITY_ANSWER TEXT,
LAST_PASSWD_RESET DATE,
DESCRIPTION TEXT,
LAST_UPDATED DATE,
CREATED DATE
)
;
-- ADD KEYS FOR TABLE ACCOUNT
ALTER TABLE ACCOUNT ADD CONSTRAINT KEY1 PRIMARY KEY (ID)
;
ALTER TABLE ACCOUNT ADD CONSTRAINT USER_NAME UNIQUE (USER_NAME)
;
ALTER TABLE ACCOUNT ADD CONSTRAINT E_MAIL UNIQUE (E_MAIL)
;
-- TABLE ACCOUNT_ROLE
CREATE TABLE ACCOUNT_ROLE(
ID INTEGER NOT NULL,
USER_NAME TEXT NOT NULL,
ROLE INTEGER,
PERMISSION TEXT,
LAST_UPDATED DATE,
CREATED DATE
)
;
-- CREATE INDEXES FOR TABLE ACCOUNT_ROLE
CREATE INDEX IX_RELATIONSHIP19 ON ACCOUNT_ROLE (ID)
;
-- ADD KEYS FOR TABLE ACCOUNT_ROLE
ALTER TABLE ACCOUNT_ROLE ADD CONSTRAINT KEY26 PRIMARY KEY (ID)
;
ALTER TABLE ACCOUNT_ROLE ADD CONSTRAINT RELATIONSHIP19 FOREIGN KEY (ID) REFERENCES ACCOUNT (ID) ON DELETE CASCADE ON UPDATE CASCADE
;
Working query:
SELECT * FROM ACCOUNT ORDER BY %S %S offset ? limit ?
I tried this SQL query:
SELECT *
FROM ACCOUNT_ROLE
INNER JOIN ACCOUNT ON ACCOUNT.ID = ACCOUNT_ROLE.ID
ORDER BY Account.%S Account.%S offset ? limit ?
But I get this error message:
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "Account"
Position: 99
How I can fix this query? I would like to get the data from two tables and sort it based in value.
It is not entirely clear what you are asking so instead I am proposing a few troubleshooting steps:
It looks like you are trying to do some query preprocessing. Please log the query after this is done and troubleshoot based on that. Failing that, check the PostgreSQL logs for the failing query text string (the logged query is better because of how placeholders are handled).
Once you are looking at the query itself, then look at it for syntax errors.
The problems are almost certainly in portions of your code you have not shown us. Knowing how to get the troubleshooting process started however can be worth mentioning.

SQLite not printing

I'm trying to learn SQL through "Learn SQL the Hard Way" and I am having difficulty with the the command prompt. In particular, I am having trouble with the 3rd exercise.
I am able to create a database from ex2.sql by calling
sqlite3 ex3.db < ex2.sql
This should build a database with the schema:
CREATE TABLE person (
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
age INTEGER
);
CREATE TABLE pet (
id INTEGER PRIMARY KEY,
name TEXT,
breed TEXT,
age INTEGER,
dead INTEGER
);
CREATE TABLE person_pet (
person_id INTEGER,
pet_id INTEGER
);
Calling .schema on sqlite3 ex3.db prints out exactly this.
Then I use ex3.sql which has the contents:
INSERT INTO person (id, first_name, last_name, age)
VALUES (0, "Zed", "Shaw", 37);
INSERT INTO pet (id, name, breed, age, dead)
VALUES (0, "Fluffy", "Unicorn", 1000, 0);
INSERT INTO pet VALUES (1, "Gigantor", "Robot", 1, 1);
I insert to ex3.db by saying:
sqlite3 -echo ex3.db < ex3.sql
According to the book, this should insert the values from ex3.sql into ex3.db while printing out what it is doing. However, when I type the above into cmd it prints nothing. And when I call
sqlite3 ex3.db
select * from person;
it still shows nothing. My guess is that either the database file is not updating, something with my install went wrong, or I'm messing something else up here. Appreciate any help that can be given.
The command:
sqlite3 -echo ex3.db < ex3.sql
Is used to print what is in the ex3.sql file as it is executed. If nothing is displayed then nothing will be in the file! This means that it will be a problem with the ex3.sql file, such that it is either empty or corrupt.

Adobe Air Database

I have 3 Questions
1) Can Adobe Air Database create more than 1 table in the DB?
will it work by just executing two create table statement work?
sqls.sqlConnection = sqlc;
sqls.text = "Create table if not exists test_table ( id INTEGER PRIMARY KEY autoincrement, first_name TEXT, last_name TEXT);"
sqls.execute();
sqls.text = "Create table if not exists test_table2 ( id INTEGER PRIMARY KEY autoincrement, first_name TEXT, last_name TEXT);";
sqls.execute();
2) How can i do SQL indexing for a table in a database?
sqls.text= "create index index_1 ON test_table (first_name);";
sqls.execute();
Will this work for indexing?
3) If all the above work, how to i check if the database is implementing the above?
1) Yes.
2) Yes. http://www.sqlite.org/lang_createindex.html I'm not sure if this is what you want, but you can force the use of an index in SQLite via the INDEXED BY keyword: http://www.sqlite.org/lang_indexedby.html

sqlite3 explain table_name

In mysql you can view a table's structure via explain tablename; What is the equivalent for sqlite3?
I believe ".schema tablename" is what you're looking for.
You can use .schema in the Command Line Shell:
With no arguments, the ".schema"
command shows the original CREATE
TABLE and CREATE INDEX statements that
were used to build the current
database. If you give the name of a
table to ".schema", it shows the
original CREATE statement used to make
that table and all if its indices.
This was already answered in a more generic way here.
Edit:
Note that .schema will also give you INDEXES that match the same name.
Example:
CREATE TABLE job (
id INTEGER PRIMARY KEY,
data VARCHAR
);
CREATE TABLE job_name (
id INTEGER PRIMARY KEY,
name VARCHAR
);
CREATE INDEX job_idx on job(data);
Note the differences between:
sqlite> SELECT sql FROM SQLITE_MASTER WHERE type = 'table' AND name = 'job';
CREATE TABLE job (
id INTEGER PRIMARY KEY,
data VARCHAR
)
sqlite> SELECT sql FROM SQLITE_MASTER WHERE name = 'job_idx';
CREATE INDEX job_idx on job(data)
and
sqlite> .schema job
CREATE TABLE job (
id INTEGER PRIMARY KEY,
data VARCHAR
);
CREATE INDEX job_idx on job(data);
Including the semi-colon at the end of the queries.