Oracle SQL and Generative AI

Oracle SQL and Generative AI

I had done the first post around use of Generative AI tools like #CharGPT to generate Oracle SQL here.

I decided to test it further using an Oracle APEX workspace which is free to obtain. While there is a approval process, it is usually very quick. The overall goal is to see if we have to start from scratch an Oracle database environment for testing or training, can we do it quickly using Generative Tools along with generation of sample data.

Since, I wanted to start from scratch, I started by asking it to give me the SQL for employee table:

Provide SQL to create an employee table with columns emp_id, emp_name, start_dt, position, comments, dept_id in Oracle database;

ChatGPT gave me the SQL with foreign key to Department table.

CREATE TABLE employee (

?emp_id NUMBER(10) PRIMARY KEY,

?emp_name VARCHAR2(50) NOT NULL,

?start_dt DATE NOT NULL,

?position VARCHAR2(50),

?comments VARCHAR2(200),

?dept_id NUMBER(10),

?CONSTRAINT fk_dept_id FOREIGN KEY (dept_id)

??REFERENCES department (dept_id)

);

ChatGPT provided a decent explanation as well of the SQL:

This SQL statement creates a table named "employee" with the specified columns and data types. The "emp_id" column is defined as the primary key, while the "dept_id" column is defined as a foreign key referencing the "dept_id" column in a table named "department". The "NOT NULL" constraint is used to ensure that certain columns cannot be left empty when inserting data into the table.

Since ChatGPT gave me the SQL with foreign key to Department table, I asked it for the SQL to create the Department table.

I got that as well:

CREATE TABLE department (

?dept_id NUMBER(10) PRIMARY KEY,

?dept_name VARCHAR2(50) NOT NULL

);

After creating the Department and the Employee table in that sequence, in APEX environment, I asked ChatGPT to help create 10 rows of sample data to populate the Department table.

create 10 records to create sample data in table department table and the SQL to insert those records in Oracle database

That is where the issue started. Here is the SQL code I got:

INSERT INTO department (dept_id, dept_name)

VALUES

?(1, 'Sales'),

?(2, 'Marketing'),

?(3, 'Engineering'),

?(4, 'Finance'),

?(5, 'Human Resources'),

?(6, 'Customer Service'),

?(7, 'Operations'),

?(8, 'Research and Development'),

?(9, 'Legal'),

?(10, 'Information Technology');

A quick inspection would reveal that code would run in MySQL or SQL Server but not in Oracle.

By the way when I asked Google #BARD to create Oracle SQL for inserting one row, it gave up!

create oracle SQL insert statement for one sample row in table named department with columns dept_id, dept_name

BARD: I'm not trained for coding yet, so I can't help with that right now. I can help you write things, brainstorm ideas, or answer other complex questions. Maybe we can try something like that?

ChatGPT did a fine job for one row in Oracle:

create oracle SQL insert statement for one sample row in table named department with columns dept_id, dept_name

INSERT INTO department (dept_id, dept_name)

VALUES (1, 'Sales');

I did the manual workaround to fix the SQL and make it run for Oracle DB, to insert multiple rows. Look at this site for more details on how INSERT SQL varies across different databases.

I manually created the INSERT SQL for multiple records as below:

INSERT INTO employee (emp_id, emp_name, start_dt, position, comments, dept_id)

WITH e AS (

??SELECT 5, 'John Smith', TO_DATE('2022-01-01', 'YYYY-MM-DD'), 'Manager', 'Joined the company recently', 1 FROM dual UNION ALL

??SELECT 2, 'Jane Doe', TO_DATE('2021-06-01', 'YYYY-MM-DD'), 'Engineer', 'Works on the frontend team', 2 FROM dual UNION ALL

??SELECT 3, 'Bob Johnson', TO_DATE('2020-03-15', 'YYYY-MM-DD'), 'Designer', 'Expert in UI design', 1 FROM dual UNION ALL

??SELECT 4, 'Sara Lee', TO_DATE('2019-09-01', 'YYYY-MM-DD'), 'Engineer', 'Works on the backend team', 2 FROM dual

?)

SELECT * FROM e;

