%% Execute Non-SELECT SQL Statement
% Using the PostgreSQL native interface, create and execute a non-SELECT SQL statement that deletes a database table. The PostgreSQLDataSource data source configures a database connection to a PostgreSQL database.
% This example uses a PostgreSQL database version 9.405 database and the libpq driver version 10.12.
%
% Connect to the database using the data source name, user name, and password.

datasource = "PostgreSQLDataSource";
username = "dbdev";
password = "matlab";

conn = postgresql(datasource,username,password);

%
% Load patient information into the MATLAB® workspace.

patients = readtable("patients.xls");

% Create the patients database table using the patient information.

tablename = "patients";
sqlwrite(conn,tablename,patients)

%
% Import the data from the patients database table.

data = sqlread(conn,tablename);

%
% Delete the patients database table using the execute function.

sqlquery = strcat("DROP TABLE ",tablename);
execute(conn,sqlquery)

%
% Ensure that the table no longer exists.

data = sqlfind(conn,tablename)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.