Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 URGENT.CREATE TABLE ERROR.

Author  Topic 

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-09-28 : 11:51:16
Please I have a problem executing the queries for this questions below. Can someone please help me out?


create table transaction.orderdetals
PurchaseOrderID must be auto generated
OrderDate should not be greater than the current date.
If the order date is not entered,the current date should be taken as the default date
QuantityOrdered,QuantityReceived, and UnitPrice should be greater than 0
Quantity received cannot be greater than QuantityOrdered.
QuantityReceived should allow NULL.
QuantityReceived should be added to QuantityInHand in the Items table
When a record is inserted into the table,QuantityInHand in the Items table should be updated automatically
OrderStatus must be any of the followinf values: `In Transit' , `Received' , or `Cancelled'.
ReceivingDate should allow NULL and should be greater than OrderDate.

Best Regards.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-09-28 : 14:53:06
Show us what you have so far. We can help you with your homework, but you must show some effort.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-29 : 03:32:03
start off with this

http://technet.microsoft.com/en-us/library/ms174979(v=sql.105).aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-09-29 : 13:42:24
Okay tkizer. Guess you'll please help point out my errors and also help fill those loopholes i couldnt fill myself. Thanks alot.

CREATE DATABASE SHOPHERE
CREATE TABLE ITEMS
(
CategoryID Int,
Categoryname Varchar (200) Not null ,
CategoryDescription Varchar (200) Not null,
)
INSERT INTO ITEMS (Categoryname, CategoryDescription)
VALUES ('Household','Toys,Iron,Bed,Pillow')
INSERT INTO ITEMS (Categoryname, CategoryDescription)
VALUES ('Sports','Ball,Boots,Socks,Gloves')
INSERT INTO ITEMS (Categoryname, CategoryDescription)
VALUES ('Accessories/Clothing','Shirt,skirt,Blouse,Trousers')

CREATE TABLE SUPPLIER
(
SupplierID int,
FirstName Varchar (150) Not null,
LastName Varchar (100) Not null,
Address Varchar (80) Not Null,
Phone Char (50) Not null,
Country Varchar (100) Not Null,
)
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Aziz','Ansah','p.o.box MB485','123456789','Ghana')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Raul','Bravo','p.o.box 219','213456798','Spain')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Dede','Ayew','5th Melon Street','129456783','Benin')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Cristiano','Ronaldo','4th Market Avenue','120456783','Portugal')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Zenden','Morn','1st Pepper Lane','123006789','Sweden')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Louis','Figo','9th Soccer Road','123552789','Porto')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Ronaldinho','Robinho','5th Nike Fan','126256789','Usa')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Varane','Modric','1st Madrid Street','120000798','Spain')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Lionel','Messi','2nd Barcelona Lane','123987654','Barcelona')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Frank','Lampard','West London street','100056789','England')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Louis','Swarez','5th Deustchland','123422789','Holland')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Arjen','Robben','4th turkey Lane','125286789','Lebanon')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Nasr','Ansah','p.o.box 521','123456009','Malaysia')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Berbatov','Ozil','p.o.box 419','123450419','Greece')
INSERT INTO Supplier (FirstName, LastName, Address, Phone, Country)
VALUES ('Roberto','Carlos','Real Madrid','103452789','Brazil')

