Thursday, December 19, 2024

SQL TABLE Keyword

 In SQL, the TABLE keyword is commonly used in various contexts, typically in relation to defining, querying, and modifying tables. Here are some key ways the TABLE keyword is used:

1. CREATE TABLE:

Used to create a new table in a database.

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    column3 datatype constraints
);

2. DROP TABLE:

Used to delete an existing table and all of its data.

DROP TABLE table_name;

3. ALTER TABLE:

Used to modify an existing table, such as adding, deleting, or modifying columns.

ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

4. SELECT FROM:

Used to query data from a table.

SELECT * FROM table_name;

5. DESCRIBE (or EXPLAIN) TABLE:

This command shows the structure of a table, including columns, data types, and constraints.

DESCRIBE table_name;

6. TEMPORARY TABLE:

In some databases, you can create temporary tables that only exist during the session.

CREATE TEMPORARY TABLE temp_table_name (
    column1 datatype,
    column2 datatype
);

7. INSERT INTO TABLE:

Used to insert data into a table.

INSERT INTO table_name (column1, column2) VALUES (value1, value2);

8. SELECT INTO:

Creates a new table and inserts data into it from another table.

SELECT * INTO new_table FROM existing_table;

9. REFERENCES (Foreign Key):

Used in the context of defining foreign keys that reference another table.

CREATE TABLE child_table (
    id INT,
    parent_id INT,
    FOREIGN KEY (parent_id) REFERENCES parent_table(id)
);

In summary, the TABLE keyword is mainly associated with creating, altering, and querying tables in a database.

No comments:

Post a Comment