Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


559 posts

Ultimate Geek
+1 received by user: 2

Trusted

Topic # 58556 14-Mar-2010 16:52
Send private message

how would i go about chaging the value of something ive added in a database in sql

eg. originally i inserted this.

Insert into Fruit values
(1, 'Apple', '$4.99'),
(2, 'Orange','$3.99'),

what script would i use to change oranges to 2.99?
is it update?

its for my uni submission but i cant experiment on sql because my computer has flipped out.

Create new topic
42 posts

Geek


  Reply # 307114 14-Mar-2010 16:59
Send private message

update fruit set <whatever the $ value column is called>='$2.99'
where <whatever you have called the id column>=2 



559 posts

Ultimate Geek
+1 received by user: 2

Trusted

  Reply # 307115 14-Mar-2010 17:03
Send private message

thank you!

352 posts

Ultimate Geek


  Reply # 307162 14-Mar-2010 19:14
Send private message

you're storing the amounts as a string? really?
you should store numeric data as a numeric data type. do the formatting later, in the application or report.

assuming you've changed the amount datatype to a number:
update fruit set amount = 2.99 where fruit_id = 2;

otherwise as string:
update fruit set amount = '$2.99' where fruit_id = 2;

6315 posts

Uber Geek
+1 received by user: 380

Moderator
Trusted
Lifetime subscriber

  Reply # 307226 14-Mar-2010 22:39
Send private message

rvangelder: you're storing the amounts as a string? really?
you should store numeric data as a numeric data type. do the formatting later, in the application or report.


+1

Definitely start good habits now, plenty of reasons why to store data correctly - uses less space, better performance, easier to write queries etc.



3 posts

Wannabe Geek


  Reply # 307528 15-Mar-2010 20:31
Send private message

PS The insert will fail...

You need to specify destination columns in your insert query i.e.
Insert into Fruit(IDField,FruitName,Price) values
(1, 'Apple', '$4.99'),
(2, 'Orange','$3.99'),

Also rather use an autonumber field for the first column as this will allow you to index your datatables making queries faster.

Just my humble opinion. :)
 

352 posts

Ultimate Geek


  Reply # 307577 15-Mar-2010 22:11
Send private message

I think it's good practise to name the columns too, but his SQL won't fail for that reason.
Named columns are optional (depending on the database).
http://download.oracle.com/docs/cd/B14156_01/doc/B13812/html/sqcmd.htm#BABGFGDA

A few tidy ups for those inserts
1 insert statement per row of data:
insert into fruit (fruit_id, name, amount) values (1, 'Apple', 4.99);
insert into fruit (fruit_id, name, amount) values (2, 'Orange', 3.99);

If you're developing an application, bind parameters wherever you can.
Not only will it increase performance by saving the database from re-parsing your SQL and calculating a query path for every line, it can protect your application from a security problem called "sql injection". http://xkcd.com/327/. You'll immediately notice vulnerable websites - those that can't handle an apostrophe in a text field.

Create new topic

Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Hawaiki Transpacific cable ready-for-service
Posted 20-Jul-2018 11:29


Microsoft Dynamics 365 Business Central launches
Posted 10-Jul-2018 10:40


Spark completes first milestone in voice platform upgrade
Posted 10-Jul-2018 09:36


Microsoft ices heated developers
Posted 6-Jul-2018 20:16


PB Technologies charged for its extended warranties and warned for bait advertising
Posted 3-Jul-2018 15:45


Almost 20,000 people claim credits from Spark
Posted 29-Jun-2018 10:40


Cove sells NZ's first insurance policy via chatbot
Posted 25-Jun-2018 10:04


N4L helping TAKA Trust bridge the digital divide for Lower Hutt students
Posted 18-Jun-2018 13:08


Winners Announced for 2018 CIO Awards
Posted 18-Jun-2018 13:03


Logitech Rally sets new standard for USB-connected video conference cameras
Posted 18-Jun-2018 09:27


Russell Stanners steps down as Vodafone NZ CEO
Posted 12-Jun-2018 09:13


Intergen recognised as 2018 Microsoft Country Partner of the Year for New Zealand
Posted 12-Jun-2018 08:00


Finalists Announced For Microsoft NZ Partner Awards
Posted 6-Jun-2018 15:12


Vocus Group and Vodafone announce joint venture to accelerate fibre innovation
Posted 5-Jun-2018 10:52


Kogan.com to launch Kogan Mobile in New Zealand
Posted 4-Jun-2018 14:34



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.