CREATE TABLE ITEMDETAILS
(
ItemID int,
ItemName Varchar (50) not null,
ItemDescription Varchar (50) Not null,

CREATE TABLE TRANSACTIONS.ORDERDETAILS
(
PurchaseID INT,IDENTITY (2,2),
OrderDate Datetime , CONSTRAINT CHECK_ORDERDATE CHECK (ORDERDATE > DATETIME)
third point,I dont know how to create it.
QuantityOrdered INT, QUANTITYORDERED INT,
CONSTRAINT CHECK_QUANTITYORDERED CHECK (QUANTITYORDERED > 0 ),
QuantityReceived INT,NULL,QUANTITYRECEIVED INT,
CONSTRAINT CHECK_QUANTITYRECEIVED CHECK (QUANTITYRECEIVED > 0 ),
UnitPrice Money, UNITPRICE MONEY,
CONSTRAINT CHECK_UNITPRICE MONEY CHECK (CHECK_UNITPRICE > 0 ),
ReceivingDate Datetime ,null ,
OrderStatus Varchar(50),
Eight point, I dont know how to create.
)

Best Regards.
Go to Top of Page

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-09-29 : 13:52:12
Sorry I copied everything. But i think you can reference to the last table which i have difficulty with.

Best Regards.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-29 : 17:16:07
You have too many comma's all over the place. The syntax for creating a table has very specific rules. Take a look at this page for the rules and examples: http://technet.microsoft.com/en-us/library/ms174979.aspx If the rule set looks too dense, look at the examples they have on that page.

CREATE TABLE TRANSACTIONS.ORDERDETAILS
(
PurchaseID INT IDENTITY (2,2),
OrderDate Datetime CONSTRAINT CHECK_ORDERDATE CHECK (ORDERDATE > DATETIME),

-- don't know what you mean by this
--third point,I dont know how to create it.

QUANTITYORDERED INT CONSTRAINT CHECK_QUANTITYORDERED CHECK (QUANTITYORDERED > 0 ),
QUANTITYRECEIVED INT CONSTRAINT CHECK_QUANTITYRECEIVED CHECK (QUANTITYRECEIVED > 0 ),
UNITPRICE MONEY CONSTRAINT CHECK_UNITPRICE_MONEY CHECK (CHECK_UNITPRICE > 0 ),
ReceivingDate Datetime null ,
OrderStatus Varchar(50)

-- don't know what you mean by this
--Eight point, I dont know how to create.
)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-09-30 : 03:28:56
[code]CHECK_ORDERDATE CHECK (ORDERDATE > GETDATE())[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-30 : 06:25:18
OrderDate should not be greater than the current date.


shouldnt check constraint be this?

..
CHECK_ORDERDATE CHECK (ORDERDATE < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1))


If the order date is not entered,the current date should be taken as the default date

for this you need a DEFAULT constraint also on it

OrderDate Datetime DEFAULT(GETDATE())

also if you want to enforce this for UPDATES too you need an UPDATE trigger

QuantityReceived should be added to QuantityInHand in the Items table
You'll need a trigger on table for insert,update to add values to Item table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-10-01 : 03:07:50
James am telling you that I don't know how to create the third and Eight points, respectively

Best Regards.
Go to Top of Page

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-10-01 : 19:00:24
please if you are asked a question like this how will you go about it.

Phone number must be in the following format : '[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]'

I tried to build a rule, and sp_bindrule to bind the rule to the table. But when inserting values it give me errors. So please I will like you to help me out or anyone else around ..

Thanks alot brothers.

Best Regards.

Best Regards.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-01 : 21:31:41
quote:
Originally posted by Hinduson

please if you are asked a question like this how will you go about it.

Phone number must be in the following format : '[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]'

I tried to build a rule, and sp_bindrule to bind the rule to the table. But when inserting values it give me errors. So please I will like you to help me out or anyone else around ..

Thanks alot brothers.

Best Regards.

Best Regards.

Use exactly the expression you posted in your check constraint
CHECK (phoneno LIKE  '[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]')
Go to Top of Page

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-10-02 : 14:21:16
Thanks Bro. I really Appreciate

Best Regards.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-02 : 15:46:16
You are very welcome - glad to help.
Go to Top of Page

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-10-02 : 21:18:51
This questions are killing me . And the solution VISAKH16 gave me are not working.

Please can someone help me out.

Create table Transaction.OrderDetails
*PurchaseOderID must be auto generated
*OrderDate should not be greater than the current date
*If the order date is not entered,the current date should be taken as the default date
*QuantityOrdered,QuantityReceived, and UnitPrice should be greater than 0
*QuantityReceived should allow NULL.
*Quantity received cannot be greater than QuantityOrdered.
*QuantityReceived should be added to QuantityInHand in the Items table
*When a record is inserted into the table,QuantityInHand in the Items table should be updated automatically
*OrderStatus must be any of the followinf values: `In Transit' , `Received' , or `Cancelled'.
*ReceivingDate should allow NULL and should be greater than OrderDate.

Best Regards.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-04 : 01:50:20
quote:
Originally posted by Hinduson

This questions are killing me . And the solution VISAKH16 gave me are not working.

Please can someone help me out.

Create table Transaction.OrderDetails
*PurchaseOderID must be auto generated
*OrderDate should not be greater than the current date
*If the order date is not entered,the current date should be taken as the default date
*QuantityOrdered,QuantityReceived, and UnitPrice should be greater than 0
*QuantityReceived should allow NULL.
*Quantity received cannot be greater than QuantityOrdered.
*QuantityReceived should be added to QuantityInHand in the Items table
*When a record is inserted into the table,QuantityInHand in the Items table should be updated automatically
*OrderStatus must be any of the followinf values: `In Transit' , `Received' , or `Cancelled'.
*ReceivingDate should allow NULL and should be greater than OrderDate.

Best Regards.


Which solution is not working for you?

see illustration below. its working fine for me


CREATE TABLE #Test
(
ID int IDENTITY(1,1),
ORDERDATE datetime DEFAULT (GETDATE()),
Value varchar(100),
CONSTRAINT CHECK_ORDERDATE CHECK (ORDERDATE < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1))
)
--this will be success
INSERT #Test (ORDERDATE,Value)
VALUES (GETDATE()-5,'test 1')

--this will throw check constraint error
INSERT #Test (ORDERDATE,Value)
VALUES (GETDATE()+2,'test 2')

--this will put default value for date which will be current days date
INSERT #Test (Value)
VALUES ('test 3')


select * from #Test


output
------------------------------------------

Msg 547, Level 16, State 0, Line 13
The INSERT statement conflicted with the CHECK constraint "CHECK_ORDERDATE". The conflict occurred in database "tempdb", table "dbo.#Test_______________________________________________________________________________________________________________000000000005", column 'ORDERDATE'.
The statement has been terminated.


ID ORDERDATE Value
----------- ----------------------- ----------------------------------------------------------------------------------------------------
1 2013-09-29 11:19:48.270 test 1
3 2013-10-04 11:19:48.273 test 3




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-10-04 : 07:11:04
Let me check it out now, and thanks in Advance Brother.

Best Regards.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-04 : 07:42:09
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Hinduson
Yak Posting Veteran

69 Posts

Posted - 2013-10-07 : 19:06:31
Please how do I write the queries for the questions Below?

*QuantityReceived should be added to QuantityInHand in the Items table.
*When a record is inserted into the table,QuantityInHand in the Items table should be updated automatically


Best Regards.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-07 : 23:35:30
quote:
Originally posted by Hinduson

Please how do I write the queries for the questions Below?

*QuantityReceived should be added to QuantityInHand in the Items table.
*When a record is inserted into the table,QuantityInHand in the Items table should be updated automatically


Best Regards.


create a trigger on orderdetails and put logic inside it to add the value to Items table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -