The complete collection of SQL statement operations is worth permanent collections
The following statements are part of a MSSQL statement and cannot be used in access.
ddl-Data Definition language (create,alter,drop,declare)
dml-Data Manipulation Language (Select,delete,update,insert)
dcl-Data Control Language (Grant,revoke,commit,rollback)
First, briefly introduce the underlying statement:
1, Description: Create a database
CREATE DATABASE Database-name
2, Note: Delete the database
Drop Database dbname
3, Description: Backup SQL Server
Device to create backup data---
EXEC sp_addumpdevice ' disk ', ' testback ', ' C:mssql7backupmynwind_1.dat '
BACKUP DATABASE pubs to Testback
4, Description: Create a new table
CREATE TABLE TabName (col1 type1 [NOT NULL] [primary key],col2 type2 [NOT NULL],..)
To create a new table from an existing table:
A:create table tab_new like Tab_old (Create a new table using the old table)
B:create table tab_new as Select Col1,col2 ... from tab_old definition only
5, Description: Delete new Table drop table TabName
6, Description: Add a column
Alter table tabname Add column col type
Note: The column will not be deleted after it has increased. The data type can not be changed when the column in the DB2 is added, and the only change is the length of the varchar type.
7, Description: Add PRIMARY key: ALTER TABLE TabName add primary key (COL)
Description: Delete primary key: Alter table tabname drop primary key (COL)
8, Description: CREATE INDEX: Created [unique] index idxname on tabname (col ...)
Delete index: DROP index Idxname
Note: The index is immutable and you want to change that you must delete the rebuild.
9, Description: Creating view: Create VIEW viewname AS SELECT statement
Delete view: Drop view viewname
10, Description: Several simple basic SQL statements
Selection: Select * FROM table1 where to
Insert: INSERT INTO table1 (field1,field2) VALUES (value1,value2)
Remove: Delete from table1 where scope
Updating: Update table1 set field1=value1 where scope
Find: SELECT * FROM table1 where field1 like '%value1% '---the syntax of like is very subtle, look for information! www.phperz.com
Sort: SELECT * from table1 ordered by FIELD1,FIELD2 [desc]
Total: SELECT Count * as TotalCount from table1
Sum: Select SUM (field1) as Sumvalue from table1
Average: Select AVG (field1) as Avgvalue from table1
Max: SelecT Max (field1) as MaxValue from table1
Minimum: select min (field1) as MinValue from table1
11, Description: Several advanced query operators
A:union operator The br> UNION operator derives a result table by combining the other two result tables (such as TABLE1 and TABLE2) and eliminating any duplicate rows in the table. When all is used with union (that is, union ALL), duplicate rows are not eliminated. In both cases, each row of the derived table is either from TABLE1 or from TABLE2. The
EXCEPT operator derives a result table by including all rows that are in TABLE1 but not in TABLE2, and all duplicate rows are eliminated. When all is used with EXCEPT (EXCEPT all), duplicate rows are not eliminated. The
INTERSECT operator derives a result table by including only the rows in TABLE1 and TABLE2 and eliminates all duplicate rows. When all is used with INTERSECT (INTERSECT all), duplicate rows are not eliminated.
Note: Several query result rows that use an operator must be consistent. Www~phperz~com
12, Description: Using outer joins
A, left outer join:
Left-to-right connection (left connection): The result set includes a matching row for the join table and all rows of the left-attached table.
Sql:select a.a, a.b, A.C, B.C, B.D, B.f from-a left-out JOIN b on a.a = B.C
b:right outer join:
Right outer join (right connection): The result set includes both the Matches the connection row, including all rows of the right join table.
C:full outer join:
Full outer join: Includes not only matching rows for symbolic join tables, but also all records in two connected tables.
Second, let's look at some good SQL statements
1, Description: Replication table (only copy structure, source table name: A new table name: B) (Access available)
Law one: SELECT * into B from a where 1 <>1
Method Two: Select top 0 * into B from a
2, Description: Copy table (copy data, source table name: A target table name: B) (Access available)
Insert into B (A, B, c) select d,e,f from B;
3, note: cross-database copy of the table (specific data using absolute path) (Access available)
Insert into B (A, B, c) Select d,e,f from B in ' specific database ' Where condition PHP Programmer's Home
Example:. From B in ' "&server.mappath (". ") & "Data.mdb" & "where.
4, Description: Subquery (table name 1:a table name 2:b)
Select A,b,c from a where a in (select D from B) or: Select A,b,c from a where a where a (1,2,3)
5. Description: Display article, author and final reply time
Select A.title,a.username,b.adddate from Table A, (select Max (adddate) adddate from table where Table.title=a.title) b
6, Description: External connection query (table name 1:a table name 2:b)
Select A.a, A.B, A.C, B.C, B.D, B.f from left-out JOIN b on a.a = B.C
7, Description: Online view query (table name 1:a)
SELECT * FROM (select A,b,c from a) T where t.a > 1;
8, Description: inclusive use, inclusive limit the query data range includes the boundary value, not inclusive does not include
SELECT * FROM table1 where time inclusive time1 and time2
Select A,b,c, from table1 where A is not inclusive numeric 1 and value 2
9, Description: In the use of methods
SELECT * FROM table1 where a [isn't] in (' Value 1 ', ' Value 2 ', ' Value 4 ', ' Value 6 ')
10, Description: Two related tables, delete the main table has not been in the secondary table of information
Delete from table1 where does exists (SELECT * from table2 where table1.field1=table2.field1)
11, note: Four table joint Inquiry question:
SELECT * from a left INNER join B on a.a=b.b right inner join C on A.A=C.C inner join D on A.A=D.D where ....
12. Description: Schedule five minutes advance reminder
Sql:select * from schedule where DateDiff (' minute ', F start time, GETDATE ()) >5
13, Description: A SQL statement to handle the database paging
Select Top b.* from (select Top 20 primary key field, sort field from table name order by sort field desc) A, table name B where B. primary key field = A. primary key field order by a. Sort fields
14, Description: The first 10 records
Select top * form table1 where scope
15, Description: Select in each group B value of the same data in the corresponding a the largest record of all information (similar to the use can be used for the forum monthly rankings, monthly hot product analysis, ranking by subject results, etc.)
Select A,b,c from tablename ta where a= (select Max (a) from TableName TB where tb.b=ta.b)
16. Description: Include all rows in TableA but not in TableB and TableC and eliminate all duplicate rows and derive a result table
(select a from TableA) except (select a-TableB) except (select a from TableC)
17. Description: Randomly remove 10 data
Select Top * FROM tablename ORDER by NEWID ()
18, Description: Random selection of records
Select NEWID ()
(for MySQL randomly selected records)
SELECT * FROM News ORDER by Rand () LIMIT 0,1
19, Note: Delete duplicate records
Delete from TableName where ID. (SELECT max (ID) from tablename GROUP by Col1,col2,...)
20. Description: List all the table names in the database
Select name from sysobjects where type= ' U '
21, Description: List of all the
Select name from syscolumns where id=object_id (' tablename ')
22, Description: Listed type, Vender, PCs fields, sorted by Type field, case can easily implement multiple choices, similar to the case in select.
Select Type,sum (Case vender when ' A ' then pcs else 0), sum (case vender when ' C ' then pcs else 0), sum (case vender when ' B ' th En pcs else 0 end) from tablename GROUP By type
Type Vender pcs
Computer A 1
Computer A 1
CD B 2
CD A 2
Mobile B 3
Mobile C 3
23, Description: Initialization of the table table1
TRUNCATE TABLE table1
24, Description: Select records from 10 to 15
Select Top 5 * FROM (Select the From table order by ID ASC) Table_ alias ORDER by id DESC
Random Methods for selecting database records (using the Randomize function, implemented through SQL statements)
for data stored in a database, the random number feature gives the effect, but they may be too slow. You can't ask an ASP to "find a random number" and print it out. In fact, the common solution is to create a loop that looks like this:
Rnumber = Int (rnd*499) +1
While not objrec.eof
If objrec ("ID") = Rnumber THEN
... Here is the execution script ...
This is easy to understand. First, you take out a random number from 1 to 500 (assuming 500 is the total number of records in the database). You then iterate through each record to test the ID's value and check to see if it matches the rnumber. The code that starts with the then keyword is executed if the condition is met. If your rnumber equals 495, then it takes a long time to cycle through the database. Although the 500 figure looks bigger, it's a small database compared to a more robust enterprise solution, which typically contains thousands of records in a single database. Is this the right time to die? PHP Programmer's Home
with SQL, you can quickly find accurate records and open a recordset that contains only that record, as follows:
Rnumber = Int (rnd*499) + 1
SQL = "SELECT * from Customers WHERE ID =" & Rnumber
Set objrec = objConn.Execute (SQL)
Response.writernumber ; "=" & Objrec ("ID") & "" & Objrec ("C_email")
You don't have to write Rnumber and IDs, you just need to check the match. As long as you are satisfied with the above code work, you can operate "random" records on demand. The recordset does not contain other content, so you will soon be able to find the records you need so that the processing time is greatly reduced.
Talk about random numbers
Now that you are determined to squeeze the last drop of the random function, you may be able to take out more than one random record at a time or want to use a certain random range of records. By extending the standard random example above, you can use SQL to deal with both of these situations.
To remove a few randomly selected records and keep them in the same recordset, you can store three random numbers and then query the database for records that match those numbers:
SQL = "SELECT * from Customers WHERE ID =" & Rnumber & "or id =" & RNumber2 & "or id =" & RNumber3 www.phperz.com
If you want to select 10 records (perhaps 10 links per page load) List, you can use inclusive or mathematical equations to select the first record and the appropriate number of incremental records. This can be done in several ways, but the SELECT statement only shows one possibility (where the ID is an auto-generated number):
SQL = select * from Customers WHERE ID inclusive "& Rnumber & "and" & Rnumber & "+ 9"
Note: The above code is not intended to check whether there are 9 concurrent records in the database.
Randomly read several records, tested
Access syntax: SELECT top * FROM table name ORDER by Rnd (ID)
SQL server:select Top n * FROM table name ORDER by NEWID ()
Mysql:select * FROM table name ORDER by rand () Limit n
Access left connection syntax (recently developed to use left-side connection, Access Help nothing, no access to the Internet SQL instructions, only the test itself, now write down for later)
Syntax elect table1.fd1,table1,fd2,table2.fd2 from table1 left join table2 on TABLE1.FD1,TABLE2.FD1 where ...
Use SQL statements with ... Instead of too long string display
SQL database: Select Case when Len (field) >10 then left (field,10) + ' ... ' else field end as news_name,news_id from TableName www~ Phperz~com
Access database: SELECT iif (>2,left (field,2) + ' ... ', field) from TableName;
This method is used to execute the SQL statement. The format of the method is divided into the following two categories, based on whether the recordset is returned after execution of the SQL statement:
1. When the SQL query statement is executed, the recordset that the query gets is returned. Usage is:
Set object Variable name = Connection object. Execute (SQL Query Language)
After the Execute method call, the Recordset object is created automatically, and the query results are stored in the Record object, and the recordset is assigned to the specified object by the set method, and then the object variable represents the Recordset object.
2. When executing an operational language for SQL, there is no return of recordsets. The usage is:
Connection object. Execute SQL Operational statement [, Recordaffected][, Option]
· Recordaffected is optional, this can place a variable, after the execution of the SQL statement, the number of records in effect is automatically saved to the variable. By accessing the variable, you know how many records have been manipulated by the SQL statement team.
· option, which is usually adCmdText, is used to tell ADO that the first character after the Execute method should be interpreted as the command text. You can make execution more efficient by specifying this parameter.