Delete Records in the databaseλ︎
Using next.jdbc.sql
functions provides a Clojure data structures approach, where as next.jdbc/execute!
uses specific SQL statement code.
{% tabs clojure="next.jdbc.sql functions", sql="next.jdbc/execute!" %}
{% content "clojure" %}
Generic delete record functionλ︎
Use the generic delete function from the database schema design section
(defn delete-record
"Insert a single record into the database using a managed connection.
Arguments:
- table - name of database table to be affected
- record-data - Clojure data representing a new record
- db-spec - database specification to establish a connection"
[db-spec table where-clause]
(with-open [connection (jdbc/get-connection db-spec)]
(jdbc-sql/delete! connection table where-clause)))
Delete an existing account_holder recordλ︎
Call the delete-record
function with the development database specification, the table name and a where clause to locate the specific record to delete. This where clause should use a unique value, e.g. the primary key for the table.
(delete-record db-specification-dev :public.account_holders {:account_holder_id "0bed6afe-6740-46a1-b924-36ef192eac66"})
If the record deletion is successful then :update-count 1
value is returned
Deleting an existing account recordλ︎
Update an existing record in the public.accounts
table, providing new values for current_balance
and last_updated
columns.
Deleting an existing transaction recordλ︎
Update an existing record in the public.transaction_history
table.
(delete-record db-specification-dev :public.transaction_history {:transaction_id "8ac89cfc-6874-4ebe-9ee4-59b8c5e971ff"})
{% content "sql" %}
Insert account_holdersλ︎
Insert accountsλ︎
Insert transactionsλ︎
{% endtabs %}
Hint::Generating example data from Clojure Specλ︎