How to execute an entire table and create a database diagram - sql

When I run my query, only one of my rows is displayed.
I also need to create a database diagram from these 3 tables
Can someone please assist me on how to do that?
CREATE DATABASE Lab301;
CREATE TABLE Supplier(
SupplierID NVARCHAR(50),
Name NVARCHAR(50),
Address NVARCHAR(50),
PRIMARY KEY (SupplierID)
)
INSERT INTO Supplier(SupplierID,Name,Address)
VALUES('S01','ABC Company','Penang');
INSERT INTO Supplier(SupplierID,Name,Address)
VALUES('S02','XYZ Company','Johor');
INSERT INTO Supplier(SupplierID,Name,Address)
VALUES('S03','HJK Company','Selagnor');
INSERT INTO Supplier(SupplierID,Name,Address)
VALUES('S04','PQR Company','Selagnor');
SELECT * FROM Supplier;
CREATE TABLE Product(
ProductID NVARCHAR(50),
Name NVARCHAR(50),
PriceRM DECIMAL(10,2),
QuantitInStock INTEGER,
PRIMARY KEY (ProductID),
)
INSERT INTO Product(ProductID,Name,PriceRM,QuantitInStock)
VALUES('P01','Keyboard','103.55','60');
INSERT INTO Product(ProductID,Name,PriceRM,QuantitInStock)
VALUES('P02','Mouse','30.90','70');
INSERT INTO Product(ProductID,Name,PriceRM,QuantitInStock)
VALUES('P03','Monitor','200','80');
INSERT INTO Product(ProductID,Name,PriceRM,QuantitInStock)
VALUES('P04','Pendrive','40.30','50');
SELECT * FROM Product;
CREATE TABLE Supplies(
SuppliesID NVARCHAR(50),
SupplierID NVARCHAR(50),
ProductID NVARCHAR(50),
SuppliedDate DATE,
QuantitySupplied INTEGER,
PRIMARY KEY(SuppliesID),
FOREIGN KEY(SupplierID) REFERENCES Supplier(SupplierID),
FOREIGN KEY(ProductID) REFERENCES Product(ProductID),
)
INSERT INTO Supplies(SuppliesID,SupplierID,ProductID,SuppliedDate,QuantitySupplied)
VALUES('001','S01','P01','11/1/17','100');
INSERT INTO Supplies(SuppliesID,SupplierID,ProductID,SuppliedDate,QuantitySupplied)
VALUES('002','S01','P02','22/2/2017','200');
INSERT INTO Supplies(SuppliesID,SupplierID,ProductID,SuppliedDate,QuantitySupplied)
VALUES('003','S01','P03','NULL','300');
INSERT INTO Supplies(SuppliesID,SupplierID,ProductID,SuppliedDate,QuantitySupplied)
VALUES('004','S02','P03','30/4/2017','400');
SELECT * FROM Supplies;

Related

How to output a list of all orders placed on day X as calculate the total number of repairs and the total amount spent on them

