0% found this document useful (0 votes)
7 views3 pages

CH 2

MySQL is an open-source relational database management system that uses SQL for database creation and management, organizing data in tables defined by schemas. It classifies commands into Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL), with various commands for creating, modifying, and managing data. The document also covers data types, how to create and modify databases and tables, and the use of constraints and commands for viewing and deleting table structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

CH 2

MySQL is an open-source relational database management system that uses SQL for database creation and management, organizing data in tables defined by schemas. It classifies commands into Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL), with various commands for creating, modifying, and managing data. The document also covers data types, how to create and modify databases and tables, and the use of constraints and commands for viewing and deleting table structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CH 2.

Introduction to MYSQL
1. Introduction To MYSQL
MySQL is an open source RDBMS that uses SQL to create and manage databases. As a relational database, MySQL
stores data in tables of rows and columns organized into schemas. A schema defines how data is organized and
stored and describes the relationship among various tables.
2. Classification of MYSQL commands (DDL, DML)
MySQL commands are classified into three main types based on their purpose in the database:
Data Definition Language (DDL)
Used to define the database schema, including creating, modifying, and deleting database structures
Data Manipulation Language (DML)
Used to manipulate the data within the database, including creating, reading, updating, and deleting data
Data Control Language (DCL)
Used to control access to the database and implement security on database objects
Here are some examples of DDL and DML commands:
DDL
CREATE: Creates a new table, index, function, view, store procedure, or trigger
ALTER: Modifies an existing table, such as adding a new column or changing a data type
DROP: Permanently deletes an existing database, table, or trigger
TRUNCATE: Deletes all records from a table, including all spaces allocated for the records
RENAME: Renames an existing object in the database
DML
SELECT: Used to conduct data analysis or create new tables and views
INSERT: Used to create data
UPDATE: Used to update data
DELETE: Used to delete data

3. Data Types in MYSQL (char, varchar, decimal, int, date, time)


MySQL supports many data types, including character, numeric, date and time, and spatial data types:
• Character data types
o char(n): A fixed-width character string with a maximum size of 8,000 characters
o varchar(n): A variable-width character string with a maximum size of 8,000 characters
o varchar(max): A variable-width character string with a maximum size of 1,073,741,824 characters
o text: A variable-width character string with a maximum size of 2 GB
• Numeric data types
o INT: An integer data type
o TINYINT: A numeric data type
o SMALLINT: A numeric data type
o MEDIUMINT: A numeric data type
o BIGINT: A numeric data type
o DECIMAL: A numeric data type
o NUMERIC: A numeric data type
o FLOAT: A numeric data type
o DOUBLE: A numeric data type
o BIT: A numeric data type
• Date and time data types
o DATE: A date data type
o DATETIME: A date and time data type
o TIMESTAMP: A date and time data type
o TIME: A time data type
o YEAR: A year data type
4. How to create a database?
Create a database without using a template
On the File tab, click New, and then click Blank Database.
Type a file name in the File Name box. ...
Click Create. ...
Begin typing to add data, or you can paste data from another source, as described in the section Copy data from
another source into an Access table.
5. How to Create table?
SQL CREATE TABLE Statement
1. CREATE TABLE table_name ( column1 datatype, column2 datatype, ...
2. ExampleGet your own SQL Server. CREATE TABLE Persons ( PersonID int, ...
3. CREATE TABLE new_table_name AS. SELECT column1, column2,... FROM existing_table_name. ...
4. Example. CREATE TABLE TestTable AS. SELECT customername, contactname.

6. View structure of a table?


In a relational database management system (RDBMS), a table's structure is made up of rows and columns:
• Columns
The vertical component of a table, each with a name and a specific data type, such as character, decimal, or
integer
• Rows
The horizontal component of a table, consisting of a sequence of values, one for each column
In RDBMS, views are virtual tables that allow users to see a subset of the actual data. They are only a
structure, and contain no data. The fields in a view are fields from one or more real tables in the database.
Here are some ways to view a table's structure in RDBMS:
• SQL Server Management Studio: Select the table in Object Explorer, right-click, and select Properties
from the shortcut menu
• Transact-SQL: Use sp_help to return all column information for the specified object
• Design view: Open a table in Design view to see the table's structure in detail
7. How to Add constraints in table?
To add a constraint to a table in RDBMS, you can use the ADD CONSTRAINT command in SQL. The basic syntax for
this is:
Code
ALTER TABLE table_name ADD CONSTRAINT PRIMARY KEY (col1, col2);
Here are some types of constraints you can add to a table:
• UNIQUE constraint
Prevents duplicate values in a column. You can declare multiple columns as UNIQUE in a table.
• Check constraint
Specifies rules for the contents of a table. A search condition must be satisfied for all rows in the table.
• DEFAULT constraint
Automatically inserts a specified default value when no other value is provided.
• Primary key constraint
Can be added to an existing table. The constraint name must be unique within the table.
• Foreign key constraint
Defines the relationship between two tables. A single table can be related to more than one table in the
database.

8. Modify structure?
To modify the structure of a table in SQL, you can use the ALTER TABLE command:
Add a column: Use the syntax ALTER TABLE table_name ADD column_name data_type. The new column is added
to the end of the existing columns.
Change a column: Use the CHANGE clause to rename a column or change its data type. For example, you can
change a column from STRING to TIMESTAMP.
Add or remove constraints: You can add or remove constraints on an existing table.
Declare a default value: You can declare a default value for a field.
Rename a table: You can rename the table itself.
Altering a table's structure can cause data loss or rounding errors. It can also break other parts of your application
that may refer to the changed field.
9. Show all tables created in a database

Learn more…Opens in new tab


Listen
To show all tables in a database in SQL, you can use the following queries:
• SQL Server: SELECT * FROM INFORMATION_SCHEMA.TABLES; GO
• Oracle: SELECT table_name FROM dba_tables
• MySQL: SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE'
You can also filter out views from the results by adding a WHERE TABLE_TYPE = 'BASE TABLE' clause.
You can also use a GUI tool like dbForge Studio to display tables in a database:

MySQL
Open the Command Prompt, navigate to the bin folder, and run mysqlshow -u user -p. You
can also use the SHOW TABLES command in dbForge Studio.

SQL Server
Select the desired database in the left pane, expand the database, and then expand the
Tables folder

10. Delete structure


To delete the structure of a table in SQL, you can use the DROP TABLE statement: DROP TABLE table_name.
This statement deletes the table and all of its rows. For example, to drop the table named "Shippers", you would
use the statement DROP TABLE Shippers.
You can also use the TRUNCATE TABLE statement to delete the data inside a table, but not the table itself.
In SQL Server Management Studio, you can also delete a table by:
1. Selecting the table you want to delete in Object Explorer
2. Right-clicking the table and choosing Delete from the shortcut menu
3. Clicking Yes in the message box to confirm the deletion
Deleting a table in SQL automatically removes any relationships to it.

You might also like