Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amadr-95/sql-essentials
https://github.com/amadr-95/sql-essentials
databases postgresql sql
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/amadr-95/sql-essentials
- Owner: amadr-95
- Created: 2024-04-19T10:05:40.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-04-25T16:21:26.000Z (9 months ago)
- Last Synced: 2024-10-31T09:23:53.109Z (3 months ago)
- Topics: databases, postgresql, sql
- Homepage:
- Size: 49.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# [SQL ESSENTIALS](https://www.amigoscode.com/courses/sql)
## Connect to PostgreSQL server on Windows
```bash
psql -U user_name
```## Connect to a database through the terminal
```bash
psql -U user_name -d db_name
```## List databases
```bash
\l
```## Connect to a database
```bash
\c db_name
```## Describe tables
```bash
\d (show all tables)
\d db_name (describe the specified table)
```## Import .sql script
```bash
\i script/path/.sql
```## List of functions
```bash
\df
```## Export query to CSV file
```bash
\copy (query) TO 'path/file_name.csv' DELIMITER ',' CSV HEADER;
```## PostgreSQL extensions
```sql
SELECT * FROM pg_available_extensions;
```
### Install extension
```sql
CREATE EXTENSION IF NOT EXISTS "extension_name";
```## CREATE TABLE
```sql
CREATE TABLE table_name (
column name + data type + constraints if any
);
```
## DROP TABLE
```sql
DROP TABLE table_name;
```
## ADDING AND DROPPING CONSTRAINTS
### PRIMARY KEY
```sql
ALTER TABLE table_name ADD PRIMARY KEY (table_column);
```### UNIQUE
```sql
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(table_column);
--OR
ALTER TABLE table_name UNIQUE(table_column);
```### CHECKS
```sql
ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK(column_condition/s);
```### DROP CONSTRAINTS
```sql
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
```
## MODIFYING DATA
### INSERTING RECORDS
```sql
INSERT INTO table_name(colum1, column2, ...)
VALUES (value1, value2, ...);
```### DELETING RECORDS
```sql
DELETE FROM table_name WHERE conditions;
```### UPDATING RECORDS
```sql
UPDATE table_name SET column = new_data WHERE conditions;
```## SQL COMMANDS ORDER
```sql
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
FETCH/LIMIT
OFFSET
```