can you help me to solve this:
Make a list of all orders placed on day X as
calculate the total number of repairs and the total amount spent on them
Thats the code :
CREATE TABLE Employee (
ID INT IDENTITY NOT NULL PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
);
CREATE TABLE Orders (
ID INT IDENTITY NOT NULL PRIMARY KEY,
Date_of_acceptance DATE,
Date_of_deadline DATE,
Status_order NVARCHAR(50) NOT NULL,
Type_of_repair NVARCHAR (50) NOT NULL,
Price DECIMAL (20),
EmployeeID INT,
CustomerID INT,
ItemsID INT
);
ALTER TABLE Orders ALTER COLUMN Price DECIMAL (5,2)
CREATE TABLE Customer (
ID INT IDENTITY NOT NULL PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
PhoneNumber VARCHAR(20)
);
CREATE TABLE Items (
ID INT IDENTITY NOT NULL PRIMARY KEY,
ItemsName NVARCHAR(50) NOT NULL,
Status_items NVARCHAR(50) NOT NULL
);
ALTER TABLE Orders
ADD CONSTRAINT FK_Orders_Employee FOREIGN KEY (EmployeeID)
REFERENCES Employee(ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
ALTER TABLE Orders
ADD CONSTRAINT FK_Orders_Customer FOREIGN KEY (CustomerID)
REFERENCES Customer(ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
ALTER TABLE Orders
ADD CONSTRAINT FK_Orders_Items FOREIGN KEY (ItemsID)
REFERENCES Items(ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
INSERT INTO Items( ItemsName, Status_items )
VALUES ( 'Printer', 'Accept for the repair ');
INSERT INTO Items( ItemsName, Status_items )
VALUES ( 'Computer', 'Unclaimed');
INSERT INTO Items( ItemsName, Status_items )
VALUES ('Laptop', 'Accept for the repair');
INSERT INTO Items( ItemsName, Status_items )
VALUES ('Battery' , 'Unclaimed' );
INSERT INTO Items( ItemsName, Status_items )
VALUES ('Computer' , 'Accept for repair' );
INSERT INTO Items( ItemsName, Status_items )
VALUES ( 'Monitor', 'Unclaimed');
SELECT * FROM Items;
UPDATE Items SET Status_items ='Accept for repair' WHERE ID=7;
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price, EmployeeID, CustomerID, ItemsID)
VALUES('2021-11-16', '2021-11-18','Accept','Broken screen of laptop', 100, 1,1,7);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-10-9', '2021-10-15','Submitted','Broken printer',90,1,1,1);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-5-16', '2021-5-30','Waiting for delivery of part','Rеplacing the motherboard',500,1,1,6);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-8-2', '2021-8-26','Accept','Repair a broken computer',600, 2,2,2);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES('2021-7-12', '2021-7-14','Accept','Change the laptop battery', 120,2,2,5);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-10-9', '2021-10-15','Submitted','Update windows',30,2,2,6);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-2-6', '2021-2-21','Waiting for delivery of part',' Rеplacing the motherboard ',500,3,3,3);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-4-3', '2021-4-25','Accept',' Virus scan',25,3,3,3);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-5-16', '2021-5-30','Accept',' Change the laptop battery',120,3,3,5);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-10-9', '2021-10-15','Submitted','Broken printer',90,3,3,1);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-10-5', '2021-10-7','Accept','Broken screen',90,3,3,7);
INSERT INTO Orders( Date_of_acceptance,Date_of_deadline,Status_order,Type_of_repair,Price,EmployeeID, CustomerID, ItemsID)
VALUES ('2021-3-5', '2021-3-8','Accept',' Rеplacing the motherboard',90,3,3,2);
SELECT * FROM Orders;
UPDATE Orders SET Date_of_acceptance ='2021-10-9' WHERE ID=7;
UPDATE Orders SET Date_of_deadline ='2021-10-15' WHERE ID=7;
I did this, but I don't know if it's right
I don't know how to fix it:
SELECT Date_of_deadline,Type_of_repair,Price
FROM Orders
WHERE Orders.Status_order = 'Submitted'(
SELECT SUM(Price) AS 'Sum of repairs'
FROM Orders
WHERE Orders.Status_order = 'Submitted')
I'm confused by your code, you made two separate queries right?
I think you forgot to add a count(1) on the second select as to calculate the total amount of orders and add the date as where parameter to equal it to X

sql error with foreign key

