{"id":83,"date":"2014-02-03T16:53:46","date_gmt":"2014-02-03T23:53:46","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=83"},"modified":"2016-10-19T15:36:54","modified_gmt":"2016-10-19T22:36:54","slug":"sql","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/databases\/sql\/","title":{"rendered":"SQL Basics"},"content":{"rendered":"<p>The <span style=\"color: #0000ff;\">Structured Query Language<\/span>, SQL, is a standard language to manipulate databases. There are four basic operations of databases: Create, Read, Update and Delete (CRUD). The required SQL statement for each operation depends on the data object(s) handled. Here is a summary of the most common ones:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>Operation<\/td>\n<td>SQL Statement<\/td>\n<td>Description<\/td>\n<\/tr>\n<tr>\n<td>Create<\/td>\n<td>CREATE TABLE\u2026<\/td>\n<td>Creates a new table, defining field names and data types<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>SELECT\u2026 INTO\u2026<\/td>\n<td>Creates a new table from a query result<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>INSERT INTO\u2026 VALUES\u2026<\/td>\n<td>Creates tuples in a table<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>INSERT INTO\u2026 SELECT\u2026<\/td>\n<td>Creates tuples in a table obtained from a query<\/td>\n<\/tr>\n<tr>\n<td>Read<\/td>\n<td>SELECT\u2026 FROM\u2026 WHERE\u2026<\/td>\n<td>Selects columns from table where condition is met<\/td>\n<\/tr>\n<tr>\n<td>Update<\/td>\n<td>UPDATE\u2026 SET\u2026 WHERE\u2026<\/td>\n<td>Updates field values of tuples satisfying a condition<\/td>\n<\/tr>\n<tr>\n<td>Delete<\/td>\n<td>DELETE\u2026 FROM\u2026 WHERE<\/td>\n<td>Deletes tuples satisfying a condition<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>DROP TABLE\u2026<\/td>\n<td>Deletes table<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>TRUNCATE TABLE\u2026<\/td>\n<td>Deletes all data in the table<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This <a href=\"http:\/\/en.wikipedia.org\/wiki\/SQL\" target=\"_blank\">Wikipedia article<\/a> in provides more details about SQL.<\/p>\n<p><strong>Formatting in SQL<\/strong><\/p>\n<p>Before continuing with statements, here are some SQL formatting rules:<\/p>\n<p>1. SQL is NOT case sensitive: <em>SELECT<\/em> is the same as <em>select<\/em>.<\/p>\n<p>Note: for this reason many people use underscore to name columns instead of camel case. For instance Student_ID instead of StudentID.<\/p>\n<p>2. SQL ignores white space and newline characters. This gives you flexibility about formatting query statements: you can put SELECT, FROM, WHERE clauses on separate lines and indent each one, or whatever makes your code more human-readable, without affecting its machine-readability.<\/p>\n<p>3. Semicolons after SQL statements are the standard way to separate multiple statements. Some DBMS don&#8217;t require them for single statements.<\/p>\n<p><strong>Creating Tables<\/strong><\/p>\n<p>In Access, when using the create table command, you need to define each field&#8217;s data type, and you should also specify the primary key. More details are <a href=\"https:\/\/support.office.com\/en-us\/article\/Create-a-table-8fdc65f9-8d40-4ff5-9212-80e6545e8d87\">here,<\/a> including how to use the menu options to create a table. You can also create a query and type the following in &#8220;SQL View&#8221;:<\/p>\n<p><span style=\"color: #0000ff;\">CREATE TABLE<\/span> Students\u000b (StudentID number, StudentName text, Program text, Year number, Age number, primary key (StudentID))<\/p>\n<p><strong>Data Types in SQL:<\/strong> note that these may be slightly different in different implementations of SQL: <a href=\"http:\/\/www.w3schools.com\/sql\/sql_datatypes.asp\">here <\/a>is a list of the types in different versions, and <a href=\"https:\/\/support.office.com\/en-us\/article\/Data-types-for-Access-desktop-databases-df2b83ba-cef6-436d-b679-3418f622e482\">here<\/a> is a list of data types in Access specifically. For example, the type &#8220;text&#8221; in Access is a character string (may also contain numbers) that has a maximum of 255 characters, while the type &#8220;text&#8221; in MySQL has a maximum length of 65,535\u00a0characters, and in SQL Server the type &#8220;text&#8221; can contain up to 2 GB of data.<\/p>\n<p><span style=\"color: #0000ff;\">NULL<\/span> values are allowed in all the types. Null represents an unknown value or that a value does not exist. To prohibit null values in a column you need to add the statement NOT NULL in the field declaration (e.g. StudentID Number NOT NULL).<\/p>\n<p><strong>Reading: <\/strong>A typical SQL query has the form:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">SELECT<\/span> Columns<br \/>\n<span style=\"color: #0000ff;\">FROM<\/span> Tables<br \/>\n<span style=\"color: #0000ff;\">WHERE <\/span>Conditions<\/p>\n<p>The result of a SQL query is a new table called the <span style=\"color: #0000ff;\">result set<\/span>.<\/p>\n<p><span style=\"color: #0000ff;\">SELECT INTO<\/span>: creates a new table from a query result set. The fields will have the same data type of the original fields.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">SELECT<\/span> StudentID, StudentName <span style=\"color: #0000ff;\">INTO<\/span> MM13<br \/>\nFROM Students<br \/>\nWHERE Program = \u201cMMOR\u201d AND Year = 2013 ;<\/p>\n<p><strong>Creating Tuples<\/strong><\/p>\n<p>You can insert a tuple into a table setting the value of each field:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">INSERT INTO<\/span> Students \u000b<span style=\"color: #0000ff;\">VALUES<\/span> (536880, &#8220;Harry Potter&#8221;, &#8220;MMOR&#8221;, 2017, 23)<\/p>\n<p>Or you can create a tuple but\u00a0define only a subset of fields:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">INSERT INTO<\/span> Students (<span style=\"color: #800080;\">StudentID, StudentName<\/span>) <span style=\"color: #0000ff;\">VALUES<\/span> (536881, &#8220;Ron Weasley&#8221;)<\/p>\n<p>Alternatively, you can insert the rows from another table:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">INSERT INTO<\/span> Students <span style=\"color: #0000ff;\">SELECT<\/span> * FROM Admitted_Students<\/p>\n<p><strong>Updating<\/strong><\/p>\n<p>You can update field values of tuples satisfying a condition. E.g. increase the age of all students by 1:<\/p>\n<p style=\"padding-left: 30px;\">\u000b <span style=\"color: #0000ff;\">UPDATE<\/span> Students\u000b <span style=\"color: #0000ff;\">SET<\/span> Age = Age + 1\u000b <span style=\"color: #800080;\">WHERE<\/span> Age &gt; 10<\/p>\n<p><strong>Deleting<\/strong><\/p>\n<p>Deleting tuples satisfying a condition:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">DELETE<\/span> <span style=\"color: #800080;\">FROM<\/span> Students\u000b <span style=\"color: #800080;\">WHERE<\/span> name = &#8220;Harry Potter&#8221;<\/p>\n<p>Deleting a table:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">DROP TABLE<\/span> MM13 ;<\/p>\n<p>Deleting all data inside a table but preserving the table structure:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">TRUNCATE TABLE<\/span> MM13 ;<\/p>\n<p>You can find an extensive list of SQL commands and examples of how to use them <a href=\"http:\/\/www.w3schools.com\/sql\">here<\/a>. The <a href=\"https:\/\/support.office.com\/en-us\/access\">help page <\/a>for Access is also very informative.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Structured Query Language, SQL, is a standard language to manipulate databases. There are four basic operations of databases: Create, Read, Update and Delete (CRUD). The required SQL statement for each operation depends on the data object(s) handled. Here is a summary of the most common ones: Operation SQL Statement Description Create CREATE TABLE\u2026 Creates [&hellip;]<\/p>\n","protected":false},"author":22982,"featured_media":0,"parent":74,"menu_order":2,"comment_status":"open","ping_status":"closed","template":"page-templates\/full-width.php","meta":{"footnotes":""},"class_list":["post-83","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/22982"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":27,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/83\/revisions"}],"predecessor-version":[{"id":1533,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/83\/revisions\/1533"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/74"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}