...

Database and Table Management

Database and Table Management

Database and Table Management involves the creation, modification, and maintenance of databases and their respective tables to ensure efficient storage, retrieval, and manipulation of data. It includes tasks such as defining table schemas, indexing, setting up relationships, and ensuring data integrity and security.

 
 
  /*CREATE DATABASE - Creates a new database.*/
CREATE DATABASE database_name;

/*CREATE TABLE - Creates a new table in a database.*/
CREATE TABLE table_name 
(
    column1 datatype,
    column2 datatype,
    ...   
);

/*ALTER TABLE - Modifies an existing table structure*/.
ALTER TABLE table_name 
ADD column_name datatype;

/*DROP TABLE - Deletes a table from a database.*/
DROP TABLE table_name;

/*TRUNCATE TABLE - Removes all records from a table.*/
TRUNCATE TABLE table_name;

/*CREATE INDEX - Creates an index on a table.*/
CREATE INDEX index_name ON table_name (column_name);

/* DROP INDEX - Deletes an index from a table.*/
DROP INDEX index_name; ON Table_Name

/*Examples with Multiple Constraints
Example 1: Creating a Table with Various Constraints */
CREATE TABLE Employees 
(
    EmployeeID int PRIMARY KEY,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255) NOT NULL,
    Age int CHECK (Age >= 18),
    Email varchar(255) UNIQUE,
    DepartmentID int,
    FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

/*Example 2: Adding Constraints to Existing Tables
Adding a Primary Key*/

ALTER TABLE table_name
ADD PRIMARY KEY (column_name);

/*Adding a Foreign Key*/
ALTER TABLE table_name
ADD FOREIGN KEY (column_name) REFERENCES other_table(column_name);

/*Adding a Unique Constraint*/
ALTER TABLE table_name
ADD UNIQUE (column_name);

/*Adding a Not Null Constraint*/
ALTER TABLE table_name
ALTER column column_name datatype NOT NULL;

/*Adding a Check Constraint*/
ALTER TABLE table_name
ADD CHECK (condition including column name ); E.g. (Column_Name > 0)

/*Adding a Default Constraint*/
ALTER TABLE table_name
ADD Constraint Constraint  _Name  DEFAULT 1 for  default_ColumnName;

ALTER TABLE table_name
ADD Defaut 1 For Column_Name

/*Example 2: Dropping Constraints
Dropping a Primary Key*/
ALTER TABLE table_name
DROP Constraint PRIMARY KEY_Name;

/*Dropping a Foreign Key*/
ALTER TABLE table_name
DROP Constraint  FOREIGN KEY fk_name;

/*Dropping a Unique Constraint*/
ALTER TABLE table_name
DROP Constraint  UNIQUEKEY_name;

/*Dropping a Not Null Constraint*/
ALTER TABLE table_name
Alter column column_name datatype NULL;

/*Dropping a Check Constraint*/
ALTER TABLE table_name
DROP Constraint   CHECKCONSTRAINT_name;

/*Dropping a Default Constraint*/
ALTER TABLE table_name
DROP Constraint  DEFAULTCONSTRAINT_Name;
  
Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow Us On Social Media

Categories

Subscribe To Our Newsletter
Enter your email to receive a email notification on new Post.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.