Code Monkey home page Code Monkey logo

dbms-expt02's Introduction

DBMS-Expt02

Aim

To study and implement DDL commands and different types of constraints.

Theory

  1. CREATE: This is used to create a new relation (table)

Syntax: CREATE TABLE (field_1 data_type(size),field_2 data_type(size), .. . );

  1. ALTER: This is used to add some extra fields into existing relation.

Syntax: ALTER TABLE relation_name ADD (new field_1 data_type(size) );

(a)ALTER TABLE ...ADD...: ALTER TABLE std ADD (Address CHAR(10));

(b )ALTER TABLE...MODIFY...: ALTER TABLE relation_name MODIFY (field_1 newdata_type(Size)

( c) ALTER TABLE..DROP.... ALTER TABLE relation_name DROP COLUMN (field_name);

(d)ALTER TABLE..RENAME...: ALTER TABLE relation_name RENAME COLUMN (OLD field_name) to (NEW field_name);

  1. DROP TABLE: This is used to delete the structure of a relation. It permanently deletes the records in the table.

Syntax: DROP TABLE relation_name;

4.RENAME: It is used to modify the name of the existing database object.

Syntax: RENAME TABLE old_relation_name TO new_relation_name;

CONSTRAINTS: Constraints are used to specify rules for the data in a table. If there is any violation between the constraint and the data action, the action is aborted by the constraint. It can be specified when the table is created (using CREATE TABLE statement) or after the table is created (using ALTER TABLE statement).

  1. NOT NULL: When a column is defined as NOTNULL, then that column becomes a mandatory column. It implies that a value must be entered into the column if the record is to be accepted for storage in the table.

Syntax: CREATE TABLE Table_Name (column_name data_type (size) NOT NULL, );

  1. UNIQUE: The purpose of a unique key is to ensure that information in the column(s) is unique i.e. a value entered in column(s) defined in the unique constraint must not be repeated across the column(s). A table may have many unique keys.

Syntax: CREATE TABLE Table_Name(column_name data_type(size) UNIQUE, ….); 3. CHECK: Specifies a condition that each row in the table must satisfy. To satisfy the constraint, each row in the table must make the condition either TRUE or unknown (due to a null).

Syntax: CREATE TABLE Table_Name(column_name data_type(size) CHECK(logical expression), ….);

  1. PRIMARY KEY: A field which is used to identify a record uniquely. A column or combination of columns can be created as primary key, which can be used as a reference from other tables. A table contains primary key is known as Master Table.
    It must uniquely identify each record in a table.
    It must contain unique values.
    It cannot be a null field. It cannot be a multi port field.
    It should contain a minimum number of fields necessary to be called unique.

Syntax: CREATE TABLE Table_Name(column_name data_type(size) PRIMARY KEY, ….);

  1. FOREIGN KEY: It is a table level constraint. We cannot add this at column level. To reference any primary key column from other table this constraint can be used. The table in which the foreign key is defined is called a detail table. The table that defines the primary key and is referenced by the foreign key is called the master table.

Syntax: CREATE TABLE Table_Name(column_name data_type(size) FOREIGN KEY(column_name) REFERENCES table_name);

  1. DEFAULT : The DEFAULT constraint is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified.

Syntax: CREATE TABLE Table_Name(col_name1,col_name2,col_name3 DEFAULT ‘’);

1.Create a table name as "Students" with the following attributes

Screenshot 2024-04-23 185111

Query

CREATE TABLE Students (
    id integer PRIMARY KEY AUTOINCREMENT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email VARCHAR(100) UNIQUE,
    birth_date DATE
);

Output

Screenshot 2024-04-23 185306

2.Write a SQL Query for creating a table name as "Students" with the following attributes

Screenshot 2024-04-24 131025

Query

CREATE TABLE Students (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name varchar(20) NULL,
    address text,
    grades varchar(10),
    phone numeric not null
);
);

Output

Screenshot 2024-04-24 131058

3.Write a SQL query for creating a table named "Orders" with the following attributes:

