Lesson 06.01 Working With SQL

33
ADVANCED ANDROID DEVELOPMENT Lesson 6.01 – Working with SQL and SQLite

description

Lesson 06.01 Working With SQL

Transcript of Lesson 06.01 Working With SQL

  • ADVANCED ANDROID

    DEVELOPMENTLesson 6.01 Working with SQL and SQLite

  • SQLiteIt is typical for applications to store data in a relational database. A database is very efficient at working with different types of data, and is able to quickly recall that data as it becomes necessary.

    Android provides SQLite database engine for us to work with.

    "SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain."

    http://www.sqlite.org/

  • DatabasesBefore we get down to the details about how SQLite actually works, lets take a step back, and look at databases in general.

    A database is really nothing more than a well organized storage facility for your data. Everyone, at

    one time or another, or on a regular basis, uses databases, and may not even know it.

    For example, Google search engine, is a large database of web sites. Your address book maintained by your email program, is a database. A checkbook manager that helps you balance your finances, is a database. And even the television schedule that you retrieve on your DVR or cable box is a database.

  • DatabaseThink of a database as a file cabinet.

    A well organized file cabinet will have sections for different types of files. For example, you may have a set of files about your authors in the top drawer. A set of files about the books they wrote in the middle drawer, and a set of publishers who published those books for those authors in the third drawer.

    The problem with the file cabinet though is that its not easy to quickly identify all of the books that were written in a specific year, or identify all of the publishers used by a particular author. For that matter, you'd have to do some digging in order to find all of the books that an author wrote throughout his or her career. These are typicaly queries that are easily addressed with a well managed database.

  • Tables and FieldsIn the file cabinet from our previous slide, you have a drawer for each of your three main categories:

    Authors, Titles, and Publishers.

    In a relational database, you would have three tables, each one containing information about a

    specific category, such as authors, titles, and publishers.

    In the file cabinet, each drawer would contain multiple files, each one with information about a specific person, book, or publisher. In a database table, you would have rows of data, each row specific to a person, book or publisher.

    Each row of data in a table is further broken down into fields. A field is similar to a variable. It holds a single piece of information of a certain data type. Fields can be numeric, string, boolean, date or time, or even binary.

  • Relationships

    The easiest way to describe the relationship between Authors, Titles, and Publishers is with the above database "schema". A schema is a database diagram, or representation.

    In the Authors table, we are storing the name and year of birth for the author. Each author is assigned a unique identifier called Au_ID. That identifier is used in in the Title Authors table to associate one or more book ISBN numbers to an author in my Authors table. The ISBN is also a unique identifier, and joins to the Titles table, where each book is described. Finally, the Publishers table joins to the Titles table in order to establish the relationship between the publisher and the books they released to market.

  • Primary KeysThose unique identifiers I mentioned before, are known as primary keys. They are always unique. Even if a record is deleted from a database, the deleted primary key will never be introduced back into the database naturally. You can certainly, re-create the deleted record manually.

    The primary keys, and the relationships defined between tables, help keep data integrity, consistency, and makes it easily searchable. Deleting an author would require you to also delete all of the books that the author wrote (unless co-authored), as an example.

  • TableFields

    Rows of Data (a Record)

  • Designing a SchemaThe first step, when building your new database, is to review and organize your data into categories. While reviewing your data, look for ways to minimize data redundancy. Duplicate data in multiple tables is wasteful, and is difficult to maintain. The art of removing duplicate information and replacing it with a relationship instead is known as normalization.

  • Table RelationshipsThere are several different types of relationships that can be set up between database tables.

    A relationship represents how a table is able to locate related information in another table within your database. The basic relationships are:

    One To One This means that one record in Table A has one record of related information available in Table B.

    One To Many Table A has one record, but Table B has multiple records that relate the record in A.

    Many To Many Multiple records in Table A can relate to multiple records in Table B.

    Alternatively, data can stand on its own, and it isnt always necessary for a table to have a relationship with another table.

    This classification of the number of records that can exist on each side of the relationship is known as the "Cardinality".

  • One To One RelationshipIn this cardinality, one customer can have only one credit limit. This is a one to one relationship.

    Customer # Name Address Phone

    12345 Test Person 123 Main Street 555-1212

    12346 Another Person 44 Main Street 555-1111

    12347 Last Person 55 Main Street 555-1010

    Credit ID Customer # Credit Limit

    7 12345 $500

    8 12346 $1,000

    9 12347 500.00$

    Table A

    Table B

  • One To Many RelationshipA single customer (A) relates to multiple invoices (B).

    Customer # Name Address Phone

    12345 Test Person 123 Main Street 555-1212

    12346 Another Person 44 Main Street 555-1111

    12347 Last Person 55 Main Street 555-1010

    Table A

    Invoice # Customer # Amount

    3 12345 $299

    4 12345 $199

    5 12347 $399

    Table B

  • Many To Many RelationshipIn a many to many relationship, many records in one table can apply to records in another table. In the above example, Customer 12345 has a Visa and a Mastercard, while Customer 12346 has only an American Express card, and Customer 12347 has American Express and Mastercard. You can see that in order to create this relationship, a third table (called a "Junction" table) is introduced that stores keys from the other two.

    Customer # Name Address Phone

    12345 Test Person 123 Main Street 555-1212

    12346 Another Person 44 Main Street 555-1111

    12347 Last Person 55 Main Street 555-1010

    Customer # Credit Card ID

    12345 1

    12346 3

    12347 2

    12345 2

    12347 3

    Credit Card ID Type

    1 Visa

    2 Mastercard

    3 American Express

  • JoinsThere are several different ways of joining tables together using SQLite T-SQL queries.

    Inner Join allows you to select records from two or more tables where ONLY the matching records are returned. So if you have a Customer and Invoice table, and there are more customers than invoices, only records for customers who have invoices will be returned.

    Outer Join An outer join allows you to look at data in both tables. An outer join will return records that match, but also return records from the table you joined to that do not match. So, again, with Customers and Invoices, an outer join would return everything that the Inner join did, but in addition to that it would return the remaining customers, with no information for the invoice portion of your result set.

    As we build tables, and views, joins and their usage will become more apparent.

  • Database ManagersThere are a number of free, and paid applications that can be used to manage your SQLite database.

    NaviCat - http://www.navicat.com/ - This is a premium database management tool, which supports

    many different databases, not just SQLite. I use it with MySQL, SQLite, and SQL Server. Windows only.

    SQLite Admin - http://sqliteadmin.orbmu2k.de/ - This is a Windows only utility. Free.

    SQLite Manager - http://www.sqlabs.com/sqlitemanager.php - Windows and OS X friendly, but not free.

    SQLite Manager https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/ - This is a free Firefox add-on. It works across Windows, OS X, and Linux, and the utility we'll be using in this class.

  • Getting StartedUsing Firefox, download and install the SQLite Manager v0.77 (or higher).

    After the installation, the tool will become available in your Firefox menu, under Web Developer.

  • SQLite ManagerOnce you open SQLite Manager, you can close Firefox.

    To create a new database, select Database, then New Database. Give it a path and filename.

  • Building TablesOnce you have a database created, you can add tables. To do so, right-click over the Tables node, and select Create Table. You can also use the Create Table icon in the SQLite Manager toolbar.

  • Supported Data TypesThe fields that you define within your table has to be of a specific type. For example, a field can be a string of text, or a date, or a currency value, or just a number.

    SQLite doesnt support many data types, and in fact, you can define a field as being numeric, and it would be perfectly OK with the database to store some text there instead. SQLite allows you define a data type, but it is only a recommendation, not the actual type. Internally, SQLite will store your values as integers, real, text, blob, or null.

  • SQLite Internal Data TypesEach value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:

    NULL - The value is a NULL value.

    INTEGER - The value is a signed integer.

    REAL - The value is a floating point value.

    TEXT - The value is a text string.

    BLOB - The value is a blob of data, stored exactly as it was input.

    BOOLEAN SQLite doesnt have a true / false data type. Instead it saves integers (0 for false, 1 for true).

  • External Data TypesDefine each of your fields to use one of these types. The engine itself will take care of the conversion process between these external versions, and what the database actually supports (internal versions):

    Integer A whole number.

    Bool Boolean (True or False)

    Double 15 decimal places floating number.

    Float 6 decimal places floating number.

    Char A single character.

    Text A large amount of text. Typically this type of field is not searchable.

    VarChar Variable length character string.

    Blob Binary data.

    Numeric Numeric can be used to define the number of decimal places to include.

    DateTime Date and/or time.

  • 1. Column Names Column names should not have spaces in them, if you can avoid it.

    2. Data Type These are the external data types foryour defined fields.

    3. Options

    Primary Key Set this if the field you are workingwith is the unique identifier for each record.

    Autoinc? Auto-incremental integer option. This is often used with Primary Keys, which startat 1 and increment automatically each timea new record is added.

    Allow Null? If checked, empty, or null valuescan be stored in the field, otherwise a valuemust be provided.

    Unique? Creates a field who's value must beunique. A primary key is a unique value.

    Default Value We can specify a default forthe database to use, when adding new records.

    Columns

    21

    3

  • Creating Data1. Select the table you want to add data to.

    2. Click on the Browse & Search tab.

    3. Click the Add button.

    4. Fill out the form, and click OK when complete. Note that you do not have to specify a value forthe primary key column, since it will be automatically created for you.

    1

    2

    3

    4

  • Running Queries1. Select the Execute SQL tab.

    2. Enter a query (select, update, insert, delete, etc).

    3. Click on Run SQL

    4. If your query returns a result, it will bedisplayed in a grid format.

    1

    2

    3

    4

  • Structured Query LanguageThe Structured Query Language (SQL) is a standard language for defining and manipulating relational databases. Virtually all relational database products on the market today support some form of SQL.

    For truly flexible, powerful database programming, SQL is a vital element.

  • RulesImportant things to know about SQL statements:

    Tables and fields that contain spaces, punctuations, or named after SQL keywords must be surrounded by square brackets [ ].

    Dates are always surrounded by single quotes. Ex: '06/01/1999'

    Text data is always surrounded by single quote marks, while numeric data is not.

    You can use wildcards (*) to select all fields within a table, although this is less efficient.

    SQL is not case sensitive.

    SQL statements to manipulate data consist of four statements

    SELECT Selects specified values from fields in a table or tables from one or more databases.

    UPDATE Changes the contents of an existing record or records.

    INSERT Creates a new record.

    DELETE Deletes a record or records.

  • SelectThe select statement can be used to select data from a table or tables within a database.

    Syntax:

    Examples:

    SELECT Field1, Field2, Field3 FROM TableNameWHERE user specified condition exists

    SELECT * FROM Authors

    SELECT Authors.Au_ID, Authors.Author, [Title Author].ISBNFROM Authors INNER JOIN [Title Author] ON Authors.Au_ID = [Title Author].Au_IDWHERE Authors.Au_ID = 7576

  • Aggregate FunctionsThere are a number of useful functions available within SQL to help aggregate data.

    We can collect the Sum, Average, Min and Max data of specified fields. It is also possible to limit the number of records returned by the query using the LIMIT keyword. Using DISTINCT, you can get a set of unique records.

    Examples:

    SELECT SUM ([Year Born]) AS SumYear, AVG ([Year Born]) AS AvgBorn,MAX([Year Born]) AS Youngest, MIN ([Year Born]) AS Oldest

    FROM Authors WHERE [Year Born] IS NOT NULL

    SELECT * FROM Authors LIMIT 5

    SELECT DISTINCT [Year Born] WHERE [Year Born] IS NOT NULL

  • Where Clause FunHere are some examples of how you can limit data using the WHERE clause.

    SELECT * FROM Authors WHERE [Year Born] BETWEEN 1940 AND 1970

    SELECT * FROM AuthorsWHERE [Year Born] IS NOT 1970

    SELECT * FROM AuthorsWHERE [Year Born] >= 1940

    SELECT * FROM AuthorsWHERE [Year Born] < 1970

    SELECT * FROM Authors WHERE Author LIKE 'Stevens%' AND [Year Born] BETWEEN 1940 AND 1970

    SELECT * FROM Authors WHERE Author = 'Stevens, Al'

  • Sort OrderYou can specify a sort order for the results of your select queries. Sorting can be done on multiple fields, and each field can be sorted in an ascending or descending fashion.

    Examples:

    SELECT * FROM Authors WHERE [Year Born] BETWEEN 1940 AND 1970ORDER BY Author, [Year Born] DESC

    SELECT * FROM AuthorsWHERE [Year Born] IS NOT 1970ORDER BY Author DESC

  • Insert StatementThe INSERT statement allows us to add new records to a table. The syntax for the statement is as follows:

    The order in which you enter your field names should also be order in which you enter the values for those fields. Because INSERT is an action query, it does not return any data.

    Example:

    Notice that the query doesn't set a value for the AU_ID field. The correct primary key value will automatically be added by the database when the record is inserted.

    INSERT INTO TableName (field1, field2, field3)VALUES (value_for_field1, value_for_field2, value_for_field3)

    INSERT INTO Authors(Author, [Year Born])VALUES ('Alex Tushinsky', 1970)

  • Update StatementThe UPDATE statement allows us to modify existing records. UPDATE is a very powerful statement that is capable of altering an entire table, if not used correctly. Almost always, it requires the use of a WHERE clause that specifically identifies the record you want to modify. Caution: If the WHERE clause is

    omitted, the UPDATE statement will proceed to alter the entire table to the value specified by the statement.

    Syntax:

    Example:

    The above example will set the YEAR BORN field to 1970 on the record that has the Au_ID field set to 1.

    If the WHERE clause was omitted, then the entire tables YEAR BORN field would be set to 1970.

    UPDATE TableNameSET Field1 = Value, Field2 = ValueWHERE RecordID = Value

    UPDATE AuthorsSET [Year Born] = 1970WHERE Au_ID = 1

  • Delete StatementFinally, we can use the DELETE statement to remove records from a table. The DELETE statement works very much like the SELECT statement with the exception that the records met by the search criteria will be deleted rather than returned to the screen.

    Syntax:

    Note that DELETE statements are generally performed on one table at a time. You do not specify fields in a delete statement, as the entire record is removed. Caution: Once again, if you omit the

    WHERE clause, SQL will proceed to delete every record in the table.

    Examples:

    DELETE FROM TableNameWHERE RecordID = Value

    DELETE FROM AuthorsWHERE Au_ID = 1

    DELETE FROM AuthorsWHERE [Year Born] IS NOT NULL

    DELETE FROM Authors