Lab Work Session-3
Lab Work Session-3
2.To use the created data base and creation of table in the selected data use the ‘use database
name’ command and create a table using Create command:
use CDAC;
3.To fetch the table and its structure use the ‘Select’ command
Syntax: INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Create new User named ‘dbda’, Grant all the privileges and Perform following Queries.
Create Table ‘Books’ using proper data types which contain columns (name, author,price, writer)
Describe test;
5. The SQL DROP DATABASE statement is used to delete an existing database along
with all the data such as tables, views, indexes, stored procedures, and constraints.
DROP DATABASE DatabaseName;
You can drop multiple databases using the SQL DROP
DROP DATABASE testDB3, testDB4;
6. verify whether the databases are created or not
SHOW DATABASES;
To show the list of tables firstly select the database:
use cdac;
SHOW TABLES;
7. To Rename the table in database:
Syntax: RENAME TABLE oldTable_name to New_Tablename;
Ex: RENAME TABLE test to employe;
8. To add any new colum in the table using ALTER command
Syntax: ALTER TABLE table_name
ADD field_name datatype;
select *from employe;
Example: ALTER TABLE employe
ADD Email varchar(255);
select *from employe;
9. ALTER TABLE - DROP COLUMN from a table:
Syntax: ALTER TABLE table_name
DROP COLUMN column_name;
Example: ALTER TABLE employe
DROP COLUMN Email;
10. ALTER TABLE - RENAME COLUMN in a table:
Synatx: ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
Example: ALTER TABLE employe
RENAME COLUMN address to mobile;
11. ALTER TABLE - ALTER/MODIFY DATATYPE: To change the data type of a column in a
table:
Syntax: ALTER TABLE table_name
ALTER COLUMN column_name new_datatype;
Example: ALTER TABLE employe
MODIFY COLUMN age varchar(10);
12. SQL Constraints
SQL constraints are used to specify rules for the data in a table.
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL value
Case 1: At the time of Table creation:
CREATE TABLE course (ID int NOT NULL, LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL, Age int);
Case 2: if table is created then we can also apply constraints on it
Syntax: ALTER TABLE Tabele_Name
MODIFY COLUMN col_name datatype constraint;
Example: ALTER TABLE course
MODIFY COLUMN Age int NOT NULL;
UNIQUE - Ensures that all values in a column are different
Case 1: On single column field:
CREATE TABLE course ( ID int NOT NULL, LastName varchar(255) NOT NULL,
FirstName varchar(255), Age int, UNIQUE (ID));
Case 2: to define a UNIQUE constraint on multiple columns, use the
following SQL syntax:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UNIQUE (ID,LastName)
);
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table.
CREATE TABLE course (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
If table is created then:
ALTER TABLE course
ADD PRIMARY KEY (ID);
FOREIGN KEY - Prevents actions that would destroy links between tables
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
CHECK - Ensures that the values in a column satisfies a specific condition
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
CREATE INDEX - Used to create and retrieve data from the database very quickly
Syntax: CREATE INDEX ind_name
ON Table_name (col_name);
Example: CREATE INDEX ind_name
ON employe (name);
13.