Assignment 04 Answers

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Assignment:- 04

Data types

Instructions:
Please share your answers filled in line in the Word document. Submit code
separately wherever applicable.

Please ensure you update all the details:


Name: STEPHEN DORAIRAJ VP __Batch ID: _21122023__10am________
Topic: Introduction to Database

1. Create a database with a sales table containing data types like int,
varchar, char ,date, time, timestamp, Boolean, decimal, text ?
2. Insert 10 random values in the table ?
3. Change the data type of the existing column from DECIMAL (10,2) to
FLOAT ?
4. Change the data type of the existing column from Text to Varchar?
5. What is “BLOB” Data Type in SQL, what are different types of BLOB?
6. Write different character data types and different numerical data types?

ANSWERS:
Certainly! Below are SQL queries that perform the tasks you've mentioned:

### 1. Create a database with a 'sales' table:

```sql

© 360DigiTMG. All Rights Reserved.


Assignment:- 04
Data types
-- 1. Create a database
CREATE DATABASE sales_database;

-- 2. Use the created database


USE sales_database;

-- 3. Create a 'sales' table


CREATE TABLE sales (
id INT PRIMARY KEY,
product_name VARCHAR(255),
category CHAR(1),
sale_date DATE,
sale_time TIME,
sale_timestamp TIMESTAMP,
is_available BOOLEAN,
price DECIMAL(10,2),
product_description TEXT
);
```

### 2. Insert 10 random values in the 'sales' table:

© 360DigiTMG. All Rights Reserved.


Assignment:- 04
Data types
```sql
-- Insert 10 random values
INSERT INTO sales VALUES
(1, 'Product A', 'A', '2024-01-01', '12:30:00', '2024-01-01 12:30:00', true, 45.99,
'Description A'),
(2, 'Product B', 'B', '2024-01-02', '14:45:00', '2024-01-02 14:45:00', false, 29.99,
'Description B'),
-- Add 8 more rows with random values
...;
```

### 3. Change the data type of the existing column from DECIMAL (10,2) to
FLOAT:

```sql
-- Change data type from DECIMAL(10,2) to FLOAT
ALTER TABLE sales
MODIFY COLUMN price FLOAT;
```

### 4. Change the data type of the existing column from Text to Varchar:

```sql
© 360DigiTMG. All Rights Reserved.
Assignment:- 04
Data types
-- Change data type from TEXT to VARCHAR
ALTER TABLE sales
MODIFY COLUMN product_description VARCHAR(255);
```

### 5. "BLOB" Data Type in SQL:

- **BLOB (Binary Large Object):** It is a data type for storing binary data, such
as images, audio, or other multimedia objects.
- **Types of BLOB:**
- **TINYBLOB:** Up to 255 bytes
- **BLOB:** Up to 65,535 bytes
- **MEDIUMBLOB:** Up to 16,777,215 bytes
- **LONGBLOB:** Up to 4 GB

### 6. Different Character and Numerical Data Types:

**Character Data Types:**


- **CHAR(n):** Fixed-length character string.
- **VARCHAR(n):** Variable-length character string with a maximum length of
'n'.
- **TEXT:** Variable-length character string with no specified maximum length.

© 360DigiTMG. All Rights Reserved.


Assignment:- 04
Data types
**Numerical Data Types:**
- **INT:** Integer data type.
- **FLOAT:** Floating-point numeric data type.
- **DECIMAL(p, s):** Decimal data type with precision 'p' and scale 's'.
- **BOOLEAN:** Boolean data type representing true or false values.

© 360DigiTMG. All Rights Reserved.

You might also like