MySQL - MODULE 1
MySQL - MODULE 1
MySQL - MODULE 1
use B23CSE1
MAC
/usr/local/mysql/bin/mysql -h 128.66.203.247 -u
B23CSE1 -psumo@123
use B23CSE1
Data Types
USE your_database_name;
SHOW TABLES;
Example:
USE mydatabase;
SHOW TABLES;
DROP DATABASE
Example:
NOTE: This example drops the database named mydatabase. If the database
doesn't exist, it won't raise an error.
CREATE TABLE
id INT,
name VARCHAR(50),
age INT,
birthdate DATE
);
CREATE TABLE
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT,
);
Constraints are rules applied to a column
or a set of columns in a table to enforce
Creating Tables with Constraints:
data integrity. Common constraints
CREATE TABLE students ( include Primary Key, Foreign Key, Unique
student_id INT PRIMARY KEY, Key, Not Null, and Null.
student_name VARCHAR(50) NOT NULL,
date_of_birth DATE,
UNIQUE (student_name)
);
FROM table_name
WHERE condition;
FROM employees
WHERE salary > 50000;
Delete Table Rows:
EXAMPLE:
The TRUNCATE statement is used to remove all the records from a table, but it
retains the table structure for future use.
EXAMPLE:
EXAMPLE:
CREATE TABLE employees_copy AS
SELECT * FROM employees;
RENAME statement
EXAMPLE:
In MySQL, the DELETE statement is used to delete records from a table. The
basic syntax is as follows:
WHERE condition;
Suppose you have a table named employees and you want to delete all records
where the department is 'HR':
desc employee_details;
EXAMPLE
CREATE TABLE students (
student_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
date_of_birth DATE,
email VARCHAR(100) UNIQUE,
grade INT CHECK (grade >= 0 AND grade <= 100)
);
EXAMPLE
In this example:
student_id is the primary key, ensuring each student has a unique identifier.
first_name and last_name are marked as NOT NULL, ensuring that these
columns cannot contain null values.
email is marked as UNIQUE, ensuring that each email address is unique across
all records.
grade has a CHECK constraint, ensuring that the grade is between 0 and 100.
EXAMPLE
If you want to alter constraints on the age column in an existing table named
employee_details, you can use the ALTER TABLE statement. Here's an example:
FROM Customers;
NOT NULL Constraint
NOTE: DECIMAL(10, 2) means that the salary column can store numeric values with a
total of 10 digits, with 2 digits allowed after the decimal point. For example, it can store
values like 12345.67 or 9876.54.
ALTER TABLE statement to rename a column
In MySQL, you can use the ALTER TABLE statement to rename a column. The syntax is
as follows:
data_type: The data type of the column (optional if you're only changing the column name).
EXAMPLE: ALTER TABLE statement to rename a column
To rename two or more columns in MySQL, you can use the ALTER TABLE
statement with multiple CHANGE COLUMN clauses. Here's the syntax:
...;
EXAMPLE: Rename two or more than two columns.
ALTER is a SQL command used to modify the structure of a table. It allows you to
add, modify, or delete columns in an existing table.
In MySQL, when you define a constraint, you can explicitly specify its name. This is
particularly useful when you want to have more control over the constraint's name for
easier identification, management, or when you're working with foreign key constraints.
• For example, if we have a table schema that stores student information, an instance would be the data
stored in the table, i.e., all students' names, roll numbers, email addresses, etc.
DATABASE STATE: CURRENT STATE OF DATABASE
Schema
Conceptual Schema
Internal Schema