Database 101
-
Developed in the 1970s
-
initially, Structured English Query Language (SEQUEL)
-
Database Management System (DBMS)
-
Structured Query Language (SQL)
SQL
Schema
Structured Data
- PostgreSQL
- Oracle DB
- MySQL
- IBM DB2
- Microsoft SQL Server
- Apache Derby
- MariaDB
- SQLite
- HyperSQL
- TERADATA
NoSQL
Non-Structured Data
- Redis
- Apache CouchDB, "Time to relax"
- Apache HBASE
- HYPERTABLE
- Cassandra
- riak
- MongoDB, Document Oriented - (MongoDB Query Language)
- Depending on different databases, some data type might not work in one while working flawlessly in another );
- Data types often could be database specific!
- bit
- tinyint
- smallint
- int
- bigint
- decimal
- numeric
- float
- real
- date
- time
- datetime
- timestamp
- year
- char
- varchar
- text
- binary
- image
- json
- xml
- Windows w/ WSL
- DBeaver as DBMS
winget install dbeaver.dbeaversudo apt install postgresqlsudo systemctl start postgresqlstopstatusrestart
sudo su - postgrespsql -U postgrescreatedb test- user/password/etc.
ALTER USER username WITH PASSWORD 'new_password';CREATE USER starboy WITH PASSWORD 'helloworld...lol';psql -h localhost -U postgres -d testCREATE DATABASE test;\list\l\du\! clearYes, there's a space before clear
CREATE TABLE table_name (column_1 datatype, column_2 datatype, column_3 datatype);Tip: You can do this too.)
create table users (name text, age smallint, birthday date);- Semicolon (
;) at the end of the syntax isImportant! CTRL+C
\dINSERT INTO table_name(column_1, column_2, column_3) VALUES (value_1, value_2, value_3);insert into users(name, age, birthday) values ('Sam', 3000, '2049-12-31');SELECT name, age, birthday FROM users;
select * from table_name;- Wildcard (
*)
ALTER TABLE table_name ADD column_name datatype;UPDATE table_name SET column_name = value WHERE column_identifier = column_value;update users set score=0 where name='Sam' or name='Starboy';case-sensitive%= whatever*
SELECT * FROM table_name WHERE column_name LIKE 'A%';select * from users where name like '%a';- Descending order
SELECT * FROM table_name ORDER BY column_name DESC;select * from users order by score asc;
AVG()
SELECT AVG(column_name) FROM table_name;SUM()
SELECT SUM(column_name) FROM table_name;COUNT()
SELECT COUNT(column_name) FROM table_name;
- auto-incrementing IDs
CREATE TABLE login(
ID serial NOT NULL PRIMARY KEY,
secret VARCHAR (100) NOT NULL,
name text UNIQUE NOT NULL
);INSERT INTO login (secret,name) VALUES ('xyz', 'Sam'); select * from login;The real power of Relational Databases.
- Schemas
- Separation of concerns*
SELECT * FROM table_1 JOIN table_2 ON table_1.identifier = table_2.identifier;Here, Sam is a foreign key in login and in users, Sam is a primary key
select * from users join login on users.name = login.name;
DELETE FROM table_name WHERE column='Something';
Note: Be Careful with this!
DROP TABLE table_name;
Tip: Don't play with prod environments!
Note: Be Extra Careful with this !
DROP DATABASE database_name;You can't drop the db you're currently logged in!
\list
\lCREATE DATABASE prod;CREATE TABLE users (
id serial PRIMARY KEY,
name VARCHAR(100),
email text UNIQUE NOT NULL,
entries BIGINT DEFAULT 0,
joined TIMESTAMP NOT NULL
);\dCREATE TABLE login (
id serial PRIMARY KEY,
hash varchar(100) NOT NULL,
email text UNIQUE not null
);select * from login;\! clear
psql --versionpsql -V
psql --helpman psql
Here's something...
Until Next Time...✌️

