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 performed 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 use PHP to perform CRUD operations with the semantic-based web. Commonly used abbreviations
API - Application Programming Interface
CRUD - Create/Read/Update/Delete
HTTP - Hypertext Transfer Protocol
MVC - Pattern - View - Control Model-View-Controller
OOP - Object-Oriented Programming
RDF - Resource Description Framework
SPARQL - Simple Protocol and RDF Query Language
SQL - Structured Structured Query Language
UI - User interface
W3C - World Wide Web Consortium
When developing web applications, it is a standard to create a database structure that houses the server-side code for the logic layer and the UI layer. practice. 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 Web applications 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 preserving 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 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 with 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 illustrate. This is the same for both SQL and SPARQL.
PREFIX foaf:< http://xmlns.com/foaf/0.1/ > PREFIX rdf: < http://www.w3.org/1999/02/22-rdf-syntax-ns#>SELECT ?uri ?name ? dob ?locationFROM< http://www.example.org/graph>WHERE { ?urirdf:type foaf:Person ;foaf:name "John Smith" ;foaf:birthday?dob ;foaf:location ?location .} ;
in comparison When looking at the two lists, your first thought 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 the 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 where to get the data. 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 checks between data sources. What the SPARQL line of code accomplishes is relatively straightforward, 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.
Creating
CRUD operations in SPARQL is generally more arcane 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 SQL-based code in Listing 3 with the SPARQL-based code in Listing 4 create operation in .
PREFIX foaf:< http://xmlns.com/foaf/0.1/ > PREFIX rdf: < http://www.w3.org/1999/02/22-rdf-syntax-ns#>INSERT INTO GRAPH < http: //www.example.com/graph > (?realname, ?dob, ?location) {< http://www.example.org/graph/johnsmith#me > rdf:Type foaf:Person ; foaf:name "John Smith" ; foaf:birthday <1985-01-01T00:00:00> ; foaf:location "Bristol, UK" }