Postgresql Flashcards

(35 cards)

1
Q

1 What command is used to create a new PostgreSQL role

A

Use CREATE ROLE role_name optionally adding attributes such as LOGIN or CREATEDB

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2 How do you create a role that can log in

A

Use CREATE ROLE role_name LOGIN PASSWORD ‘password’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3 What is the difference between a role and a user in PostgreSQL

A

There is no functional difference a user is simply a role with the LOGIN privilege

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4 How do you list all roles in a PostgreSQL database

A

Use the psql command \du to display all roles

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

5 How is a new database created in PostgreSQL

A

Use CREATE DATABASE db_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

6 How do you assign a database owner at creation time

A

Use CREATE DATABASE db_name OWNER role_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

7 How do you connect to a specific database using psql

A

Use \c db_name to switch databases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

8 What command deletes a PostgreSQL database

A

Use DROP DATABASE db_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

9 What is a schema in PostgreSQL

A

A schema is a logical namespace that organizes database objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

10 How do you create a new schema

A

Use CREATE SCHEMA schema_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

11 How do you create a table in PostgreSQL

A

Use CREATE TABLE table_name (column_name data_type)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

12 How do you specify a primary key in a table definition

A

Declare PRIMARY KEY (column_name) or append PRIMARY KEY to the column

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

13 What is the purpose of the SERIAL data type

A

It automatically generates sequential integers using a sequence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

14 How do you insert a row into a table

A

Use INSERT INTO table_name (columns) VALUES (values)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

15 How do you insert multiple rows in one statement

A

Use INSERT INTO table_name VALUES (row1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

16 How do you insert data from another table

A

Use INSERT INTO table_name SELECT columns FROM other_table

17
Q

17 How do you prevent NULL values in a column

A

Add the NOT NULL constraint to the column

18
Q

18 What does the UNIQUE constraint enforce

A

It ensures all values in a column or column set are distinct

19
Q

19 How do you define a foreign key

A

Use FOREIGN KEY (column) REFERENCES other_table(column)

20
Q

20 How do you grant privileges on a table

A

Use GRANT privilege ON table_name TO role_name

21
Q

21 How do you revoke privileges from a role

A

Use REVOKE privilege ON object FROM role_name

22
Q

22 What privilege allows a role to create tables in a schema

A

The CREATE privilege on the schema

23
Q

23 How do you set a role as the default owner of new objects

A

Create objects while connected as that role or use ALTER DEFAULT PRIVILEGES

24
Q

24 How do you change the owner of a table

A

Use ALTER TABLE table_name OWNER TO role_name

25
25 What command lists all tables in the current schema using psql
Use \dt
26
26 Insert a new row into a table named students with values 1 Alice Physics
INSERT INTO students VALUES (1
27
27 Display all rows and columns from a table named courses
SELECT * FROM courses
28
28 Insert a row into employees specifying only id and name with values 5 Jordan
INSERT INTO employees (id
29
29 Display only the email column from the users table
SELECT email FROM users
30
30 Insert three rows into departments with values 1 Math 2 CS 3 Biology
INSERT INTO departments VALUES (1
31
31 Display all rows from orders where status is pending
SELECT * FROM orders WHERE status = 'pending'
32
32 Insert rows into books using values selected from new_books
INSERT INTO books SELECT * FROM new_books
33
33 Display all table names in the current database using SQL
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'
34
34 Insert the current timestamp into created_at in the logs table
INSERT INTO logs (created_at) VALUES (CURRENT_TIMESTAMP)
35
35 Display the total number of rows in the customers table
SELECT COUNT(*) FROM customers