Question

CREATE TABLE GLAccounts (    AccountNo INT,    AccountDescription VARCHAR(50),    --    CONSTRAINT GLAccounts_PK PRIMARY...

CREATE TABLE GLAccounts
(
   AccountNo INT,
   AccountDescription VARCHAR(50),
   --
   CONSTRAINT GLAccounts_PK PRIMARY KEY(AccountNo),
   --
   CONSTRAINT AccountDescription_NULL CHECK(AccountDescription IS NOT NULL),
   --
   CONSTRAINT AccountDescription_Zero_LEN CHECK(LEN(AccountDescription)>0)
);

GO

-----------------------------------------------------------------------------

CREATE TABLE Terms
(
   TermsID INT,
   TermsDescription VARCHAR(50),
   TermsDueDays SMALLINT,
   --
   CONSTRAINT PK_Terms PRIMARY KEY(TermsID),
   --
   CONSTRAINT TermsDescription_NULL    CHECK(TermsDescription IS NOT NULL),
   CONSTRAINT TermsDueDays_NULL        CHECK(TermsDueDays IS NOT NULL),
   --
   CONSTRAINT TermsDescription_zero_LEN
       CHECK( LEN(TermsDescription)>0)
);
ALTER TABLE Terms NOCHECK CONSTRAINT TermsDueDays_NULL;
GO

-----------------------------------------------------

CREATE TABLE Vendors
(
   VendorID INT,
   VendorName VARCHAR(50),
   VendorAddress1 VARCHAR(50),
   VendorAddress2 VARCHAR(50),
   VendorCity VARCHAR(50),
   VendorState CHAR(2),
   VendorZipCode VARCHAR(20),
   VendorPhone VARCHAR(50),
   VendorContactLName VARCHAR(50),
   VendorContactFName VARCHAR(50),
   DefaultTermsID INT,
   DefaultAccountNo INT,
   --
   CONSTRAINT PK_Vendors PRIMARY KEY(VendorID),
   CONSTRAINT FK_def_accnt_no FOREIGN KEY(DefaultAccountNo)
       REFERENCES GLAccounts(accountNo),
   --
   CONSTRAINT VendorName_NULL            CHECK(VendorName IS NOT NULL),
   CONSTRAINT VendorAddress1_NULL        CHECK(VendorAddress1 IS NOT NULL),
   CONSTRAINT VendorAddress2_NULL        CHECK(VendorAddress2 IS NOT NULL),
   CONSTRAINT VendorCity_NULL            CHECK(VendorCity IS NOT NULL),
   CONSTRAINT VendorState_NULL        CHECK(VendorState IS NOT NULL),
   CONSTRAINT VendorZipCode_NULL        CHECK(VendorZipCode IS NOT NULL),
   CONSTRAINT VendorPhone_NULL        CHECK(VendorPhone IS NOT NULL),
   CONSTRAINT VendorContactLName_NULL    CHECK(VendorContactLName IS NOT NULL),
   CONSTRAINT VendorContactFName_NULL    CHECK(VendorContactFName IS NOT NULL),
   CONSTRAINT DefaultTermsID_NULL        CHECK(DefaultTermsID IS NOT NULL),
   CONSTRAINT DefaultAccountNo_NULL    CHECK(DefaultAccountNo IS NOT NULL),
   --
   CONSTRAINT VendorName_zero_LEN           CHECK( LEN(VendorName)>0),
   CONSTRAINT VendorAddress1_zero_LEN       CHECK( LEN(VendorAddress1)>0),
   CONSTRAINT VendorAddress2_zero_LEN       CHECK( LEN(VendorAddress2)>0),
   CONSTRAINT VendorCity_zero_LEN           CHECK( LEN(VendorCity)>0),
   CONSTRAINT VendorState_valid_LEN       CHECK( LEN(VendorState)=2),
   CONSTRAINT VendorZipCode_valid_LEN       CHECK( LEN(VendorZipCode)>=5),
   CONSTRAINT VendorContactLName_zero_LEN   CHECK( LEN(VendorContactLName)>0),
   CONSTRAINT VendorContactFName_zero_LEN   CHECK( LEN(VendorContactFName)>0)
);

-- Turn the constraints I do not want to use off
ALTER TABLE Vendors NOCHECK CONSTRAINT VendorAddress1_NULL;
ALTER TABLE Vendors NOCHECK CONSTRAINT VendorAddress2_NULL;
ALTER TABLE Vendors NOCHECK CONSTRAINT VendorPhone_NULL;
ALTER TABLE Vendors NOCHECK CONSTRAINT VendorZipCode_NULL;
ALTER TABLE Vendors NOCHECK CONSTRAINT VendorContactLName_NULL;
ALTER TABLE Vendors NOCHECK CONSTRAINT VendorContactFName_NULL;
GO

--------------------------------------------------------------

CREATE TABLE Invoices
(
   InvoiceID       INT,
   VendorID       INT,
   InvoiceNumber   VARCHAR(50),
   InvoiceDate       SMALLDATETIME,
   InvoiceTotal   MONEY,
   PaymentTotal   MONEY,
   CreditTotal       MONEY,
   TermsID           INT,
   InvoiceDueDate   SMALLDATETIME,
   PaymentDate       SMALLDATETIME,
   --
   CONSTRAINT PK_Invoices PRIMARY KEY(InvoiceID),
   --
   -- Constrain all NULLs
   CONSTRAINT VendorID_NULL        CHECK(VendorID IS NOT NULL),
   CONSTRAINT InvoiceNumber_NULL    CHECK(InvoiceNumber IS NOT NULL),
   --CONSTRAINT InvoiceTotal_NULL    CHECK(InvoiceTotal IS NOT NULL),
   CONSTRAINT PaymentTotal_NULL    CHECK(PaymentTotal IS NOT NULL),
   CONSTRAINT CreditTotal_NULL    CHECK(CreditTotal IS NOT NULL),
   CONSTRAINT TermsID_NULL           CHECK(TermsID IS NOT NULL),
   CONSTRAINT InvoiceDueDate_NULL   CHECK(InvoiceDueDate IS NOT NULL),
   CONSTRAINT PaymentDate_NULL       CHECK(PaymentDate IS NOT NULL),
   --
   CONSTRAINT InvoiceNumber_zero_LEN   CHECK( LEN(InvoiceNumber)>0),
   --
   CONSTRAINT Invoices_Vendors_FK   FOREIGN KEY(VendorID)
       REFERENCES Vendors(VendorID),
   CONSTRAINT Invoices_Terms_FK   FOREIGN KEY(TermsID)
       REFERENCES Terms(TermsID)
);

-- Turn the constraints I do not want to use off
ALTER TABLE Invoices NOCHECK CONSTRAINT PaymentDate_NULL;
ALTER TABLE Invoices NOCHECK CONSTRAINT TermsID_NULL;
ALTER TABLE Invoices NOCHECK CONSTRAINT InvoiceDueDate_NULL;
ALTER TABLE Invoices NOCHECK CONSTRAINT Invoices_Vendors_FK;
GO
GO

-----------------------------------------------------------------------------

CREATE TABLE InvoiceLineItems
(
   InvoiceID INT,
   InvoiceSequence SMALLINT,
   AccountNo INT,
   InvoiceLineItemAmount MONEY,
   InvoiceLineItemDescription VARCHAR(100),
   --
   CONSTRAINT InvoiceLineItems_PK
       PRIMARY KEY(InvoiceID, InvoiceSequence),
------------------------------------------------------------------
   CONSTRAINT FK_Inv_Line FOREIGN KEY(invoiceID)
       REFERENCES invoices(invoiceID),
   CONSTRAINT FK_glAccounts FOREIGN KEY(accountNo)
       REFERENCES glAccounts(accountNo),
---------------------------------------------------------------
   --
   CONSTRAINT AccountNo_NULL CHECK(AccountNo IS NOT NULL),
   --
   CONSTRAINT InvoiceLineItemAmount_NULL CHECK(InvoiceLineItemAmount IS NOT NULL),
   --
   CONSTRAINT InvoiceLineItemDescription_NULL CHECK(InvoiceLineItemDescription IS NOT NULL),
   --
   CONSTRAINT InvoiceLineItemDescription_zero_LEN
       CHECK( LEN(InvoiceLineItemDescription)>0)
);
ALTER TABLE invoiceLineItems NOCHECK CONSTRAINT FK_Inv_Line;

CREATE TABLE InvoiceArchive
(
   InvoiceID        INT,
   VendorID        INT,
   InvoiceNumber    VARCHAR(50),
   InvoiceDate    SMALLDATETIME,
   InvoiceTotal   MONEY,
   PaymentTotal   MONEY,
   CreditTotal       MONEY,
   TermsID           INT,
   InvoiceDueDate   SMALLDATETIME,
   PaymentDate       SMALLDATETIME,
   --
   CONSTRAINT arcInvoiceID_NULL        CHECK(InvoiceID        IS NOT NULL),
   CONSTRAINT arcVendorID_NULL        CHECK(VendorID            IS NOT NULL),
   CONSTRAINT arcInvoiceNumber_NULL    CHECK(InvoiceNumber    IS NOT NULL),
   CONSTRAINT arcInvoiceDate_NULL        CHECK(InvoiceDate        IS NOT NULL),
   CONSTRAINT arcInvoiceTotal_NULL    CHECK(InvoiceTotal        IS NOT NULL),
   CONSTRAINT arcPaymentTotal_NULL    CHECK(PaymentTotal        IS NOT NULL),
   CONSTRAINT arcCreditTotal_NULL        CHECK(CreditTotal        IS NOT NULL),
   CONSTRAINT arcTermsID_NULL            CHECK(TermsID            IS NOT NULL),
   CONSTRAINT arcInvoiceDueDate_NULL    CHECK(InvoiceDueDate    IS NOT NULL),
   CONSTRAINT arcPaymentDate_NULL        CHECK(PaymentDate        IS NOT NULL),
   --
   CONSTRAINT arcInvoiceNumber_zero_LEN   CHECK( LEN(InvoiceNumber)>0),
   --
   CONSTRAINT arcInvoices_PK   PRIMARY KEY(InvoiceID)
);
ALTER TABLE InvoiceArchive NOCHECK CONSTRAINT arcPaymentDate_NULL;
GO
INSERT dbo.InvoiceArchive (InvoiceID, VendorID, InvoiceNumber, InvoiceDate, InvoiceTotal, PaymentTotal, CreditTotal, TermsID, InvoiceDueDate, PaymentDate) VALUES (300, 123, N'963007234', CAST(0x9FB80000 AS SmallDateTime), 138.7500, 138.7500, 0.0000, 3, CAST(0x9FD70000 AS SmallDateTime), CAST(0x9FD30000 AS SmallDateTime))
INSERT dbo.InvoiceArchive (InvoiceID, VendorID, InvoiceNumber, InvoiceDate, InvoiceTotal, PaymentTotal, CreditTotal, TermsID, InvoiceDueDate, PaymentDate) VALUES (400, 123, N'2-000-2556', CAST(0x9FBB0000 AS SmallDateTime), 144.7000, 144.7000, 0.0000, 3, CAST(0x9FDA0000 AS SmallDateTime), CAST(0x9FD60000 AS SmallDateTime))

HERE IS THE DATA AND I NEED HELP WITH A PROBLEM RELATED TO THIS DATA

3) We are trying to enable the FOREIGN KEY constraint in the invoiceLineItems table; however, we have data in the child table that are inconsistent with the similarly named field in the invoices table. Return all data on the row or rows. (Hint: SELECT tableName.* ... one row returned)

