DBMS 04
DBMS 04
CREATE: This command is used to create a new database, table, view, or stored procedure. The syntax for creating
a new table is as follows:
SYNTAX:-
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY (one or more columns)
);
ALTER: This command is used to modify the structure of an existing database object. The syntax for altering a table
is as follows:
SYNTAX:-
ALTER TABLE table_name
ADD column_name datatype;
DROP: This command is used to delete an existing database object. The syntax for dropping a table is as follows:
SYNTAX:-
DROP TABLE table_name;
TRUNCATE: This command is used to delete all the data from a table without deleting the table structure. The
syntax for truncating a table is as follows:
SYNTAX:-
TRUNCATE TABLE table_name;
In MySQL, integrity constraints are used to enforce rules that ensure the consistency and accuracy of the data stored
in a database. There are four types of integrity constraints in MySQL:
Primary Key Constraint: A primary key is a unique identifier for each row in a table. It is used to enforce the
uniqueness of each row and to prevent duplicate rows from being inserted into a table. A primary key constraint is
created by defining a column or a combination of columns as the primary key using the PRIMARY KEY keyword.
Foreign Key Constraint: A foreign key is a column or a combination of columns that references the primary key of
another table. It is used to establish a relationship between two tables and to enforce referential integrity. A foreign
key constraint is created by defining a column or a combination of columns as a foreign key using the FOREIGN KEY
keyword and specifying the referenced table and column(s) using the REFERENCES keyword.
Unique Constraint: A unique constraint is used to ensure that the values in a column or a combination of columns
are unique. It is similar to a primary key constraint, but it allows NULL values. A unique constraint is created by
defining a column or a combination of columns as unique using the UNIQUE keyword.
Check Constraint: A check constraint is used to ensure that the values in a column or a combination of columns
meet a certain condition. It is used to enforce business rules and data validation. A check constraint is created by
defining a condition using the CHECK keyword.
These integrity constraints can be defined while creating a table or can be added later using the ALTER TABLE
command. By using these constraints, you can ensure the consistency and accuracy of the data stored in your
MySQL database
1)-
CREATE TABLE Client_Master
(
Client_no VARCHAR (6) PRIMARY KEY,
name VARCHAR(20) not null,
city VARCHAR(15),
state VARCHAR(15),
pincode DECIMAL(6),
bal_due decimal(10,2),
check (Client_no like'C%')
);
2)-
CREATE TABLE product_master
(
product_no varchar(6) primary key,
description varchar(15) not null,
profit_percent decimal (4,2) not null,
unit_measure varchar(10) not null,
qty_on_hand decimal(8) not null,
reorder_lvl decimal(8) not null,
sell_price decimal(8,2) not null,
cost_price decimal(8,2) not null,
check(product_no like 'P%' and sell_price<>0 and cost_price<>0)
);
3)-
CREATE TABLE salesman_master
(
salesman_no varchar(10) primary key,
salesman_name varchar(20) not null,
Address1 varchar (30) not null,
Address2 varchar(30),
city varchar(18),
pincode varchar(6),
state varchar(20),
sal_amt decimal(8,2) not null,
tgt_to_get decimal(6,2) not null,
ytd_sales decimal(6,2) not null,
Remarks varchar(60),
check(sal_amt<>0 and tgt_to_get<>0 and salesman_no like 's%')
);
4)-
CREATE TABLE sales_order
(
s_order_no varchar(6) primary key,
s_order_date date,
client_no varchar(6),
salesman_no varchar(6),
dely_type char(1),
billed_yn char(1),
dely_date date,
order_status varchar(10),
check(s_order_no like '0%'),
check(order_status in('IP','F','BO','C')),
check(dely_date>s_order_date),
foreign key (client_no) references Client_master(Client_no),
foreign key (salesman_no) references salesman_master(salesman_no),
check(dely_type in('P','F'))
);
5)-
CREATE TABLE sales_order_details
(
s_order_no varchar(6) references sales_order(s_order_no),
product_no varchar(6) references product_master(product_no),
qty_ordered decimal(8),
qty_disp decimal(8),
product_rate decimal(10,2),
primary key(s_order_no,product_no)
);
6)-
CREATE TABLE challan_header
(
challan_no varchar(6) primary key,
s_order_no varchar(6),
foreign key(s_order_no) references sales_order(s_order_no),
challan_date date not null,
billed_yn char(1) default 'N',
check(challan_no like 'CH%')
);
7)-
CONCLUSION: We have learnt about various DDL commands and integrity constraints and
it’s implementation.