I have the error here:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CHITIETDH__MaDH__4316F928". The conflict occurred in database [...]
But with the code here :
create database abc_1quanly111
use abc_1quanly111
create table abc_1nhacc
(
MaNhaCC varchar(10) primary key,
TenNhaCC varchar(50),
DiaChiCC varchar(50),
Telephone varchar(50),
)
create table abc_1mamathang
(
MaMH varchar(10) primary key,
TenMH varchar(50) not null,
DonGia int check (DonGia>0),
SoLuong int ,
MaNhaCC varchar(10) foreign key references abc_1nhacc(MaNhaCC)
)
--Insert Nha CC --- Values -----
INSERT INTO abc_1nhacc VALUES('NC1','Phong Vu','123 NTMK ','0283232677')
INSERT INTO abc_1nhacc VALUES('NC2','Phong Hoang','123 NTD ','0290120217'),('NC3','THE GIOI DI DONG','12 LE VAN VIET ','028382901'),('NC4','NGUYEN KIM','23 LE DUAN ','01283232677'),('NC5','Phong KIM','13 LE LOI ','0288292677')
select * from abc_1nhacc
--Insert Mat Hang ---- Values ----
insert into abc_1mamathang values('M1','Laptop Sony',1000000,100,'NC1')
,
('M2','Laptop MSI GAMING',3200000,1,'NC2'),
('M3','Laptop ASUS',900000,10,'NC3'),
('M4','Laptop ACER',1340000,20,'NC4'),
('M5','Xperia A',10000,10,'NC5')
select * from abc_1nhacc
select * from abc_1mamathang
create table abc_1khachhang
(
MaKhachHang varchar(10) primary key,
TenKhachHang varchar(50),
DiaChiKhachHang varchar(50),
PhoneKhachHang varchar(10),
)
--Insert Khach Hang ---- Values ----
INSERT INTO abc_1khachhang values('KH1','Anh A','121 Le Duan Q1','09878721'),('KH2','Anh B','12 Quang Trung Q12','01878221'),('KH3','Anh C','331 Nguyen Thi Minh Khai','028787221'),('KH4','Anh D','23 Le Quang Dinh','09872321'),('KH5','Bac AD','','098787211')
create table abc_1dondathang
(
MaSoDonDatHang varchar(10) primary key,
NgayDatHang DATE Default GetDate(),
MaKhachHang varchar(10) foreign key references abc_1khachhang(MaKhachHang)
)
CREATE TABLE CHITIETDH
(
MaDH varchar(10) foreign key references abc_1dondathang,
MaMH varchar(10) foreign key references abc_1mamathang,
Soluong int check (soluong>0)
constraint pk_DHMH primary key(MaDH ,MaMH)
)
select * from abc_1dondathang
select * from abc_1mamathang
select * from CHITIETDH
insert into CHITIETDH values('DH1','MH1',12) // errors
select * from CHITIETDH
I don't know solution how to fix that, can anyone help me thanks.
Your table CHITIETDH has a foreign key in table abc_1dondathang.
You try to insert values to CHITIETDH while abc_1dondathang is empty. You must all the values under this column MaDH varchar(10) foreign key references abc_1dondathang will exist in abc_1dondathang before you insert something in CHITIETDH.
You can read here more.

MIN/MAX VALUES GROUP BY ID

