Intern
Are you familiar with SQL?
sobes.tech AI
Answer from AI
SQL (Structured Query Language) is a declarative query language used for interacting with relational databases. It allows managing data: creating, modifying, querying, and deleting.
Main categories of SQL commands:
- DDL (Data Definition Language): For defining the database structure.
CREATE(to create objects: tables, indexes, etc.)ALTER(to modify object structures)DROP(to delete objects)TRUNCATE(to quickly delete all records from a table)
- DML (Data Manipulation Language): For managing data within database objects.
SELECT(to retrieve data)INSERT(to add data)UPDATE(to modify existing data)DELETE(to delete data)
- DCL (Data Control Language): For managing access and permissions.
GRANT(to grant rights)REVOKE(to revoke rights)
- TCL (Transaction Control Language): For managing transactions.
COMMIT(to commit a transaction)ROLLBACK(to rollback a transaction)SAVEPOINT(to set save points)
Examples of usage:
-- Select all records from the 'customers' table
SELECT * FROM customers;
-- Select names and emails of customers over 18 years old
SELECT name, email
FROM customers
WHERE age > 18;
-- Insert a new record into the 'products' table
INSERT INTO products (name, price)
VALUES ('Laptop', 1200.00);
-- Update the price for the product with ID 10
UPDATE products
SET price = 1250.00
WHERE id = 10;
-- Delete customer with ID 5
DELETE FROM customers
WHERE id = 5;
-- Create a new 'orders' table
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
total_amount DECIMAL(10, 2)
);
In Java development, SQL is often used through APIs such as JDBC (Java Database Connectivity) or ORM frameworks (Object-Relational Mapping) like Hibernate or JPA, which abstract direct native SQL writing or provide their own query language (e.g., HQL or JPQL), which is then translated into SQL.
// Example of using JDBC
// Connection conn = null;
// Statement stmt = null;
// ResultSet rs = null;
// try {
// conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "user", "password");
// stmt = conn.createStatement();
// rs = stmt.executeQuery("SELECT * FROM customers");
// while (rs.next()) {
// // Data processing
// System.out.println(rs.getString("name"));
// }
// } catch (SQLException e) {
// e.printStackTrace();
// } finally {
// // Closing resources
// }
Knowledge of SQL is essential for effective interaction with databases, understanding ORM operations, and query optimization.