And then executed it in Oracle APEX, worked fine. Then tested it by Selecting the rows for the Employee table. This is how it looked like in Oracle APEX environment:

No alt text provided for this image
Oracle APEX

My conclusion so far is that Generative AI tools like ChatGPT have a good promise but requires human intervention to deal with the differences between different flavors of database. I am yet to test more complex scenarios of SQL, PL/SQL and complex joins.

Please feel free to share you feedback on this topic. Ravi Sharma Arup Nanda Richard Niemiec Y V Ravi Kumar (Oracle Certified Master) Satyendra Pasalapudi? Jennifer Nicholson

Packiyaraj Raja

Database Alchemist | Transforming IT/Pharma Systems with Magic | OracleSQL | ETL PerformanceTuning | Database Management| SQL Developer| PLSQL Developer | Data Modeling| Data Warehousing| Data Mart |Query Optimization

1 年

Great sharing it will help lot for development and we can complete task easy and faster with less effort.

回复
Ravi Sharma

Senior Principal Product Manager at Oracle | PanIIT USA | WHEELS Global Foundation

1 年

Interesting experiments, Shyam. Thanks for sharing. BTW, Just for fun, I think you could work around generating your desired insert statement(s) by slightly tweaking your prompt. For example, if you had prompted it with - “You will now be populating data in the DEPT table. Write SQL statements to insert each of the 10 rows”, if would give you the desired SQL statements that could be used. PS: This is NOT an endorsement of ChatGPT for the purposes of writing SQL! ??

Mohammad Hasan

Market Analysis Expert | Focused on IIoT, Industry 4.0, Digital Twin, Cloud Computing & Data Strategies

1 年

要查看或添加评论,请登录

Shyam Varan Nath的更多文章

  • Generative AI and Hallucinations

    Generative AI and Hallucinations

    In August I had attended the CIO100 event by IDG (International Data Group) and this week attended Oracle Cloudworld…

    20 条评论
  • Getting Ready for Oracle Cloudworld and GenAI?

    Getting Ready for Oracle Cloudworld and GenAI?

    As I am getting ready to be at Oracle Cloudworld (OCW) from Sep 18 to Sep 21. While I will be speaking in 3 different…

    1 条评论
  • A New Operating system for Health Care

    A New Operating system for Health Care

    Recently Deloitte and Oracle published a White Paper titled: "The Future of Work in the Age of Artificial Intelligence"…

  • Driving Competitive Advantage through Oracle Cloud, Edge Computing and AI

    Driving Competitive Advantage through Oracle Cloud, Edge Computing and AI

    I have been experimenting with #ChatGPT and #Google #BARD #AI I asked BARD to summarize our recently released white…

    1 条评论
  • Cloud, Edge Computing and AI

    Cloud, Edge Computing and AI

    We at Deloitte recently wrote a whitepaper (WP) titled "Driving Competitive Advantage through Oracle Cloud, Edge…

  • Oracle Fusion Analytics Warehouse (FAW) with Private Endpoints

    Oracle Fusion Analytics Warehouse (FAW) with Private Endpoints

    Are you using Oracle Fusion Analytics Warehouse (FAW) to create insights for your Oracle Fusion ERP and HCM…

  • Can ChatGPT Write good SQL?

    Can ChatGPT Write good SQL?

    Would you let #ChatGPT write #SQL for you? I asked ChatGPT: write sql to delete duplicate records in Oracle database…

  • What is New Radio (NR) in 5G?

    What is New Radio (NR) in 5G?

    Wondering what is "new" about the "radio" as we start to adopt #5G? New Radio (NR) is the air interface for 5G cellular…

    6 条评论
  • Oracle's Next Quest

    Oracle's Next Quest

    "Our expertise has always been running the hardest, most complex jobs," Barron's quote Oracle CEO Safra Catz, in a…

  • My Distinguished Toastmasters (DTM) Journey

    My Distinguished Toastmasters (DTM) Journey

    My Toastmasters journey started in early 2014. I was a Charter Member of Electric Speakers Toastmasters Club at General…

    1 条评论

社区洞察

其他会员也浏览了