Unit-5 (Creating and Altering Database and Tables (SQL)
Unit-5 (Creating and Altering Database and Tables (SQL)
❖ SQL is a comprehensive database language: It has statements for data definitions, queries, and
updates
❖ It is used for database creation, deletion, fetching rows and modifying rows, etc.
❖ All DBMS like Oracle, SQL Server, MySQL use SQL as standard database language.
DATA DEFINITION LANGUAGE
CREATE: Creates database and table.
Syntax:
CREATE DATABASE database-name;
DATA DEFINITION LANGUAGE
CREATE (For tables)
Syntax: CREATE TABLE tablename (
column1 datatype,
column2 datatype,
……..
);
ATTRIBUTE DATA TYPES
❖ Data types specify the kind of data that can be stored in a column.
1. Numeric Data Types:
• INT: Integer values e.g., column_name INT
• Float: Floating-point numbers e.g., column_name FLOAT
• DECIMAL: Fixed-point numbers e.g., column_name DECIMAL(10, 2)
2. String Data Types
• CHAR: Fixed-length character strings. e.g., column_name CHAR(10)
• VARCHAR: Variable-length character strings. E.g., column_name VARCHAR(10)
• TEXT: Large text data. E.g., column_name TEXT
ATTRIBUTE DATA TYPES
3. Date and Time Data Types
• DATE: Date values. E.g., column_name DATE
• TIME: Time values. E.g., column_name TIME
• DATETIME: Combined date and time values. E.g., column_name DATETIME (2024-09-02 14:30:00)
1.Data Type: Specifies the type of data that can be stored (e.g., integer, string, date).
2.Constraints: Rules that restrict the values that can be stored. Common constraints include:
• NOT NULL: Ensures that a column cannot have a NULL value.
• CHECK: Ensures that values meet a specific condition.
• UNIQUE: Ensures that all values in a column are unique.
• PRIMARY KEY: Uniquely identifies each row in a table.
• FOREIGN KEY: Ensures referential integrity between tables
CREATING TABLE WITH DOMAINS
❖ Example
DATA DEFINITION LANGUAGE
Create table with foreign key
DATA DEFINITION LANGUAGE
Create Table (Key, Check, Default)
DATA DEFINITION LANGUAGE
ALTER
Syntax: ALTER TABLE tablename ADD columnname type;
E.g.,: ALTER TABLE Employees ADD Salary DECIMAL(10, 2);
OR
DATA DEFINITION LANGUAGE
ALTER
Adding FOREIGN KEY Constraint
E.g.
DATA DEFINITION LANGUAGE
ALTER
Dropping a constraint
DATA DEFINITION LANGUAGE
ALTER
Modifying a constraint
DATA DEFINITION LANGUAGE
ALTER
Adding Not Null Constraint
TRUNCATE – to remove all records from a table, but not the table itself.
Syntax: TRUNCATE TABLE tablename;
END OF UNIT 5 Dipesh Koirala