Data Science Explorer

[Basic Grammar] SQL UPDATE Statement 본문

SQL

[Basic Grammar] SQL UPDATE Statement

grace21110 2023. 10. 14. 11:45
반응형

The UPDATE statement is used to modify the existing records in a table. 

 

  • UPDATE Syntax 
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

** Tip: Notice the WHERE clause in the UPDATE statement.**

 

  • UPDATE Table 

Example 

Update the first customer (CustomerID = 60) with a new contact person and a new city.

UPDATE Customers 
SET ContactName = 'Pearl', City = 'Seoul'
WHERE CustomerID = 60;

 

  • UPDATE Multiple Records 

It is the WHERE clause that determines how many records will be updated. 

 

Example

Update the BrandName to "Kim's" for all records where city is "Salt Lake City":

UPDATE Brand
SET BrandName = 'Kim's'
WHERE City = 'Salt Lake City';

 

** WARNING: If you omit the WHERE clause, ALL records will be updated!! **

 

Exercises 

1. Update the Price column of all records in the "Products" table. Set the Price to $29.99 for all products.

UPDATE Products
SET Price =  $29.99 ;

 

2. Update the EmployeeName column of all records in the "Employees" table. Set the EmployeeName to 'John Doe' for all employees.

UPDATE Employees 
SET EmployeeName ='John Doe';

 

3. Update the "ProductCategory" to 'Electronics' for all products in the "Products" table that have a "Price" greater than $100.

UPDATE Products 
SET ProductCategory = 'Electronics'
WHERE Price > $100;

 

 

'SQL' 카테고리의 다른 글

[Basic Grammar] SQL MIN() and MAX() Functions  (0) 2023.10.14
[Basic Grammar] SQL DELETE Statement  (0) 2023.10.14
[Basic Grammar] SQL NULL Values  (0) 2023.10.14
[Basic Grammar] SQL INSERT INTO Statement  (1) 2023.10.14
[Basic Grammar] SQL NOT Operator  (0) 2023.10.13