Common additions and deletions to SQL example

Source: Internet
Author: User

First, insert data

(1)INSERT into Persons VALUES (' Gates ', ' Bill ', ' xuanwumen ', ' Beijing ')

(2) INSERT into Persons (LastName, Address)

VALUES (' Wilson ', ' champs-elysees '), (' Wilson2 ', ' champs-elysees2 ')

(3) INSERT into Persons select * from Persons2

(4) SELECT lastname,firstname to persons_backup from Persons


Ii. deletion of data

(1)DELETE from person WHERE LastName = ' Wilson '

(2)drop person


Third, modify the data

(1)UPDATE person SET Address = ' Zhongshan ", city = ' Nanjing '

WHERE LastName = ' Wilson '

(2)update P Set p.address = ' Zhongshan 23 '

from person p INNER join city c on P.cityid = c.id where c.name = ' Beijing '


Iv. Query Statements

1. General Enquiry:

(1) SELECT lastname,firstname from Persons WHERE city= ' Beijing '

ORDER by company DESC, OrderNumber ASC

(2) SELECT DISTINCT Company from Orders

(3) SELECT * from Persons WHERE (firstname= ' Thomas ' OR firstname= ' William ')

and Lastname= ' Carter '

(4) SELECT * from Persons WHERE LastName in (' Adams ', ' Carter ')

(5) SELECT * from Persons WHERE LastName not between ' Adams ' and ' Carter '

(6) SELECT TOP 2 * from Persons (SQL Server only)

(7) SELECT * from Persons LIMIT 5 (MySQL only)

(8) SELECT Po. OrderID, P.lastname, p.firstname from Persons as P,

Product_orders as Po WHERE p.lastname= ' Adams ' and p.firstname= ' John '(alias)

(9) Select column_name (s) from table_name1 UNION all SELECT column_name (s) from Table_name2 (merge result set, union All is directly connected, fetch all values,

Records may be duplicated; Union is a unique value, record is not duplicated)

2. Like query

(1) SELECT * from Persons WHERE city like '% ' + ' N ' + '% '

(2) SELECT * from Persons WHERE city like ' N% '(this method may be used for indexing)

(3) SELECT * from user where userName like ' Hair _1_2 '(_ replaces only one character)

(4) SELECT top * from user where userId like ' [23]% '

([charlist] Word columns any single character, only SQL Server is useful)

(5) SELECT top * from user where userId like ' [^23]0% '

([^charlist] any single character not in Word columns, only SQL Server is useful)

3. Related queries

SELECT Persons.lastname, Persons.firstname, Orders.orderno from Persons

INNER JOIN Orders on persons.id_p = orders.id_p ORDER by persons.lastname

(1) JOIN: Returns a row if there is at least one match in the table

(2) Left JOIN: Returns all rows from the table, even if there is no match in the right table

(3) Right JOIN: Returns all rows from the correct table even if there is no match in the left table

(4) Full JOIN: Returns a row if there is a match in one of the tables



Common additions and deletions to SQL example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.