Pertemuan 5 : Pengantar Basis Data - Structured Query Language (SQL)

3 min read 3 hours ago
Published on Oct 09, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial serves as an introduction to Structured Query Language (SQL) and focuses specifically on Data Definition Language (DDL) within the context of databases. Understanding DDL is essential for defining and managing database structures, which is crucial for any data-related project. This guide will provide you with clear and actionable steps to get started with SQL DDL commands.

Step 1: Understanding SQL and DDL

  • SQL is a standard programming language used to manage and manipulate relational databases.
  • Data Definition Language (DDL) is a subset of SQL used to define and manage all database objects, such as tables, indexes, and schemas.
  • Key DDL commands include:
    • CREATE: to create new database objects.
    • ALTER: to modify existing database objects.
    • DROP: to delete database objects.

Step 2: Creating a Database

  • Use the CREATE DATABASE command to set up a new database.
  • Syntax:
    CREATE DATABASE database_name;
    
  • Example:
    CREATE DATABASE SchoolDB;
    
  • Practical Tip: Always choose a meaningful name for your database that reflects its purpose.

Step 3: Creating a Table

  • Use the CREATE TABLE command to define a new table within your database.
  • Syntax:
    CREATE TABLE table_name (
        column1 datatype,
        column2 datatype,
        ...
    );
    
  • Example:
    CREATE TABLE Students (
        StudentID INT PRIMARY KEY,
        FirstName VARCHAR(50),
        LastName VARCHAR(50),
        BirthDate DATE
    );
    
  • Common Pitfall: Always define a primary key for your table to ensure each record can be uniquely identified.

Step 4: Altering a Table

  • Use the ALTER TABLE command to modify an existing table's structure.
  • Syntax to add a column:
    ALTER TABLE table_name ADD column_name datatype;
    
  • Example:
    ALTER TABLE Students ADD Email VARCHAR(100);
    
  • Syntax to drop a column:
    ALTER TABLE table_name DROP COLUMN column_name;
    
  • Example:
    ALTER TABLE Students DROP COLUMN BirthDate;
    
  • Practical Tip: Use the ALTER TABLE command cautiously, as dropping columns can result in data loss.

Step 5: Dropping a Table

  • Use the DROP TABLE command to delete a table and all its data from the database.
  • Syntax:
    DROP TABLE table_name;
    
  • Example:
    DROP TABLE Students;
    
  • Warning: This action is irreversible; ensure you back up any important data before executing this command.

Conclusion

In this tutorial, you learned the basics of SQL DDL, including how to create, alter, and drop databases and tables. Understanding these commands is crucial for effectively managing database structures. As a next step, practice these commands in a database management system (DBMS) to solidify your understanding and explore more complex DDL operations.