Home  >  Article  >  Backend Development  >  PHP builds semantic Web CRUD operations_PHP tutorial

PHP builds semantic Web CRUD operations_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:25777browse

Create, read, update and delete (Create/Read/Update/Delete, CRUD) operations are the most basic database operations, but they are also the most important operations. CRUD operations are usually completed using Structured Query Language (SQL) in relational database systems. As the Web becomes more data-oriented, there is a need to move from SQL-based CRUD operations to Semantic Web-based CRUD operations. Learn how to perform CRUD operations with the semantic-based web using PHP.

Commonly used abbreviations

API — Application Programming Interface (Application Programming Interface)

CRUD — Create/Read/Update/Delete (Create/Read/Update/Delete)

HTTP — Hypertext Transfer Protocol

MVC — Model-View-Controller (Model-View-Controller)

OOP — Object-Oriented Programming

RDF — Resource Description Framework

SPARQL — Simple Protocol and RDF Query Language (Simple Protocol and RDF Query Language)

 SQL — Structured Query Language (Structured Query Language)

 UI — User interface (User interface)

W3C — World Wide Web Consortium

When developing web applications, it is a standard practice to create a database structure that houses the server-side code for the logic and UI layers. To connect to a database, server-side code needs to perform some basic operations such as creating, updating, deleting, and — most importantly — reading records. Since the backend database of a web application is usually a relational database, these CRUD operations are performed using the well-known SQL language. However, as web development increasingly adopts object-oriented programming (OOP), the model has changed.

Resource Description Framework (RDF) is an ideal way to describe objects while retaining the meaning of the data. Simple Protocol and RDF Query Language (SPARQL—pronounced “sparkle”) is the language typically used to query this data because its statement structure matches the structure of RDF itself. RDF and SPARQL are both technologies in the so-called semantic Web stack.

To fully apply Semantic Web concepts, you can use SPARQL to apply traditional web development techniques to RDF data. This article will show you how to connect to RDF using a simplified Model-View-Controller (MVC) design model, the PHP server-side scripting language, and SPARQL—as opposed to using SQL in a relational database system.

 SQL and SPARQL CRUD operations

Prerequisites

 This article assumes you have a basic understanding of SQL, PHP, and web application development. Understanding the Semantic Web is also beneficial. To run create, update, and delete commands on Semantic Web-based data, you need a Semantic Web database that supports the SPARQL/Update specification.

When developing with SQL and SPARQL, you need to look at the similarities and differences between CRUD operations. Listing 1 shows the SQL code for the read operation.

SELECT realname, dob, locationFROM UserTable WHERE realname = "John Smith";

Compare this SQL-based code to the SPARQL-based code shown in Listing 2. The reason for using these two read operations is that they are the easiest to understand, implement, and describe. This is the same for both SQL and SPARQL.

PREFIX foaf:

Your first thought when comparing the two lists is probably that the SPARQL version is significantly longer than the SQL version. This is true, but don't be fooled into thinking that SQL is necessarily simpler and cleaner. Depending on the engine it's running on, SPARQL can all be delivered through what's called a linked data effect. Furthermore, it allows having dynamic schemas because it has an object-oriented perspective that is linked to each other, in contrast to the strict SQL relational perspective. If you wanted to separate a relational database table into many data silos, you would actually use many more lines of SQL code than SPARQL—not to mention a lot of annoying JOIN descriptors in SQL.

The first two lines of SPARQL are PREFIX statements. According to Semantic Web theory, all content—whether it is an object or a data graph source (also an object)—has a Uniform Resource Identifier (URI). The PREFIX line simply applies a temporary tag to some URI—in this case, Friend of a Friend and the RDF schema. The benefit is that you can later use the PREFIX statement in queries without having to use the full URI.

The next line of SPARQL code describes the query request. This statement is essentially the same as the SQL statement, except for the additional request for the URI. Note the use of the question mark (?) to indicate that the term is a variable.

 The FROM statement describes the location where data is obtained. This is the same in SQL and SPARQL, except that in SPARQL the data source name is a URI rather than a string representing a physical location on a computer or network.

The WHERE statements of the two are completely different, because with SPARQL, you must specify the schema used to obtain the data. Again, if you've tried to do this using the relational approach, it's much more expensive than plain SQL: you need to use PHP, the Java™ programming language, or some other server-side language to perform the check between data sources. What the SPARQL line of code accomplishes is relatively self-explanatory, including ensuring that the data being retrieved is only of type Person. SPARQL will get the name and location and perform some pattern matching to find the correct John Smith.

Create

CRUD operations in SPARQL are often more mysterious than read operations. However, these operations can be completed. First, the create operation inserts a new record or object into the table or chart.

 INSERT INTO UserTable (realname, dob, location) VALUES ("John Smith", "1985-01-01", "Bristol, UK");

Now, compare the create operation in the SQL-based code in Listing 3 with the SPARQL-based code in Listing 4.

PREFIX foaf:

Again, note that the PREFIX line is exactly what you would do in a read operation with SPARQL. INSERT INTO works like SQL, but again, this is URI-based rather than string-based, table-based, and name-based, which allows the operation to be completed using HTTP. You must specify the mode again. Here it's slightly easier to understand than in the read operation, since you can have almost any property as long as it's compatible with the schema. This is the beauty of distributed, dynamically extensible objects provided by RDF.

Delete

To a certain extent, if you want to create it, it will probably need to be deleted. For example, a user may want to delete their account on your site (which is obviously a shame and they want to leave, but they may have a valid reason). Listing 5 provides the SQL code for a typical delete operation.

 DELETE FROM UserTable WHERE realname = "John Smith"

Now, compare the SQL-based code in Listing 5 with the SPARQL-based code in Listing 6.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752064.htmlTechArticleCreate, read, update and delete (Create/Read/Update/Delete, CRUD) operations are the most basic database operations, but they are also the most important operations. CRUD operations are usually performed using relational databases...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn