A Step-by-Step Guide to Learning Structured Query Language
Setting Up Your MySQL Environment
Day:1 – create a database called ‘assignments’
Create the tables and Enter the records
Understanding Database Concepts:
Date:
Executing SQL Statements:
Step:1
Create a table called countries with the following columns name, population, capital by choosing choose appropriate datatypes for the columns
Step:2
Insert the data into the table
Step:3:
Add a couple of countries of your choice
Step:4
Change ‘Delhi’ to ‘New Delhi’
Rename the table countries to big_countries .
Creating table with Fields and Constraints:
create table journey(
bus_id integer not null,
bus_name varchar (255) not null,
source_station varchar(255) not null,
destination varchar(255) not null,
email varchar(255) not null,
constraint journey_pk primary key (bus_ID),
constraint email_unique unique (email)
);
create table vendor(
vendor_Id int primary key,
name varchar (50) not null,
email varchar(50) unique,
country varchar(50) default "N/A"
);
CREATE TABLE movies (
Movie_ID INT NOT NULL,
Name VARCHAR(50) NOT NULL,
Release_Year VARCHAR(4) DEFAULT '-',
Cast VARCHAR(255) NOT NULL,
Gender ENUM('Male', 'Female'),
No_of_shows INT CHECK (No_of_shows > 0),
CONSTRAINT movie_id_unique UNIQUE (Movie_ID),
PRIMARY KEY (Movie_ID)
);
create table product_stock(
product_id Int auto_increment primary key,
product_name varchar(255) not null unique,
description text
);
INSERT INTO product_stock(product_id, product_name, description)
VALUES
(1001,'samsung', 'Spigen Samsung Galaxy S5 Case Wallet'),
(1002,'apple', 'Apple iPhone 5C'),
(1003,'logitech', 'Wireless Gaming Headset G930');
CREATE TABLE Suppliers_stock(
supplier_id INT AUTO_INCREMENT PRIMARY KEY,
supplier_name VARCHAR(255),
location VARCHAR(255),
parent_supplier_id INT,
FOREIGN KEY (parent_supplier_id) REFERENCES Suppliers_stock(supplier_id)
on delete cascade
on update cascade
);
INSERT INTO Suppliers_stock (supplier_name, location, parent_supplier_id)
VALUES
('Supplier A', 'United states', NULL),
('Supplier B', 'Location B', 1),
('Supplier C', 'Location C', 2);
ALTER TABLE Suppliers_stock
MODIFY supplier_name VARCHAR(255) NOT NULL UNIQUE;
DELETE FROM Suppliers_stock;
set sql_safe_updates = 1;
DROP TABLE IF EXISTS Stock;
CREATE TABLE Stock (
id INT PRIMARY KEY,
prod_id INT,
balance_stock INT,
FOREIGN KEY (prod_id) REFERENCES product_stock(product_id)
on delete cascade
on update cascade
);
INSERT INTO Stock (id, prod_id, balance_stock)
VALUES
(1, 1001, 50),
(2, 1002, 75),
(3, 1003, 100);
Recent Comments