200 likes | 300 Views
SQL. CMSC 461 Michael Wilson. Finally, some code. This is where the theory and practice actually come together Basically taking the relational algebra and mapping it all to a query language The concepts are very close to what we’ve already seen, but there are some nuances. One true SQL?.
E N D
SQL CMSC 461 Michael Wilson
Finally, some code • This is where the theory and practice actually come together • Basically taking the relational algebra and mapping it all to a query language • The concepts are very close to what we’ve already seen, but there are some nuances
One true SQL? • There are ISO/ANSI SQL standards • In my experience, different DBMSes have different/proprietary extensions that can make raw SQL queries pretty DBMS specific • The extensions aren’t exactly frequent • Invariably, it affects whatever thing you’re working on
DBMS to focus on? • I firmly believe that PostgreSQL is going to be a big deal • MySQL is getting mired in some drama relating to being owned by Oracle • Forked off into MariaDB • Therefore, I’m going to focus on Postgres • Also, AWS RDS supports Postgres now • You are welcome to use whatever you wish for projects • RDS supports MySQL and PostgreSQL, but you’re not limited to AWS
Before we get started • PostgreSQL’s SQL language documentation • http://www.postgresql.org/docs/9.3/static/sql.html • Use it! • PostgreSQL is case insensitive and insensitive to whitespace between commands • Can spread a command across multiple lines or type it all on one line
Getting started • Most DBMSes can have multiple databases • Each database can consist of many tables • If we configure PostgreSQL using AWS RDS, then we don’t have to create the database ourselves • This can be a little complex in Postgres • We’re going to assume the database is created
Connecting to your database using psql • psql –h <hostname> -d <database_name> -U <username> • Command line PostgreSQL client • On success, this will drop you to a prompt
Table creation • Before you can do anything, you need to create a table • Basic syntax: • CREATE TABLE table_name (column_namedata_type,column_namedata_type, ….);
Table creation • This is effectively creating a new relation • We’ll be referencing this a lot • We’ll also be building on it, so expect to learn new features about table creation as we go • This syntax will create a basic table with columns
Data types • Some basic ones to know • Integer (signed 4 byte integer) • Numeric (numbers with high precision) • Kind of like a double, but has EXACT calculations • Boolean (true/false) • Varchar (character strings of variable lengths) • Text(unlimited length strings) • Not part of SQL standard
Data types • Databases support TONS of data types • Numbers, dates, booleans, money, geometric shapes, JSON, etc. • Some are part of the SQL standard, some aren’t • Best to refer to the PostgreSQL documentation for guidance on data types • Lots of nuances, recommendations, etc.
Table creation example • CREATE TABLE AddressBook (address varchar(256),daysSinceContact integer,contactPhoneNumbervarchar(64),numberTypevarchar(16),contactNamevarchar(256));
Table insertion • Figure it might be handy to actually add data to our table • This is like adding a tuple to the relation • Basic syntax: • INSERT INTO <table> (column_name1, column_name2, … column_name_n)values (value_1, value_2, …, value_n)
Table insertion • You can specify the columns in any order you want • As long as you specify the values in the corresponding order
Table insertion • A note • If you recall, tuples are explicitly unique in databases • This is not the case in tables • They can be made this way, but this is not the default behavior
Table insertion example • INSERT INTO AddressBook (address, daysSinceContact, contactPhoneNumber, numberType, contactName)VALUES (’21 Jump Street’, 40, ‘123 4567’, ‘Cell’, ‘Johnny’)
Enough for today • See you next class!