You are on page 1of 21

SQL Statements

Before starting...
1. Open MySQL Query Browser 2. Create a stock table - int: IdStock {PK} - VarChar: Name {Not Null} - VarChar: Description

INSERT
The INSERT INTO statement is used to insert new records in a table. Stock (Before)
idStock 1001 Name Green Pen Description This pen is color green

Stock (After)
idStock 1001 1002 Name Green Pen Yellow Pad Description This pen is color green Used for writing

INSERT
First Form: INSERT INTO <table_name> VALUES (<value1>, <value2>, <value3,...>) Second Form: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

INSERT (1 Form)
The first form doesn't specify the column names where the data will be inserted, only their values: Stock (Before)
IdStock {PK} 1001 Name Green Pen Description This pen is color green

st

INSERT INTO STOCK VALUES (1002,Yellow Pad, Used for writing); Stock (After)
idStock 1001 1002 Name Green Pen Yellow Pad Description This pen is color green Used for writing

INSERT (2

nd

Form)

The second form specifies both the column names and the values to be inserted: Stock (Before)
IdStock {PK} 1001 Name Green Pen Description This pen is color green

INSERT INTO STOCK (idStock, Name) VALUES (1002,Yellow Pad); Stock (After)
idStock 1001 1002 Name Green Pen Yellow Pad Description This pen is color green

UPDATE
The UPDATE statement is used to update existing records in a table. Stock (Before)
IdStock {PK} 1001 Name Green Pen Description This pen is color green

Stock (After)
idStock 1001 Name Green Pen Description This pen is obviously color green

UPDATE
UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value

UPDATE
Stock (Before)
IdStock {PK} 1001 Name Green Pen Description This pen is color green

UPDATE stock SET description=This pen is obviously color green WHERE idStock=1001 Stock (After)
idStock 1001 Name Green Pen Description This pen is obviously color green

UPDATE: Warning
Be careful when updating records. Omitting the WHERE clause will update all of the records in the specified field:
idStock 1001 1002 Name Green Pen Yellow Pad Description This pen is obviously color green This pen is obviously color green

DELETE
The DELETE statement is used to delete rows in a table. Stock (Before)
idStock 1001 1002 Name Green Pen Yellow Pad Description This pen is obviously color green This pen is obviously color green

Stock (After)
IdStock {PK} 1001 Name Green Pen Description This pen is obviously color green

DELETE
DELETE FROM table_name WHERE some_column=some_value

DELETE
Stock (Before)
idStock 1001 1002 Name Green Pen Yellow Pad Description This pen is obviously color green This pen is obviously color green

DELETE FROM STOCK WHERE IDSTOCK = 1002 Stock (After)


IdStock {PK} 1001 Name Green Pen Description This pen is obviously color green

DELETE: Warning
Be careful when updating records. Omitting the WHERE clause will delete all of the records in the table: Delete from STOCK; Stock
IdStock {PK} Name Description

Another alternative way to write it is: Delete * from Stock

Coding: INSERT INTO

String query = INSERT INTO STOCK Values (1001, Yellow Pad, Used for Writing); Statement stmt = con.createStatement(); int code = stmt.executeUpdate(query);

Coding: Delete

String query = UPDATE stock SET description='This pen is obviously color green' WHERE idStock=1001 Statement stmt = con.createStatement(); int code = stmt.executeUpdate(query);

Prepared Statement
Parameterized Statement Used when an SQL Statement is to be executed multiple times with different values only Can do everything a Statement object can do Use setXXX methods to update values

Coding: Prepared Statement


try{ String query = insert into stocks VALUES (?,?,?); PreparedStatement pstmt = con.prepareStatement(query); pstmt.setInt(1, 2001); pstmt.setString(2, C2 Lemon Green Tea); pstmt.setString(3, Refreshing Drinks); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }

MP2 Specs:

Overview: Create a database application that registers, authenticates, edits and deletes a user. Individual work

MP2 Specs:
System Details: A user should be able to register to register into the system. Data captured from a user are his name, address, username and password. Once a user has registered in the system, he should be able to login into your system. If the user enters a valid username and password combination, he should be able to view his own profile based on the registration form. Otherwise, the system should notify that he entered an invalid username and password combination. Once the user has entered the system, he should be able to update his current details and delete his account.

MP2 Specs:
Technical Requirements: Must use prepared statements. May use any method of DB connection. Must have GUI Submission: Next week Monday (Oct. 24 2011) demo in class

You might also like