Junior — Middle
How is record deletion from tables in PostgreSQL implemented?
sobes.tech AI
Answer from AI
In PostgreSQL, deleting records from tables is done using the SQL command DELETE.
Example of basic deletion:
delete from table_name where condition;
For example, to delete a user with id = 10:
delete from users where id = 10;
If no condition is specified, all records in the table will be deleted:
delete from users;
In Go (Golang), deleting records through a database typically uses the database/sql package or an ORM. Example with database/sql:
import (
"database/sql"
_ "github.com/lib/pq"
)
func deleteUser(db *sql.DB, id int) error {
_, err := db.Exec("DELETE FROM users WHERE id = $1", id)
return err
}
Thus, deletion is implemented via the SQL command DELETE with a condition to select records.