1.JPQL Query
JPQL Full name Java persistence Query Languagebased on the EJB query language (EJB QL), introduced in EJB2.0 for the first time, the Java Persistence Query Language (JPQL) is a portable query language designed to bind SQL syntax and simple query semantics together in an expression of an object-oriented expression language · Queries written in this language are portable and can be compiled into SQL on all major database servers. its characteristics are similar to native SQL statements and are fully object-oriented, accessed through class names and properties, rather than table names and table properties. the query syntax provided by JPQL is divided into three main categories:
- SELECT syntax for queries
- Update syntax for updates
- Delete syntax for deletion
The SELECT syntax structure consists of several parts:SELECT clause from sentence [WHERE clause] [GROUP by clause] [HAVING clause] [ORDER by clause ]
A basic SELECT statement is as follows:SELECT u.id, u.name from User u WHERE u.age > U.age <
where User U is a path representation (path expression), there are three types of paths: the range variable path representation, the cluster member (Collection member) path representation, and the Associated Navigation (association traversing) said. User U is an example of a range variable path representation that specifies that the entity for the query is user and alias U.
a cluster member path represents a cluster member that is used to specify objects, such as: SELECT u from User u, in (u.emails) e WHERE e.address like '%.% @openhome. CC '
where in is specified, is the cluster member path representation, and >, <, and, in, like, and so are the conditional expressions in the WHERE clause, a simple list of some of the conditional expressions are as follows:
| Comparison statement |
=, >, >=, <, <=, <> |
| Between statement |
[Not between |
| Like statement |
[NOT] Like |
| In statement |
[NOT] Inch |
| NULL statement |
is [NOT] NULL |
| EMPTY statement |
is [NOT] EMPTY |
| EXISTS statement |
[NOT] EXISTS |
Like, you can use _ to represent a single character, and% to represent any number of characters.
The associated navigation representation provides the functionality of join in SQL syntax, including inner join, left OUTER join, fetch, and so on, with the following practical examples of inner joins: SELECT u from the User u INNER joinu.emails e WHERE e.address like '%.% @openhome. CC '
The join keyword can be omitted, and the equation above is equivalent to:SELECT u from User u joinu.emails e WHERE e.address like '%.% @openhome. CC '
the OUTER keyword of the left OUTER join can be omitted, an example is as follows:SELECT u from User u left joins u.emails e WHERE e.address like '%.% @openhome. CC '
in the inner join, the left OUTER join can be added with the FETCH keyword to pre-retrieve the relevant data, for example:SELECT u from User u left JOIN FETCH u.emails e WHERE e.address like '%.% @openhome. CC '
you can use aggregate functions in Select, for example:SELECT AVG (u.age) from User u
in Select, you can use the construct representation to encapsulate some data directly into the specified object, for example:SELECT NEW someobject (u.id, U.name, o.number) from User u JOIN Order o WHERE u.id = 1975
A jpql function such as length (), LOWER (), UPPER (), SUBSTRING () can be used in the WHERE clause.
You can use order by to sort the results of a query: SELECT u from the User u ORDER by U.age
The order preset is an ASC-power sort, which can be sorted using the desc descending power:SELECT u from the User u ORDER by u.age DESC
you can specify more than two sorting methods at the same time, for example, sort by the "age" power, or, if the "age" is the same, by "name" Power:SELECT u from the User u ORDER by U.age DESC, U.name
You can use the GROUP BY clause to automatically group the specified fields according to the same content group, for example, by field "sex" and averaging:SELECT U.sex, AVG (u.age) from the User u GROUP by U.sex
GROUP by is usually paired with having to use, for example:SELECT u.sex, avg (u.age) from the User u GROUP by U.sex have avg (u.age) >
You can use the update syntax to update the data, for example:UPDATE User u SET u.name= ' momor ' WHERE u.name= ' BBB '
You can delete data through delete, for example:DELETE User u WHERE u.name= ' Bush 'Select queryEditSELECT <select_expression>From <from_clause>[WHERE <conditional_expression>][ORDER by <order_by_clause>]Aggregate QueriesEditSELECT <select_expression>From <from_clause>[WHERE <conditional_expression>][GROUP by <group_by_clause>][having <conditional_expression>][ORDER by <order_by_clause>]Update QueryEditUPDATE <entity name>[[as] <identification Variable>]SET <update_statement>{,<update_statement>}*[WHERE <conditional_expression>]Delete QueryEditDELETE from <entity name>[[as] <identification Variable>][WHERE <conditional_expression>]2.SQL Language Overview
Structured Query Language (structured Query Language, abbreviated to SQL) is a language between relational algebra and relational calculus, and is a standard computer language used to communicate with relational database management systems. Its functions include 4 aspects of data query, data manipulation, data definition and data control, and it is a general and powerful relational database language. It is now a standard language for relational databases.
1 SQL Basic Features
SQL language Set data query, data manipulation, data definition and data control, all embody the characteristics and advantages of the relational data language in a full-featured.
1. Data definition function
Implemented through the DDL (Data Definition Language) language. Can be used to support the definition or establishment of database objects (such as tables, indexes, sequences, views, etc.), define the relational database mode, outer mode, internal mode. Common DDL statements are different forms of the Create, ALTER, and drop commands.
2. Data manipulation function
The data manipulation function is implemented by DML (data manipulation Language) language, and DML includes two kinds of statements: querying, statistics, sorting, grouping and retrieving data in the database. Data update means updating, deleting, Modifications, and so on.
3. Data Control function
The data control function of the database refers to the security and integrity of the data. Implemented by the data control Language DCL.
2. Features of SQL language
1. Simple language, easy to learn and use
2. High degree of non-process
Users only have to ask "what to do" to get the expected results, as for "How to" is done by the RDBMS, and its processing of the user hidden
3.SQL languages can be used interactively or in embedded form
The former is mainly used for database users such as database managers, allowing users to issue SQL commands directly to the DBMS, subject to running results, or primarily embedded (c, C + +) host languages, used by programmers to develop database applications. In two different ways, the syntax structure of SQL language is basically the same. This unified grammatical structure provides two different ways of use, providing users with great flexibility and convenience.
4. Set-oriented operation mode
The SQL language uses a collection operation, not only to find the results can be a collection of tuples, but also an INSERT, delete, update operation of the object can be a set of tuples.
5.SQL Language Support relational database three-level schema structure
Database level Three mode refers to: The internal mode corresponds to the storage file, the pattern corresponds to the base table, the outer mode corresponds to the view. A base table is a table that itself exists independently, and a view is a table that is exported from a base table or other view, which itself is not stored independently in the database, that is, the database stores only the definition of the view without storing the data for the view, which is still stored in the base table of the exported view, so the view is a virtual table. Users can query the view and base tables in SQL language. In the eyes of the user, both the view and the base table are relationships, and the stored files are transparent to the user.
6. Integrated and unified
①sql language Set data definition language DDL, Data manipulation language DML, Data Control Language DCL function in one, language style unified, can independently complete the database life cycle of all activities, including the definition of relationship patterns, input data to establish database, query, update, maintenance, database reconstruction, Database security control, such as a series of operational requirements, which provides a good environment for database application system development, such as users in the database put into operation, but also according to the need to modify the pattern at any time, does not affect the operation of the database, so that the system has a good extensibility.
② the relationships between entities and entities in the relational model are expressed in relation to each other, and the uniqueness of this data structure leads to the unification of operators, that is, every operation (such as finding, inserting, deleting, modifying) of the relation between entities and entities requires only one operator . 3.HQLHQL is the acronym for Hibernate Query Language, providing richer, more flexible, and more powerful query capabilities, HQL closer to the SQL statement query syntax. The criteria query encapsulates the query criteria in an object-oriented manner, but HQL (Hibernate query Language) queries provide richer and more flexible query features. As a result, Hibernate makes the HQL query the official recommended standard Query method, HQL query to cover all the functions of the criteria query, provides a similar standard SQL statement query method, but also provides a more object-oriented encapsulation. The complete HQL statement form is as follows: Select/update/delete ... from ... to ... .... ... ..... ............ asc/desc the update/delete of which is the hibernat of the .... The newly added features in E3, visible hql queries are very similar to standard SQL queries. HQL queries are at the core of the entire Hibernate entity operating system. 4. Conditional Query
And and or can combine two or more conditions in a where sub-statement.
When using the OR keyword:
- As long as one of these criteria is met, such a record is queried.
- If any of these query conditions are not met, such records will be excluded.
When using the AND keyword:
- All the conditions are met and the records are queried.
- If any one of these conditions is not met, such records will be excluded.
MySQL and and or instances
The table data you need to use in this example is as follows:
| title |
content |
category |
Seo_name |
| PHP arrays |
PHP Array Usage Analysis |
1 |
Php |
| MySQL DISTINCT |
MySQL distinct instances |
2 |
Mysql |
| Java array |
How to use Java array |
3 |
Java |
| PHP input |
How PHP input is given a value |
4 |
Php |
(1) An and conditional query operator instance:
Use and to display all data with the title "PHP Array" and Category 1:
SELECT * FROM ar WHERE title=‘php数组‘ AND category=‘1‘
Results:
| title |
content |
category |
Seo_name |
| PHP arrays |
PHP Array Usage Analysis |
1 |
Php |
(2) or conditional operator instance
Use or to display all data with the title "Java array" or Seo_name "PHP":
SELECT * FROM ar WHERE title=‘java array‘ OR seo_name=‘php‘
Results:
| title |
content |
category |
seo_name |
| php array |
php Array usage Analysis |
1 |
php |
| java array |
java Array use method |
3 |
java |
| php input |
p How HP input receives the value |
4 |
php |
(3) combining and and OR operators
We can also combine and and or together (using parentheses to form complex expressions):
SELECT * FROM ar WHERE (title=‘java array‘ OR category=‘4‘)AND seo_name=‘php‘
Results:
| title |
content |
category |
Seo_name |
| PHP input |
How PHP input is given a value |
4 |
Php |
and and or precedence
You can include any number of and and or operators in where, and when there are no other symbols, such as parentheses, SQL executes the and condition first, and then executes the or statement, such as:
select * from table from id=1 or id=2 and price>=10;/* http://www.manongjc.com/article/1439.html */
This statement defaults to id=2 and the price is greater than or equal to 10, or id=1.
If you add parentheses:
select * from table from (id=1 or id=2) and price>=10;
The statement executes id=1 or id=2, and the price is greater than or equal to 10.
Java EE Week 11th