SQL on AS400 (now IBM i) is a powerful way to interact with your data. It's deeply integrated into the system and offers a lot of flexibility. Here's how it works:
1. DB2 for i :
- Integrated Database: The database system on IBM i is called DB2 for i. It's a core part of the operating system, not a separate add-on. This tight integration means that SQL is deeply woven into how you work with data on the system.
2. Accessing Data :
- Tables: In SQL, data is organized in tables, which are similar to files in the traditional AS400 file system.
- SQL Statements: You use SQL statements to perform actions on these tables:
SELECT: Retrieve data
INSERT: Add new data
UPDATE: Modify existing data
DELETE: Remove data
CREATE: Define tables and other database objects
DROP: Delete tables and other database objects
3. Running SQL :
You can run SQL in several ways on IBM i:
- Interactive SQL:
- STRSQL: This command starts an interactive SQL session where you can type and execute SQL statements directly. It's great for quick queries and testing.
- Query Manager: This tool provides a user-friendly interface for creating, storing, and running SQL queries, even if you're not an SQL expert.
- Embedded SQL:
- Within Programs: You can embed SQL statements directly into programs written in languages like RPG, COBOL, or C. This lets your programs interact with the database seamlessly.
- Static SQL: The SQL statements are fixed within the program.
- Dynamic SQL: The SQL statements can be built or modified at runtime, providing more flexibility.
- ODBC/JDBC:
- External Access: You can connect to the DB2 for i database from external applications using standard interfaces like ODBC (Open Database Connectivity) and JDBC (Java Database Connectivity). This allows tools like Microsoft Excel or Java applications to access and manipulate data on your IBM i.
4. Key Features :
- Powerful Querying: SQL provides a rich set of features for querying data, including:
WHERE clause for filtering data
JOIN operations for combining data from multiple tables
GROUP BY and aggregate functions for summarizing data
ORDER BY for sorting data
- Data Integrity: DB2 for i supports features like:
- Transactions to ensure data consistency
- Constraints to enforce rules about the data
- Security features to control access to data
- Performance: DB2 for i is designed for performance, with features like:
- Query optimization to find the most efficient way to execute queries
- Indexing to speed up data retrieval
5. Working with AS400 Objects :
- Seamless Integration: SQL works seamlessly with traditional AS400 objects like physical files and logical files. You can use SQL to query and manipulate data in these files.
- Views: You can create views using SQL to define logical subsets of data from one or more tables. This simplifies complex queries and provides a way to present data in a specific way.