Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ds Labsession 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

LAB SESSION 5

DATABASE SYSTEMS

DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY

JINNAH UNIVERSITY FOR WOMEN


5-C NAZIMABAD, KARACHI 74600
LAB SESSION 5: WORKING WITH MORE DML
USING TOP
The TOP operator is Microsoft extension in transact SQL that allows us to limit the number of rows we
want in our result set. After the query has been processed, the result set is limited to the number of
rows specified. We can either use a hard number like 10, or include the PERCENT keyword to indicate
that we want to see only the percentage of the full result set.

EXAMPLE: SELECT TOP 10 * FROM EMPLOYEES

SELECT TOP 10 PERCENT * FROM EMPLOYEES

(Note the difference in the results of both)

This query will show the records of top 10 employees in the database.

INSERTING COMMENTS IN A QUERY


Transact SQL recognizes two types of comments:

SINGLE LINE COMMENT (--)


Select top 10 * from employees – single line comment (I can write any thing on this line)

MULTI LINE COMMENT (/*…………………….*/)

Select top 10 * from employees/*I can write

Any thing

On this area*/

USING LITERALS
Literals are letters, numerals or symbols that are used as specific value in a result set to increase
readability. The usage is illustrated in the following examples:

SELECT 'COMPANYNAME: '+ COMPANYNAME AS CNAME FROM CUSTOMERS

SELECT 'COMPANYNAME: ‘, COMPANYNAME AS CNAME FROM CUSTOMERS

The ‘companyname: ‘ is literal in the above queries but result will be different for both queries.

INSERT STATEMENT
The insert statement adds rows to a table. The syntax is:

INSERT [INTO] <TABLE_NAME> [(COLUMN_LIST)]


VALUES [(VALUES_LIST) | SELECT STATEMENT]

Insert is used with the values clause to add rows. When inserting rows, consider the following points:

• Use the column list to specify the columns that will store each incoming value. If values for all
columns are supplied then column list is optional.
• The actual values to be inserted to go in the values_list.
• If you have provided column names in the column_list then each and every values after values
clause (i.e. values_list) should correspond to the column list. If column_list is not provided
then values for all the table fields must be supplied.
• Data should be inserted in correct format. For example, characters or date must be enclosed
in single quotation marks.
• Into clause is also optional.
• Insert statement will fall if it violates an existing constraint or rule.

The following example inserts a new record in the employees table. INSERT

INTO CUSTOMERS

(CUSTOMERID, COMPANYNAME, CONTACTNAME, CONTACTTITLE, ADDRESS, CITY, REGION,


POSTALCODE, COUNTRY, PHONE, FAX)

VALUES(‘ABCD’, ’JOHN ENTERPRISES’, ‘JOHN MICHAEL’, ‘OWNER’, ‘760 MASTER STREET ’, ‘BERGS’,
‘AREA’, ‘G785’,’CANADA’, ’(603) 588-8980’, ‘(603) 322-7490’)

INSERTING PARTIAL DATA


If a column has a default value or accepts Null values then you can omit the column from insert
statement, SQL server automatically inserts the values. Remember following points for partial data
insertion:

List only the column names for the data that you are supplying in the insert statement.

Enter a Null value explicitly by typing Null without single quotation marks.

The following example adds the last name in the table employees.

INSERT INTO EMPLOYEES

(FIRSTNAME, LASTNAME)

VALUES ('MICHAEL','JOHN')

UPDATE STATEMENT
This statement modifies the existing data. Syntax is:

UPDATE <TABLE_NAME>
SET <COLUMN_NAME> = <EXPRESSION>

WHERE <SEARCH_CONDITION>

Use update statement to change single rows, group of rows or all of the rows in a table. Guidelines for
this statement are as under.

• Specify the rows to update with the where clause.


• Specify the new values with the set clause.
• You can change the data in only one table at a time.
• Verify that the input values are the same as the data types that are defined for the

columns. The following example adds 1500 in the salary of all employees

UPDATE EMPLOYEES

SET SALARY = SALARY*10

WHERE EMPLOYEEID = 1

DELETE STATEMENT
Use delete statement to remove one or more rows from a table. Syntax is:

DELETE [FROM] <TABLE_NAME>

WHERE <SEARCH_CONDITION>

Important points for DELETE statement is, SQL server deletes all rows from a table unless you include a
where clause, so do remember to add the where clause, otherwise all the data will be deleted.

DELETE FROM EMPLOYEES

WHERE LASTNAME='JOHN'

The above example will delete only the row with last name as john.

Note: try to avoid deleting existing records. First add a record and then apply deletion to that particular
record.

EXERCISE:
 Run the example queries and observe the results.
 a query that selects starting 20 percent records of the all records of table customers whose
contact title must be sales representative and country must be Germany and contact name
must have ‘N’ as third character. Sort the result by contact name.

 Insert at least 8 records in the each table of your named database you made in previous lab
session.
 Insert using partial data insertion the order id 100 and order name chocolate in table order.
 Modify the table employees of Northwind database by setting salary as triple plus hundred for
employee id 1 to 10.

 Set the first name of employee as john in the employees table of Northwind database whose
id is 1.

 Delete any one record (row) of any table and write query in the manual.
ff

You might also like