Find the names and addresses of the customers with the highest balance and the people with the lowest loan in each branch.
Tables:
CREATE TABLE Branch(
BranchID INT NOT NULL,
Address varchar(100),
ManagerSsn varchar(100),
Branch_name varchar(255),
PRIMARY KEY (BranchID)
);
CREATE TABLE Has_Account(
Assn Int,
ANo CHAR(16),
Opened_date date,
PRIMARY KEY(Assn),
FOREIGN KEY(Assn) REFERENCES Customer(Ssn),
FOREIGN KEY(ANo) REFERENCES Account(AccountNo)
);
CREATE TABLE Account(
AccountNo CHAR(16) NOT NULL,
BranchID Int,
Balance Int,
PRIMARY KEY(AccountNo),
FOREIGN KEY(BranchID) REFERENCES Branch(BranchID)
);
CREATE TABLE Customer(
Ssn Int CHECK(0101000000000 < Ssn < 3112999995999) NOT NULL,
Address VARCHAR(100),
Name VARCHAR(100),
EmployerID Int,
PRIMARY KEY(Ssn)
);
CREATE TABLE Loan(
LoanNo CHAR(16) NOT NULL,
BranchID Int,
Amount Int,
PRIMARY KEY(LoanNo),
FOREIGN KEY(BranchID) REFERENCES Branch(BranchID)
);
CREATE TABLE Has_Loan(
LNo CHAR(16),
Lssn Int,
Repay_date date,
PRIMARY KEY (LNo),
FOREIGN KEY(Lno) REFERENCES Loan(LoanNo),
FOREIGN KEY(Lssn) REFERENCES Customer(Ssn)
);
Populating the tables
INSERT INTO CUSTOMER
VALUES('2004980890999', ‘Kanaribakken 2B’, ‘Paul Stolten’, ‘’);
INSERT INTO CUSTOMER
VALUES(‘1010968250234’, ‘Parkveien 5’, ‘Anders Nilsson’, ‘’, );
INSERT INTO CUSTOMER
VALUES(‘2104996495987’, ‘Kongeveien 24B’, ‘Karoline Torskerud’, ‘123’, );
INSERT INTO CUSTOMER
VALUES(‘2910943135444’, ‘Gåsesmauet 3’, ‘Bente Arildsen’, ‘555’, );
INSERT INTO CUSTOMER
VALUES(‘1304980890678’, ‘Knallebakken 30’, ‘Knut Solberg’, ‘555’,);
INSERT INTO CUSTOMER
VALUES('1019730000234', ‘Laksebakken 40’, ‘Harald Olsen’, ‘’);
INSERT INTO BRANCH
VALUES (‘123’, ‘Børsveien 49’, ‘1412963670786’, ‘Nesttun’,);
INSERT INTO BRANCH
VALUES(‘555’, ‘Vimpelveien 24’, ‘2412983905999’, ‘DNB’,);
INSERT INTO ACCOUNT
VALUES (‘1234567890087620’, ‘123’, ‘459’,);
INSERT INTO ACCOUNT
VALUES (‘8402849284910340’, ‘123’, ‘35120’,);
INSERT INTO ACCOUNT
VALUES (‘3452948293817280’, ‘555’, ‘69’,);
INSERT INTO HAS_ACCOUNT
VALUES(‘1010968250234’, ‘1234567890087620’, ‘17.04.2012’,);
INSERT INTO HAS_ACCOUNT
VALUES (‘2104996495987’, ‘8402849284910340’, ‘20.09.2000’,);
INSERT INTO HAS_ACCOUNT
VALUES (‘2910943135444’, ‘3452948293817280’, ‘29.01.2006’,);
INSERT INTO LOAN
VALUES (‘2291928394039280’, ‘555’, ‘50000’,);
INSERT INTO LOAN
VALUES (‘1295829384932100’, ‘123’, ‘400000’,);
INSERT INTO LOAN
VALUES ('1295829384932222', ‘123’, ‘40000’);
INSERT INTO HAS_LOAN
VALUES (‘2910943135444’, ‘2291928394039280’, ‘09.09.2019’,);
INSERT INTO HAS_LOAN
VALUES (‘2104996495987’, ‘1295829384932100’, ‘14.03.2022’,);
INSERT INTO HAS_LOAN
VALUES ('2004980890999', '1295829384932222', '01.02.2021');
Draft so far:
SELECT DISTINCT Customer.Name, Customer.Address
FROM Customer, Account, Loan, Has_Account, Has_Loan
WHERE Customer.Ssn = Has_Account.Assn AND Has_Account.Assn = Has_Loan.Lssn
(SELECT Account.Balance MAX(Balance) GROUP BY(BranchID)) AND
Loan.AMOUNT(SELECT Loan.Amount MIN(Amount) GROUP BY(BranchID));
I think it is correct that we need to group our output by BranchID, and max/min is ok. Looks like the statement formalities are wrong somehow.
Any help or hint is much appreciated!

how to create STORED PROCEDURE to insert data in two tables according to the CustomerID