OrderID as INTEGER autoincrement not null (Specify OrderID as the Primary Key). OrderNumber as INTEGER. ProductName as Varchar(30) Manufacturename as varchar(30) PersonID as INTEGER not null (Specify PersonID as a Foreign Key referencing the table "Person"). Screenshot 2024-04-24 131220

Query

CREATE TABLE Orders (
    OrderID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    OrderNumber INTEGER,
    ProductName Varchar(30),
    Manufacturename varchar(30),
    PersonID INTEGER NOT NULL,
    FOREIGN KEY (PersonID) REFERENCES Person(PersonID)
);

Output

Screenshot 2024-04-24 131310

4.Create a table name as "Students" with the following attributes

Screenshot 2024-04-24 132306

rollno as integeprimarykey with autoincrement stu_name as varchar(50) year as varchar(30) parentsoccupation as varchar(30) address as varchar(30) Screenshot 2024-04-24 132332

Query

CREATE TABLE Students (
    rollno INTEGER PRIMARY KEY AUTOINCREMENT,
    stu_name VARCHAR(50),
    year VARCHAR(30),
    parents_occupation VARCHAR(30),
    address VARCHAR(30)
);

Output

Screenshot 2024-04-24 132352

5.Write a SQL query to Add a new column Mobilenumber as number in the Student_details table.

Screenshot 2024-04-24 141706

Query

ALTER TABLE Student_details
ADD COLUMN Mobilenumber number;

Output

Screenshot 2024-04-24 141742

6.Write a SQL Query to Add attribute "birth_date" in the table employee

Screenshot 2024-04-24 141835

Query

ALTER TABLE employee
ADD COLUMN birth_date date;

Output

Screenshot 2024-04-24 141910

7.Write a SQL query to Add a new column named "discount" with the data type DECIMAL(5,2) to the "customer" table.

Screenshot 2024-04-24 142025

Query

ALTER TABLE customer
ADD COLUMN discount DECIMAL(5,2);

Output

Screenshot 2024-04-24 142100

8.Write an SQL query to insert values into the Student_database table. Set 'rollno' as the primary key with auto-increment, specify other attributes appropriately, and set the 'mark' attribute to null.

Screenshot 2024-04-24 142231

Query

INSERT INTO Student_database (fname, lname, Gender, Subject, MARKS)
VALUES 
    ('Emma', 'Johnson', 'Female', 'Physics', NULL),
    ('Samuel', 'Davis', 'Male', 'Chemistry', NULL),
    ('Ethan', 'Moore', 'Male', 'History', NULL),
    ('Olivia', 'Robinson', 'Female', 'ComputerScience', NULL),
    ('Mason', 'Clark', 'Male', 'English', NULL);

Output

Screenshot 2024-04-24 142308

9.Write a SQL Query for inserting the below values as multiple row format in the table "Student_database"

Screenshot 2024-04-24 142450

Query

INSERT INTO Student_database (fname, lname, Gender, Subject, MARKS) 
VALUES 
    ('Emma', 'Johnson', 'Female', 'Physics', 95),
    ('Samuel', 'Davis', 'Male', 'Chemistry', 82),
    ('Ethan', 'Moore', 'Male', 'History', 78),
    ('Olivia', 'Robinson', 'Female', 'ComputerScience', 92),
    ('Mason', 'Clark', 'Male', 'English', 85);

Output

Screenshot 2024-04-24 142537

10.Write a SQL query for inserting new customer details (customer_id, name, email) from the 'old_customers' table into the 'new_customers' table with the subquery. Ensure that only customers name as "James' are included in the transfer.

Screenshot 2024-04-24 142634

Query

INSERT INTO new_customers (customer_id, name, email)
SELECT customer_id, name, email
FROM old_customers
WHERE name = 'James';

Output

Screenshot 2024-04-24 142657

Result!

Thus , to study and implement DDL commands and different types of constraints.

dbms-expt02's People

Contributors

harsayazheni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.