PHP constructs semantic Web CRUD operations

Source: Internet
Author: User
Tags php server web database

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 done using the Structured Query Language (structured query language,sql) in the relational database system. As the Web becomes more data-oriented, it needs to move from sql-based crud operations to a semantic Web based crud operation. Learn how to use PHP to perform CRUD operations through a semantically based Web.

Common abbreviations

api-application Programming Interface (application programming Interface)

crud-Create/read/update/delete (Create/read/update/delete)

http-Hypertext Transfer Protocol (hypertext Transfer Protocol)

mvc-mode-View-controller (Model-view-controller)

oop-object-oriented Programming (object-oriented programming)

rdf-Resource Description Framework (Resource Description Framework)

sparql-simple protocol and RDF query language (easy Protocol and RDF queries Language)

sql-Structured Query Language (structured query Language)

ui-UI (User interface)

w3c-Web Consortium (World Wide website Consortium)

When developing WEB applications, it is a standard practice to create a database structure that places server-side code for the logical and UI tiers. To connect to a database, the server-side code needs to perform some basic creation, update, delete, and-most important-read records, and so on. Because the WEB application's background database is usually a relational database, these CRUD operations are performed using a well-known SQL language. However, as WEB development has increasingly adopted object-oriented programming (OOP), the model has changed.

The Resource Description Framework (Resource Description FRAMEWORK,RDF) is an ideal way to describe an object while preserving the meaning of the data. The simple protocol and the RDF Query language, which is pronounced as "sparkle", is a language commonly used to query against the data, because it matches the structure of the RDF itself in the form of statements Protocol. Both RDF and SPARQL are technologies in the so-called Semantic Web stack (Semantic Web stack).

To thoroughly apply semantic Web concepts, you can use SPARQL to apply traditional Web development technologies to RDF data. This article will show you how to use the simplified schema-view-controller (MODEL-VIEW-CONTROLLER,MVC) design model, PHP server-side scripting language, and SPARQL to connect to rdf-in contrast to using SQL in relational database systems.

SQL and SPARQL CRUD operations

Prerequisite

This article assumes that you have a basic understanding of SQL, PHP, and WEB application development. Understanding the semantic Web is also very beneficial. To run the Create, update, and DELETE commands on semantic web based data, you need a semantic Web database that supports the Sparql/update specification.

When using SQL and SPARQL for development, 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 with the SPARQL based code shown in Listing 2. The reason for using these two read operations is that they are easiest to understand, implement, and explain. This is the same for both SQL and SPARQL.

PREFIX FOAF:

When comparing two lists, your first idea is likely to be that the SPARQL version is significantly longer than the SQL version. This is true, but do not mistakenly assume that SQL is inherently simpler and cleaner. Depending on the engine being run, SPARQL can be distributed all through content called linked data results (linked). In addition, it allows for dynamic mode because it has an object-oriented perspective that is linked to each other, and contrasts with a rigorous SQL relational perspective. If you want to separate relational database tables into islands of many data, you actually use a lot more SQL code than SPARQL-not to mention that there are a lot of nasty JOIN descriptors in SQL.

The first two lines of SPARQL are PREFIX declarations. According to the semantic Web theory, everything-whether object or data graph source (also an object)-has a Uniform Resource identifier (uniform Resource Identifier,uri). The PREFIX line simply applies the temporary tag to some URIs-in this case, friend of a friend and RDF schema. The benefit is that you can use the PREFIX declaration in a query later without having to use the full URI.

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

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

The WHERE statement of the two is completely different, because using SPARQL, you must specify the pattern used to get the data. Similarly, if you try to do this with a relational method, you will need to spend much more than normal SQL: you need to use PHP, the Java™ programming language, or some other server-side language to perform checks between data sources. The SPARQL code line completes the operation, which includes ensuring that the data being retrieved belongs only to the person type. SPARQL will get the name and location, while performing 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 done. First, the Create operation inserts a new record or object into the table or diagram.

INSERT into Usertable (Realname, DOB, location) VALUES ("John Smith", "1985-01-01", "Bristol, UK");

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

PREFIX FOAF:

Also, note that the PREFIX line is exactly the same as the operation performed in the read operation with SPARQL. INSERT into works like SQL, but again, it is based on URIs rather than strings, based on tables, and based on names, which allows operations to be done using HTTP. You must specify the pattern again. Here, it's a little easier to understand than in the read operation, because you can have almost any property, as long as it is compatible with the schema. This is the advantage of the distributed, dynamically extensible object that RDF provides.

Delete

To a certain extent, if you want to create it, you probably need to delete it. For example, users may want to delete their accounts in your site (it is obviously a pity that 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.

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.