PHP construct Semantic Web CRUD operations

Source: Internet
Author: User
Tags 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 completed using the Structured Query Language (SQL) in the relational database system. As the Web becomes more data-oriented, it is necessary to move from SQL-based CRUD operations to Semantic Web-based CRUD operations. Learn how to use PHP to perform CRUD operations on a semantic-based Web.

Common acronyms

API-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)

SPARQL-Simple Protocol and RDF Query Language)

Structured Query Language)

UI-User interface)

W3C-World Wide Web Consortium)

When developing Web applications, it is a standard practice to create a database structure for the logic layer and UI Layer to place server code. To connect to the database, the server code needs to perform basic operations such as creating, updating, deleting, and-most importantly-reading records. Because the background databases of Web applications are usually relational databases, these CRUD operations are executed in a well-known SQL language. However, as Web development increasingly uses Object-Oriented Programming (OOP), the model also changes.

Resource Description Framework (RDF) is an ideal method to describe the meaning of objects while retaining data. Simple Protocol and RDF Query Language (SPARQL-pronounced as "sparkle") are commonly used to Query the data, because it matches the structure of the RDF in the statement structure. Both RDF and SPARQL are technologies in the so-called semantic Web stack (semantic Web stack.

To thoroughly apply the Semantic Web concept, you can use SPARQL to apply traditional Web development technologies to RDF data. This article will show you how to use the Simplified Model-View-Controller (MVC) design Model, PHP server scripting language, and SPARQL connect to RDF-opposite to using SQL in relational database systems.

SQL and SPARQL CRUD operations

Prerequisites

This document assumes that you have a basic understanding of SQL, PHP, and Web application development. It is also advantageous to understand semantic Web. To run the create, update, and delete commands on semantic Web-based data, a semantic Web database that supports SPARQL/Update specifications is required.

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

PREFIX foaf:

When comparing the two lists, your first idea is that the SPARQL version is much longer than the SQL version. This is a fact, but do not mistakenly think that SQL must be simpler and cleaner. According to the engine running, SPARQL can be distributed by calling the link data result (linked data effect. In addition, it allows a dynamic mode, because it has an object-oriented perspective that is linked to each other, which is in contrast to a strict perspective of the SQL relationship. If you want to separate relational database tables into multiple data islands, the actually used SQL code will be much more than SPARQL-not to mention that there will be a lot of annoying JOIN descriptors in SQL.

The first two lines of SPARQL are PREFIX statements. According to the semantic Web theory, all content-whether an object or a Data graph source (also an object)-has a Uniform Resource Identifier (URI ). The PREFIX row only applies the temporary label to some Uris-in this example, it is in the Friend of a Friend and RDF modes. The advantage is that you can use the PREFIX declaration in the query in the future without using the complete URI.

The next line of SPARQL Code describes the query request. This statement is essentially the same as an SQL statement, except that it is an additional request to the URI. Note the use of question marks (?) 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, but in SPARQL, the data source name is URI, not a string that represents a physical location in a computer or network.

The WHERE statements of the two are completely different. Because SPARQL is used, you must specify the mode used to obtain data. Similarly, if you have tried to use the relational method to perform this operation, the cost is much higher than that of common SQL: PHP, Java™Check between data sources can be performed only in programming languages or some other server languages. The Operations completed by SPARQL code lines are clear, including ensuring that the data being retrieved only belongs to the Person type. SPARQL will get the name and location, and execute some pattern matching to find the correct John Smith.

Create

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

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 SPARQL-based code in Listing 4.

PREFIX foaf:

Similarly, note that the PREFIX row is exactly the same as the operation performed by sparql in the read operation. Insert into works in a similar way to SQL. However, it is URI-based, not string-based, table-based, or name-based, which allows the operation to be completed using HTTP. You must specify the mode again. Here, it is slightly easier to understand than read operations, because you can have almost any attribute, as long as it is compatible with the mode. This is the advantage of Distributed Dynamic and scalable objects provided by RDF.

Delete

To a certain extent, if you want to create a file, you may need to delete it. For example, users may want to delete their accounts on your site (this is obviously a pity, they want to leave, but they may have a legitimate reason ). Listing 5 provides the SQL code for typical delete operations.

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.