0% found this document useful (0 votes)
9 views

Unit-5 (Creating and Altering Database and Tables (SQL)

Uploaded by

Kunal Kharga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit-5 (Creating and Altering Database and Tables (SQL)

Uploaded by

Kunal Kharga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

UNIT 5: CREATING AND ALTERING Dipesh Koirala

DATABASE AND TABLES(SQL)


OUTLINE
❖ Introduction to SQL
❖ Creating Database with Different Type of Arguments and Alter Database
❖ Creating Normal Tables and Complex Tables with Different Type of
Constrains(Key, Check, Default)
❖ Alter Tables: Adding and Dropping Attributes and Other Constraints
❖ Drop Statement: Table, Database
INTRODUCTION TO SQL
❖ SQL is a database language.
❖ It is designed for managing data in a relational database management system (RDBMS).

❖ 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)

4. Binary Data Types


• BINARY: Fixed-length binary data E.g., column_name BINARY(5) 0x12300
• VARBINARY: Variable-length binary data. E.g., column_name VARBINARY(5) 0x123
• BLOB: Binary Large Objects used for storing large binary data like images e.g., BLOB
DOMAINS
❖ A domain in SQL defines the permissible values for an attribute. It includes the data type and any
constraints that apply to the attribute. Domains help ensure data integrity and consistency.

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);

Syntax: ALTER TABLE tablename DROP COLUMN columnname;


E.g.,: ALTER TABLE Employees DROP COLUMN Position;
DATA DEFINITION LANGUAGE
ALTER
Removing Primary Key Constraint
DATA DEFINITION LANGUAGE
ALTER
Adding a Primary Key
DATA DEFINITION LANGUAGE
ALTER
Removing Unique Constraint
❖ Use ‘UQ’ instead of ‘PK’
DATA DEFINITION LANGUAGE
ALTER
Removing FOREIGN KEY Constraint

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

Adding Check Constraint


DATA DEFINITION LANGUAGE
ALTER
Adding Default Constraint
DATA DEFINITION LANGUAGE
DROP – to delete database objects
Syntax: DROP DATABASE databasename;
Syntax: DROP TABLE tablename;

TRUNCATE – to remove all records from a table, but not the table itself.
Syntax: TRUNCATE TABLE tablename;
END OF UNIT 5 Dipesh Koirala

You might also like