IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Customers') BEGIN CREATE TABLE…
- User
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Customers') BEGIN CREATE TABLE Customers ( CustomersID INT PRIMARY KEY NOT NULL, LastName NVARCHAR(30) NOT NULL, FirstName NVARCHAR(30) NOT NULL, City NVARCHAR(15) NULL, Postalcode NVARCHAR(10) NULL, Country NVARCHAR(15) NULL, Phone NVARCHAR(24) NULL ); END;
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Orders') BEGIN CREATE TABLE Orders ( OrderID INT PRIMARY KEY NOT NULL, CustomersID INT NULL, Order_date DATETIME NULL, Shipped_date DATETIME NULL, TransportCost INT NULL CONSTRAINT "order_transport" DEFAULT (0), ShipVia INT NULL, FOREIGN KEY (CustomersID) REFERENCES Customers(CustomersID), FOREIGN KEY (ShipVia) REFERENCES Shippers(ShipperID) ); END;
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Shippers') BEGIN CREATE TABLE Shippers ( ShipperID INT PRIMARY KEY NOT NULL, CompanyName NVARCHAR(40) NOT NULL, phone NVARCHAR(24) NULL ); END;
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Suppliers') BEGIN CREATE TABLE Suppliers ( SupplierID INT PRIMARY KEY NOT NULL, CompanyName NVARCHAR(40) NOT NULL, ContactName NVARCHAR(30) NULL, Address NVARCHAR(60) NULL, City NVARCHAR(15) NULL, Region NVARCHAR(15) NULL, PostalCode NVARCHAR(10) NULL, Phone NVARCHAR(24) NULL ); END;
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Supplier_product') BEGIN CREATE TABLE Supplier_product ( SupplierID INT, ProductID INT, PRIMARY KEY (SupplierID, ProductID), FOREIGN KEY (SupplierID) REFERENCES Suppliers(SupplierID), FOREIGN KEY (ProductID) REFERENCES Products(ProductID) ); END;
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Products') BEGIN CREATE TABLE Products ( ProductID INT PRIMARY KEY NOT NULL, ProductName NVARCHAR(40) NOT NULL, CategoryID INT NULL, UnitPrice MONEY NULL CONSTRAINT "Prod_unit_price" DEFAULT (0), UnitsInStock INT NULL CONSTRAINT "Prod_units_stock" DEFAULT(0), Discontinued BIT NOT NULL CONSTRAINT "Prod_discontin" DEFAULT(0), FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID) ); END;
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Categories') BEGIN CREATE TABLE Categories ( CategoryID INT PRIMARY KEY NOT NULL, CategoryName NVARCHAR(15) NOT NULL, TextDescription TEXT NULL, Picture IMAGE NULL ); END;
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Order_Details') BEGIN CREATE TABLE Order_Details ( OrderID INT NOT NULL, ProductID INT NOT NULL, PRIMARY KEY (OrderID, ProductID), UnitPrice MONEY NOT NULL CONSTRAINT "Order_unit_price" default (0), Quantity INT NOT NULL CONSTRAINT "Order_quantity" default(1), Discount REAL NOT NULL CONSTRAINT "Order_discount" default(0), FOREIGN KEY (OrderID) REFERENCES Orders(OrderID), FOREIGN KEY (ProductID) REFERENCES Products(ProductID), CONSTRAINT "CK_Discount" CHECK (Discount >= 0 and (Discount <= 1)), CONSTRAINT "CK_Quantity" CHECK (Quantity > 0), CONSTRAINT "CK_UnitPrice" CHECK (UnitPrice >= 0) ); END;
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Review') BEGIN CREATE TABLE Review ( ReviewID INT PRIMARY KEY NOT NULL, CustomersID INT NULL, ProductID INT NOT NULL, ReviewText TEXT NOT NULL, Rating INT NOT NULL, CreatedDate DATETIME NULL, FOREIGN KEY (CustomersID) REFERENCES Customers(CustomersID), FOREIGN KEY (ProductID) REFERENCES Products(ProductID) ); END;
- Assistant
Below are some examples of more complex UPDATE statements for the given tables:
- Update a customer, setting the city and postal code equal to a different customer's values:
sqlUPDATE Customers SET City = ( SELECT City FROM Customers WHERE CustomersID = 2 ), Postalcode = ( SELECT Postalcode FROM Customers WHERE CustomersID = 2 ) WHERE CustomersID = 1;- Increase the price of products in a category by a percentage:
sqlUPDATE Products SET UnitPrice = UnitPrice * 1.1 -- Increase the price by 10% WHERE CategoryID = ( SELECT CategoryID FROM Categories WHERE CategoryName = 'Beverages' ); - User
Create more, more complex updates
- Assistant
Here are five additional complex UPDATE examples:
- Update the discontinued status for products that have related reviews with an average rating below 2:
sqlUPDATE Products SET Discontinued = 1 WHERE ProductID IN ( SELECT ProductID FROM Review GROUP BY ProductID HAVING AVG(Rating) < 2 ); - User
- Assistant
Unfortunately, SQL update operations cannot conditionally update to a different value depending on some criteria; they can only update a specific column(s) with a specific value or a derived value from the same row.
However, the following SQL illustrates how to set the ticket status for tickets assigned to an agent with a specified last name:
sql - User
- Assistant