4) While we're at it, let's check the contactUpdates table for bogus data. The odd thing about that table is that somebody set up the FOREIGN KEY referencing the vendors table in contactUpdates as also being the PRIMARY KEY! Write an SQL query that will find any vendorID in contactUpdates that does not appear in the vendors; return all data. (There is one.)

5) List the vendorID and vendorName for all vendors who have no corresponding rows in the invoices table. (I see 88 of them.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In above question you need data from both parent and child tables. Here parent table is "Invoices" and child is "invoiceLineItems".

Following is the query to fetch data from both tables:

SELECT t1.InvoiceID, t1.VendorID, t1.InvoiceNumber, t1.InvoiceDate, t1.InvoiceTotal, t1.PaymentTotal, t1.CreditTotal, t1.TermsID, t1.InvoiceDueDate, t1.PaymentDate,

t2.InvoiceSequence, t2.AccountNo, t2.InvoiceLineItemAmount, t2.InvoiceLineItemDescription

FROM Invoices t1, invoiceLineItems t2

WHERE t1.InvoiceID = t2.InvoiceID;

Add a comment
Know the answer?
Add Answer to:
CREATE TABLE GLAccounts (    AccountNo INT,    AccountDescription VARCHAR(50),    --    CONSTRAINT GLAccounts_PK PRIMARY...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a select statement that returns the vendorname and paymentsum of each vendor, where paymentsum is...

    Write a select statement that returns the vendorname and paymentsum of each vendor, where paymentsum is the sum of the paymentotal column. Return only the top ten vendors who have been paid the most and the number of invoices is >5. CREATE TABLE InvoiceArchive InvoiceID int NOT NULL, VendorID int NOT NULL InvoiceNumber varchar(50) NOT NULL, InvoiceDate smalldatetime NOT NULL, Invoice Total money NOT NULL, Payment Total money NOT NULL, CreditTotal money NOT NULL, TermsID int NOT NULL, InvoiceDueDate smalldatetime...

  • Someone Please Help Me modify this in PHP I'm in desperate need I cant figure this out ... Make t...

    Someone Please Help Me modify this in PHP I'm in desperate need I cant figure this out ... Make the following modifications: For the vendors table: Comment out the table-level primary key Change the VendorIDcolumn to be a column-level primary key Add a VendorEmail column to the bottom of the table definition (define the data type for the column as variable character and set it to not exceed 45 characters) After the lineItems table, add code to create a table...

  • SQL SERVER Write a SELECT statement that return the vendor name and the total number of...

    SQL SERVER Write a SELECT statement that return the vendor name and the total number of accounts that apply to that vendor’s invoices. Filter the result set to include only the vendor who is being paid more than twice. (HINT: use Vendors table, Invoices table and InvoiceLineItems table). TABLES: Vendor Columns: VendorID, VendorName, DefaultTermsID, DefaultAccountNo Invoices Columns: InvoiceID, VendorID, InvoiceNumber, InvoiceDate, InvoiceTotal, PaymentTotal, CreditTotal, TermsID InvoiceLineItems Columns: InvoiceID, InvoiceSequence, AccountNo, InvoiceLineItemAmount, InvoiceLineItemDescription

  • -- Schema definition create table Customer (     cid   smallint not null,     name varchar(20),    ...

    -- Schema definition create table Customer (     cid   smallint not null,     name varchar(20),     city varchar(15),     constraint customer_pk         primary key (cid) ); create table Club (     club varchar(15) not null,     desc varchar(50),     constraint club_pk         primary key (club) ); create table Member (     club varchar(15) not null,     cid   smallint     not null,     constraint member_pk         primary key (club, cid),     constraint mem_fk_club         foreign key (club) references Club,     constraint mem_fk_cust...

  • In the invoices table, the invoiceNumber values are stored as VARCHAR; however, there are some rows...

    In the invoices table, the invoiceNumber values are stored as VARCHAR; however, there are some rows where the invoiceNumber values represent valid integers. Print the invoiceID and the invoiceNumber cast to int for only those rows that can be so cast. (Do not print NULL values.) Hint: Either TRY_CAST from chapter eight or ISNUMERIC from chapter nine would be useful.

  • CREATE TABLE Gender ( gender CHAR(1), description VARCHAR(10), PRIMARY KEY (gender) ); CREATE TABLE People (...

    CREATE TABLE Gender ( gender CHAR(1), description VARCHAR(10), PRIMARY KEY (gender) ); CREATE TABLE People ( ID INT, name VARCHAR(50), gender CHAR(1), height FLOAT, PRIMARY KEY (ID), FOREIGN KEY (gender) REFERENCES Gender (gender) ); CREATE TABLE Sports ( ID INT, name VARCHAR(50), record FLOAT, PRIMARY KEY (ID), UNIQUE (name) ); CREATE TABLE Competitions ( ID INT, place VARCHAR(50), held DATE, PRIMARY KEY (ID) ); CREATE TABLE Results ( peopleID INT NOT NULL, competitionID INT NOT NULL, sportID INT NOT NULL,...

  • Hello, Does anyone have any input on how to fragment this table in visual studio? CREATE...

    Hello, Does anyone have any input on how to fragment this table in visual studio? CREATE TABLE [dbo].[Stock] ( [itemNo] INT NOT NULL , [store] VARCHAR(50) NOT NULL, [qtyOnHand] INT NULL, [qtyHand] INT NULL, [qtyOnOrder] INT NULL, [reorderPoint] INT NULL, CONSTRAINT [FK_Stock_Item] FOREIGN KEY ([itemNo]) REFERENCES [Item]([itemNo]), CONSTRAINT [FK_Stock_Store] FOREIGN KEY ([store]) REFERENCES [Store]([storeName]), CONSTRAINT [PK_Stock] PRIMARY KEY ([itemNo], [store]), The other tables are: CREATE TABLE [dbo].[Item] ( [itemNo] INT NOT NULL PRIMARY KEY, [itemName] VARCHAR(50) NULL, [supplier] VARCHAR(50) NULL,...

  • SQL CHECK CONSTRAINT AND TEST CASE... SQL Query Here are the tables: CREATE TABLE Movies(     movieID...

    SQL CHECK CONSTRAINT AND TEST CASE... SQL Query Here are the tables: CREATE TABLE Movies(     movieID INT,     name VARCHAR(30) NOT NULL,     year INT,     rating CHAR(1),     length INT,     totalEarned NUMERIC(7,2),     PRIMARY KEY(movieID),     UNIQUE(name, year) ); CREATE TABLE Showings(     theaterID INT,     showingDate DATE,     startTime TIME,     movieID INT,     priceCode CHAR(1),     PRIMARY KEY(theaterID, showingDate, startTime),     FOREIGN KEY(theaterID) REFERENCES Theaters,     FOREIGN KEY(movieID) REFERENCES Movies ); CREATE TABLE Tickets(     theaterID INT,     seatNum INT,     showingDate DATE,     startTime TIME,     customerID INT,     ticketPrice NUMERIC(4,2),     PRIMARY KEY(theaterID, seatNum, showingDate, startTime)...

  • CREATE TABLE vendor ( vendor_id int NOT NULL, vendor_name char(50) NOT NULL, contact_name char(50), CONSTRAINT vendors_pk...

    CREATE TABLE vendor ( vendor_id int NOT NULL, vendor_name char(50) NOT NULL, contact_name char(50), CONSTRAINT vendors_pk PRIMARY KEY (vendor_id) ); What is the code to add the foreign key for this table? Can you please add the entire code with mine included. Thank you in advance.

  • CREATE TABLE Users (               userId varchar (30) NOT NULL,               pass varchar (30),     

    CREATE TABLE Users (               userId varchar (30) NOT NULL,               pass varchar (30),               fname varchar (50),               lname varchar (50),               email varchar (50),               gender char(1),               age integer,               banned boolean,               PRIMARY KEY (userId),               UNIQUE(email)) CREATE TABLE FavSellers (               userId varchar (30),               sellerId varchar (30),               PRIMARY KEY (userId, sellerId),               FOREIGN KEY(userId) references Users,               FOREIGN KEY(sellerId) references Users(userId)) CREATE TABLE Items (               itemId integer,               title varchar (50),              ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT