0% found this document useful (0 votes)
3 views7 pages

STRING)

The document demonstrates how to create a database and table in MySQL, insert data, and perform various string functions like concatenation, upper/lower case conversion, substring, length, left, right, and trim. It creates a database called VIMAL and a table called SCHOOL to store student data, inserts sample records, and runs queries to select, manipulate and display the data.

Uploaded by

nihalparteti26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

STRING)

The document demonstrates how to create a database and table in MySQL, insert data, and perform various string functions like concatenation, upper/lower case conversion, substring, length, left, right, and trim. It creates a database called VIMAL and a table called SCHOOL to store student data, inserts sample records, and runs queries to select, manipulate and display the data.

Uploaded by

nihalparteti26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

mysql> CREATE DATABASE VIMAL;

Query OK, 1 row affected (0.05 sec)

mysql> SHOW DATABASES;

+--------------------+

| Database |

+--------------------+

| vimal |

+--------------------+

8 rows in set (0.00 sec)

mysql> USE VIMAL;

Database changed

mysql> CREATE TABLE SCHOOL

-> (

-> SR_NO INT PRIMARY KEY,

-> S_NAME VARCHAR(20),

-> CLASS INT,

-> ROLL_NO INT,

-> MARKS INT);

Query OK, 0 rows affected (0.11 sec)

mysql> DESCRIBE SCHOOL;

+---------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| SR_NO | int | NO | PRI | NULL | |

| S_NAME | varchar(20) | YES | | NULL | |

| CLASS | int | YES | | NULL | |


| ROLL_NO | int | YES | | NULL | |

| MARKS | int | YES | | NULL | |

+---------+-------------+------+-----+---------+-------+

5 rows in set (0.01 sec)

mysql> INSERT SCHOOL

-> VALUES(1,'VIMAL',12,5,60),

-> (2,'OM',12,6,55),

-> (3,'SHIVA',12,8,65),

-> (4,'KARAN',11,9,45),

-> (5,'RAM',10,15,50);

Query OK, 5 rows affected (0.02 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> SELECT* FROM SCHOOL;

+-------+--------+-------+---------+-------+

| SR_NO | S_NAME | CLASS | ROLL_NO | MARKS |

+-------+--------+-------+---------+-------+

| 1 | VIMAL | 12 | 5 | 60 |

| 2 | OM | 12 | 6 | 55 |

| 3 | SHIVA | 12 | 8 | 65 |

| 4 | KARAN | 11 | 9 | 45 |

| 5 | RAM | 10 | 15 | 50 |

+-------+--------+-------+---------+-------+

5 rows in set (0.00 sec)

mysql> SELECT S_NAME,CLASS, CONCAT(S_NAME,CLASS)FROM SCHOOL;

+---------+-------+----------------------+

| S_NAME | CLASS | CONCAT(S_NAME,CLASS) |


+---------+-------+----------------------+

| VIMAL | 12 | VIMAL12 |

| OM | 12 | OM12 |

| SHIVA | 12 | SHIVA12 |

| KARAN | 11 | KARAN11 |

| RAM | 10 | RAM10 |

| SHYAM | 9 | SHYAM9 |

| MOHAN | 5 | MOHAN5 |

| KRISHNA | 7 | KRISHNA7 |

| hariom | 10 | hariom10 |

| SHYAM | 10 | SHYAM10 |

+---------+-------+----------------------+

10 rows in set (0.00 sec)

mysql> SELECT S_NAME, LOWER(S_NAME)FROM SCHOOL;

+---------+---------------+

| S_NAME | LOWER(S_NAME) |

+---------+---------------+

| VIMAL | vimal |

| OM | om |

| SHIVA | shiva |

| KARAN | karan |

| RAM | ram |

| SHYAM | shyam |

| MOHAN | mohan |

| KRISHNA | krishna |

| hariom | hariom |

| SHYAM | shyam |

+---------+---------------+

10 rows in set (0.01 sec)


mysql> SELECT S_NAME ,UCASE(S_NAME) FROM SCHOOL;

+---------+---------------+

| S_NAME | UCASE(S_NAME) |

+---------+---------------+

| VIMAL | VIMAL |

| OM | OM |

| SHIVA | SHIVA |

| KARAN | KARAN |

| RAM | RAM |

| SHYAM | SHYAM |

| MOHAN | MOHAN |

| KRISHNA | KRISHNA |

| hariom | HARIOM |

| SHYAM | SHYAM |

+---------+---------------+

10 rows in set (0.00 sec)

mysql> SELECT INSTR('HARIOMBAPUJI','B') FROM DUAL;

+---------------------------+

| INSTR('HARIOMBAPUJI','B') |

+---------------------------+

| 7|

+---------------------------+

1 row in set (0.00 sec)

mysql> SELECT S_NAME, LENGTH(S_NAME)FROM SCHOOL;

+---------+----------------+

| S_NAME | LENGTH(S_NAME) |

+---------+----------------+

| VIMAL | 5|
| OM | 2|

| SHIVA | 5|

| KARAN | 5|

| RAM | 3|

| SHYAM | 5|

| MOHAN | 5|

| KRISHNA | 7|

| hariom | 6|

| SHYAM | 5|

+---------+----------------+

10 rows in set (0.00 sec)

mysql> SELECT S_NAME, LENGTH(S_NAME),CLASS, LENGTH(CLASS)FROM SCHOOL;

+---------+----------------+-------+---------------+

| S_NAME | LENGTH(S_NAME) | CLASS | LENGTH(CLASS) |

+---------+----------------+-------+---------------+

| VIMAL | 5 | 12 | 2|

| OM | 2 | 12 | 2|

| SHIVA | 5 | 12 | 2|

| KARAN | 5 | 11 | 2|

| RAM | 3 | 10 | 2|

| SHYAM | 5| 9| 1|

| MOHAN | 5| 5| 1|

| KRISHNA | 7| 7| 1|

| hariom | 6 | 10 | 2|

| SHYAM | 5 | 10 | 2|

+---------+----------------+-------+---------------+

10 rows in set (0.00 sec)


mysql> SELECT RIGHT('VIMALKUMAR',5)AS 'VIMALKUMAR';

+------------+

| VIMALKUMAR |

+------------+

| KUMAR |

+------------+

1 row in set (0.00 sec)

mysql> SELECT LEFT('VIMALKUMAR',5)AS 'VIMALKUMAR';

+------------+

| VIMALKUMAR |

+------------+

| VIMAL |

+------------+

1 row in set (0.00 sec)

mysql> SELECT SUBSTR('VIMALKUMAR',1,5) AS 'VIMALKUMAR';

+------------+

| VIMALKUMAR |

+------------+

| VIMAL |

+------------+

1 row in set (0.00 sec)

mysql> SELECT LTRIM(' VIMALKUMAR ') AS ' VIMALKUMAR ';

+-----------------+

| VIMALKUMAR |

+-----------------+

| VIMALKUMAR |

+-----------------+

1 row in set, 1 warning (0.00 sec)


mysql> SELECT RTRIM(' VIMALKUMAR ') AS ' VIMALKUMAR ';

+-----------------+

| VIMALKUMAR |

+-----------------+

| VIMALKUMAR |

+-----------------+

1 row in set, 1 warning (0.00 sec)

Common questions

Powered by AI

INSTR returns the position of a specified substring within a string, which can be leveraged to extract or manipulate text data based on known patterns. In the context of the document, INSTR was used to locate the position of 'B' in 'HARIOMBAPUJI', yielding the value 7. This function is pivotal for tasks requiring precise text manipulation, such as format validation or parsing strings for rule-based data transformation processes .

Using SQL string functions can impact database performance by adding computational overhead, particularly when processing large datasets or running complex functions repeatedly. It may slow down query execution times if not optimized. However, they significantly enhance maintainability by allowing in-line data transformations and cleanup, reducing the need for post-processing and simplifying data workflows. This dual impact necessitates careful function application to balance performance efficiency and maintenance simplicity to sustain optimal database operations .

SQL functions for string manipulation, such as CONCAT, LOWER, and LENGTH, prepare data by transforming and standardizing formats, ensuring that names or codes are consistent and thus more interpretable in reports or analyses. They allow customization of data presentation, making it feasible to tailor outputs to specific reporting criteria or analysis models, improving clarity and utility of generated insights. This preparation is foundational for accurate and effective data-driven decision making .

LEFT, RIGHT, and SUBSTR functions differ in their approach to substring extraction by their orientation and specification of the starting point. LEFT extracts a substring from the beginning, RIGHT from the end, and SUBSTR allows specification starting anywhere in the string for a defined length. Each serves particular application needs: LEFT and RIGHT for fixed-format fields like codes, SUBSTR for flexible parsing based on dynamic criteria. These functions enhance string manipulation capabilities, facilitating complex data processing tasks within structured queries .

Normalizing text data with SQL functions such as LOWER, UCASE, and TRIM mitigates inconsistencies by standardizing text input, which is essential for reliable data retrieval and interpretation. By preventing disparities due to casing or extraneous spaces, normalization enhances data comparability and integrity, facilitating more accurate query results and reducing risks in data analytics. This standardization is vital for maintaining a cohesive and error-free dataset, supporting robust database interactions and reflective insights .

String trimming functions like LTRIM and RTRIM remove leading and trailing spaces from strings, respectively. In database management, they clean up input data, eliminating unwanted spaces that often lead to data entry errors and inconsistent query results. This makes database operations more reliable by ensuring that string-based comparisons and stored values are consistent across datasets, improving accuracy and integrity in data processing .

The LENGTH function reveals the size of each student's name in the SCHOOL database, providing insight into the variability and distribution of the data, such as the most common length of names or potential outliers. Understanding data distribution is crucial for designing efficient algorithms and identifying patterns or anomalies that may impact data-driven decisions, as it allows adjustments to be made in database design or data validation processes .

The CONCAT function is used to concatenate two or more strings into a single string. In the SCHOOL database example, it was used to combine the student name (S_NAME) with their class (CLASS), creating a composite identifier for each student's record. This operation helps in generating meaningful labels or records by merging separate columns together without altering original data structure .

Primary keys in the SCHOOL table, such as SR_NO, are crucial for ensuring data integrity by uniquely identifying each record in a database, preventing duplicate entries for students. They enable efficient data retrieval, as queries can leverage the indexed nature of primary keys to quickly access specific records. This characterizes primary keys as a fundamental element in relational database design, improving the reliability and speed of data operations .

String case conversion in SQL can be applied using functions like LOWER or UCASE. In the SCHOOL database, LOWER was used to convert student names to lowercase, which helps ensure uniformity and consistency, especially when comparing strings that may have inconsistent casing. This is particularly beneficial when data needs to be standardized for case-insensitive queries or reporting, reducing the risk of errors due to case mismatches .

You might also like