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!

 
 
 
 


Try Wrike: fast, easy, and efficient project collaboration software
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;

6314 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 »

New FUJIFILM X-T100 aims to do better job than smartphones
Posted 24-May-2018 20:17


Stuff takes 100% ownership of Stuff Fibre
Posted 24-May-2018 19:41


Exhibition to showcase digital artwork from across the globe
Posted 23-May-2018 16:44


Auckland tops list of most vulnerable cities in a zombie apocalypse
Posted 23-May-2018 12:52


ASB first bank in New Zealand to step out with Garmin Pay
Posted 23-May-2018 00:10


Umbrellar becomes Microsoft Cloud Solution Provider
Posted 22-May-2018 15:43


Three New Zealand projects shortlisted in IDC Asia Pacific Smart Cities Awards
Posted 22-May-2018 15:14


UpStarters - the New Zealand tech and innovation story
Posted 21-May-2018 09:55


Lightbox updates platform with new streaming options
Posted 17-May-2018 13:09


Norton Core router launches with high-performance, IoT security in New Zealand
Posted 16-May-2018 02:00


D-Link ANZ launches new 4G LTE Dual SIM M2M VPN Router
Posted 15-May-2018 19:30


New Panasonic LUMIX FT7 ideal for outdoor: waterproof, dustproof
Posted 15-May-2018 19:17


Ryanair Goes All-In on AWS
Posted 15-May-2018 19:14


Te Papa and EQC Minecraft Mod shakes up earthquake education
Posted 15-May-2018 19:12


Framing Facebook: It’s not about technology
Posted 14-May-2018 16:02



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.