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 ml ml 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 -d -U Command line PostgreSQL client On success, this will drop you to a prompt
Connecting to your database using psql
Table creation Before you can do anything, you need to create a table Basic syntax: CREATE TABLE table_name ( column_name data_type, column_name data_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 address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street CellPhil 8 Get Out of Here Way WorkBob 7 RUN! Drive CellOctavio
Table creation example CREATE TABLE AddressBook (address varchar(256), daysSinceContact integer, contactPhoneNumber varchar(64), numberType varchar(16), contactName varchar(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 (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, ‘ ’, ‘Cell’, ‘Johnny’)
Table insertion example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street CellPhil 8 Get Out of Here Way WorkBob 7 RUN! Drive CellOctavio 21 Jump Street CellJohnny
Enough for today See you next class!