Common SQL query scripts

Source: Internet
Author: User
This article assumes that the reader has some knowledge about the database connection technology, so he only discusses the syntax of SQL query commands.
The table structure is as follows (MS Access 2000 ):
Table Name: usertable
----------------------
Field Name field type
----------------------
Automatic userid count (long integer)
Username text
Usersex text
Createdate date/time (default value: Now ())
----------------------
1. Use the SELECT command to extract records
1. Retrieve all records in the table
Select command, the statement is as follows:
"Select * From usertable"
2. Obtain the USERID field record in the table
"Select userid from usertable"
3. Obtain the userid and usersex field records in the table.
"Select userid, usersex from usertable"
4. Obtain the usersex value "male" record in the table.
"Select from usertable where usersex = 'male '"
5. Obtain the record containing the word "Zhang" in the username value of the table.
"Select from usertable where username like '% male % '"
6. Retrieve all records in the table, sorted in descending order by values of the createdate Field
"Select from usertable order by createdate DESC"
Note:
1) The preceding commands can be used in combination. For example, if the username in the table contains the "hao" word, the userid value, and sorted in descending order by the time of the createdate value, the command is as follows:
"Select userid from usertable where username like '% Hao %' order by createdate DESC"
2) in use, note that if the field type is text, single quotation marks should be added out of the condition word during condition discrimination. This rule applies to any SQL query command.
3) "%" in the condition word is a wildcard.
4) when there are more than one criterion, they should be connected by "and" or.

2. Use the insert into command to insert a new record
1. Insert a new record
"Insert into usertable (username, usersex) values ('zhang haonan ', 'male ')"
Note:
Note that the value type of the inserted record must comply with the field type in the table; otherwise, an error occurs. In fact, using single quotes is only a convenient method, but it is not standard, because if the record to be inserted contains single quotes, an error will occur (although this is not often the case, but it may exist ). So we 'd better use a custom function to replace single quotes. The method is as follows:
Function sqlstr (data)
Sqlstr = "'" & replace (data ,"'","''")&"'"
End Function
In the command, it is:
Dim name, sex
Name = "Zhang haonan"
Sex = "male"
"Insert into usertable (username, usersex) values (" & sqlstr (name) & "," & sqlstr (sex )&")"
In this way, no error occurs regardless of whether the value contains single quotes or double quotation marks.
However, note that this rule applies only to fields of the text type. If it is of another type, you do not need to consider it, that is, you do not need to add any symbols.

3. update records
1. Update all fields of all records
"Update usertable set userid = 1, usersex = 'male', username = 'xuyong ', createdate =" & cdate (createdate)
Note:
You can use an arithmetic expression, such as userid = userid + 1.
2. Update the record value with the username field value "Xu Yong"
"Update usertable set userid = 1, username = 'xu wei' where username = 'xu yong '"
Note:
When you need to combine conditional queries, the method is the same as select. Pay attention to the single quotation marks.

4. delete records
1. Delete all records
"Delete from usertable"
2. delete records with specific conditions. For example, delete a record whose userid is "20 ".
"Delete from usertable where userid = 20"
3. delete a combination condition record. For example, if usersex is set to "female", username contains "Zhang" records.
"Delete from usertable where usersex = 'female 'and username like' % '"
Note:
Pay attention to single quotes.

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.