CREATE
TABLE Customers(CustomerID int IDENTITY(1,1) PRIMARY KEY,FirstName nvarchar(45),LastName nvarchar(45),Address nvarchar(45))
CREATE
TABLE Orders(OrderID int IDENTITY(1,1) PRIMARY KEY,OrderDate date,CustomerID int FOREIGN KEY REFERENCES [dbo].[Customers](CustomerID))
CREATE
TABLE CustomersOrders(OrderID int FOREIGN KEY REFERENCES [dbo].[Orders](OrderID),ProductName nvarchar(45),Quantity nvarchar(45),Price nvarchar(45),TotalPrice int)
how to create STORED PROCEDURE in to insert data in Orders and CustomersOrders two tables according to the [dbo].Customers).
Customers table has entered data with 5 CustomerID
My Database is with first table parent and second is child . Third is the child of second table. I want to
insert data in last two tables according to the CustomerID. The data has to be added with that
particular customerid.
Try this
CREATE TABLE Customers
(
CustomerID INT IDENTITY(1, 1) PRIMARY KEY,
FirstName NVARCHAR(45),
LastName NVARCHAR(45),
Address NVARCHAR(45)
)
CREATE TABLE Orders
(
OrderID INT IDENTITY(1, 1) PRIMARY KEY,
OrderDate DATE,
CustomerID INT FOREIGN KEY REFERENCES [dbo].[Customers](CustomerID)
)
CREATE TABLE CustomersOrders
(
OrderID INT FOREIGN KEY REFERENCES [dbo].[Orders](OrderID),
ProductName NVARCHAR(45),
Quantity NVARCHAR(45),
Price NVARCHAR(45),
TotalPrice INT
)
DECLARE #FirstName NVARCHAR(45)='John',
#LastName NVARCHAR(45)='Smiths',
#Address NVARCHAR(45)='46 as str',
#OrderDate DATE=getdate(),
#ProductName NVARCHAR(45)='p1',
#Quantity NVARCHAR(45)='1',
#Price NVARCHAR(45)='30',
#TotalPrice INT=30
BEGIN
INSERT INTO Customers
(FirstName,
LastName,
Address)
VALUES (#FirstName,
#LastName,
#Address);
INSERT INTO Orders
(OrderDate,
CustomerID)
VALUES (#orderdate,
##IDENTITY)
INSERT INTO CustomersOrders
(OrderID,
ProductName,
Quantity,
Price,
TotalPrice)
VALUES (##IDENTITY,
#ProductName,
#Quantity,
#Price,
#TotalPrice)
SELECT *
FROM customers
SELECT *
FROM Orders
SELECT *
FROM CustomersOrders
END

How to group several INSERTs?

I'm inserting data from one table into several others. The first insert will create a new userid. This new userid will be used in succeeding inserts. I will also continue inserting username from the source table into other tables. The chain of inserts below are for one user. There will be probably 2000 users involved.
I'm familiar with how this can be done using a cursor. Is there some other way to do this chain of inserts without a cursor?
insert into table 1 using #username and #firstname from source table
insert into table 2 using userid generated from table 1 (userid1)
insert into table 3 using #username and userid1
insert into table 4 using userid1
You can use the Output Clause of an Insert statement to capture generated Ids in bulk.
For example:
Create Table dbo.Source (
FirstName nvarchar(100),
LastName nvarchar(100)
);
Create Table dbo.Attrs (
Id int Identity Not Null Primary Key,
Name nvarchar(100) Not Null,
DefaultVal nvarchar(100)
);
Create Table dbo.Table1 (
Id Int Identity Not Null Primary Key,
FirstName nvarchar(100),
LastName nvarchar(100)
);
Create Table dbo.Table2 (
Id int Identity Not Null Primary Key,
Table1ID int Not Null Foreign Key References dbo.Table1 (Id),
AttrId int Not Null Foreign Key References dbo.Attrs (Id)
);
Insert Into dbo.Source Values
(N'Mickey', N'Mouse'),
(N'Donald', N'Duck'),
(N'Goofy', Null);
Insert Into dbo.Attrs Values
('Size', 'Small'),
('Wings', 'No');
Declare #Temp1 Table (Id Int, FirstName nvarchar(100), LastName nvarchar(100))
Declare #Temp2 Table (Id int, Table1ID int, AttrId int)
Insert Into dbo.Table1
(FirstName, LastName)
Output
inserted.Id, inserted.FirstName, inserted.LastName
Into
#Temp1
Select
FirstName, LastName
From
dbo.Source
Insert Into dbo.Table2
(Table1ID, AttrId)
Output
inserted.Id, Inserted.Table1ID, Inserted.AttrID
Into
#Temp2
Select
t.Id,
a.Id
From
#Temp1 t
Cross Join
dbo.Attrs a
Select * From #Temp2
http://sqlfiddle.com/#!3/31110/3