krypted.com

Tiny Deathstars of Foulness

OS X might be the easiest platform to install MySQL on. To do so, simply download the MySQL installation package from the MySQL Download site. I like to use the third link (the DMG). Screen Shot 2016-02-13 at 10.26.11 PM Once downloaded, run the package. The package will ask you a few questions and you can easily just select the default choice during the installation process. Screen Shot 2016-02-13 at 10.26.04 PM Once installed, you’ll be prompted that a temporary password has been used for your MySQL instance. Screen Shot 2016-02-13 at 10.26.52 PM The password will get you in the first time, so you can change it. Once you have documented the password, open System Preferences and click on MySQL in the bottom row of System Preference Panes. Screen Shot 2016-02-13 at 10.27.34 PM Click Start MySQL Server and then when prompted, authenticate to the system. If you’d like to do this programmatically and don’t need the System Preference pane, you can do so with homebrew. If you have homebrew installed, simply run the brew command with the install verb and mysql as the package: brew install mysql Whichever way you install SQL, once installed, you’ll want to set the root password to something other than the intuitionally difficult to remember password provided at install time. To do so, first connect to the mysql instance now running on your computer. As the tools are installed in /usr/local/mysql/bin, run the following: /usr/local/mysql/bin/mysql -u root Then, set the password using the ALTER statement along with the USER option and then the username followed by IDENTIFIED BY and ultimately the password, as follows: ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysupersecretpassword'; Once done, you’ll then be able to connect to mysql normally.

February 18th, 2016

Posted In: SQL

Tags: , , , ,

You can use the SELECT INTO statement finds data and then copies that data between tables or databases. To do so, use the following syntax: SELECT * INTO newtablename FROM tablename; So to copy that Customers table to a new table called Customers2: SELECT * INTO Customers2 FROM Customers; Or to copy only certain columns into Customers2, we’d use the following: SELECT ID,Site INTO Customers2 FROM Customers;

February 16th, 2016

Posted In: SQL

Tags: , , , , ,

So you’re ready to write some software? Or test some cool stuff. Or build something awesome. You can use the CREATE DATABASE statement to get started, by creating a database. To do so is pretty easy, simply run that statement followed by a name for the database (called Customers): CREATE DATABASE Customers; Once you’ve created a database, it’s time to create tables, which can be done using the CREATE TABLE statement. The Syntax of that statement looks something like this, defining a set of columns, their data type and the size of the column (in the form of a maximum length), all wrapped in parenthesis with each column separated by a comma: CREATE TABLE nameoftable ( column datatype(size), column datatype(size), ); So to create the Customers table that we’ve been using through these articles, we’ll use the following SQL Statement: CREATE TABLE Customers ( ID integer(255), Site varchar(255), Contact varchar(255), Address varchar(255), City varchar(255), Zip integer(255), Country varchar(255), ); Columns are created with data types. In the previous example, we named columns of integers and varchars. The available data types include the following:
  • CHARACTER: A string of characters using a defined length
  • VARCHAR: A variable length string of characters using a maximum length
  • BINARY: A binary string
  • BOOLEAN: TRUE or FALSE
  • VARBINARY: Binary string in a variable length, with a defined maximum length
  • INTEGER: A number with no decimals
  • SMALLINT: A 5 digit or less number with no decimals
  • BIGINT: A 19 digit or less number with no decimals
  • DECIMAL: Number with decimals
  • FLOAT: Floating number in base 10
  • REAL: Number
  • DOUBLEPRECISION: Approximate number
  • DATE: Year, month, and day in separated values
  • TIME: Hour, minute, and second in separated values
  • TIMESTAMP: Time in the form of year, month, day, hour, minute, and second
  • INTERVAL: A period of time
  • ARRAY: Ordered collection of data
  • MULTISITE: Unordered collection of data
  • XML: Stores XML

February 15th, 2016

Posted In: SQL

Tags: , , , , , , , , , , , , , , ,

Sometimes you have data in a MySQL database that you just don’t need. You can delete tables and records pretty easily. In fact, it’s almost too easy. And there’s no undo. So be careful. And backup. And then backup again. And then snapshot again, before tinkerating with anything in this article. In this article we’ll look at using the SQL DELETE statement to delete rows in a table. To do so, we’ll follow this basic syntax, which includes a WHERE clause to narrow the scope of the DELETE by specifying which records will be removed: DELETE FROM table WHERE column=value; In this article, we’ll use the same “Customers” table from our first articles: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US Let’s not run this next command, but note that you can omit the WHERE statement to remove all data from a database. To do so, you would simply delete all rows in a table without deleting the table itself. I do this a lot with test databases. It leaves the schema/table structure, attributes, and indexes will be intact: When I’m doing this, I usually specify the wildcard (*): DELETE * FROM table; In the following, we’re going to go ahead and remove the Microsoft record. To do so, we’ll run the DELETE and specify the table to delete data from. Then, we’ll use the WHERE to specify that we want to remove the record where the Site is Microsoft AND the Contact is Satya Nadella: DELETE FROM Customers WHERE Site='Microsoft' AND Contact='Satya Nadella'; The “Customers” table then looks like this: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US

February 6th, 2016

Posted In: Mac OS X, SQL

Tags: , , , , ,

Previously we looked at finding data in a SQL database and The UPDATE statement is used to update records in a table. You can also use the UPDATE statement to update existing records in a table. When using the SQL UPDATE statement, we’ll also use the WHERE clause, as we used previously to constrain output of a SELECT statement. The WHERE locates the record(s) to be updated with syntax as follows: UPDATE table SET column=value,column=value,... WHERE column=value; The WHERE clause indicates the record(s) to update. I’ve forgotten to put it in in the past and updated every record of a database. That’s bad (unless you mean to do it). So let’s run a SELECT on our sample database from earlier, so we can see the “Customers” table from our first article: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US Now, let’s update the Address of the Apple record. To update the Address “spaceship” with the contents of “1 Infinite Loop” we’ll UPDATE the Customers table and SET the Address to equal UPDATE Customers SET Address='1 Infinite Loop' WHERE Site='Apple'; The selection from the “Customers” table will now look like this: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook 1 Infinite Loop Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US You can also update multiple records by separating each item with a comma. For example, let’s say we wanted to update the Microsoft record to get the address correct: UPDATE Customers SET Address='One Microsoft Way',Zip="98052-7329" WHERE Site='Microsoft'; Note that in the above example, I quoted Zip as I didn’t use an actual integer. The selection from the “Customers” table will now look like this: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook 1 Infinite Loop Cupertino 95014 US 3 Microsoft Satya Nadella One Microsoft Way Redmond 98052-7329 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US

February 4th, 2016

Posted In: SQL

Tags: , , , , ,

Sometimes you need to write a record into a table in a SQL database. The INSERT INTO statement creates new records in a table and can work in one of two ways. The first form does not specify the column names where the data will be inserted, only their values. When doing so, each value needs to be inserted in the columned order they appear, here the table being the name of the table you’re adding a record into and each value would be replaced with the contents of your value (don’t insert the string ‘value’ into each!): INSERT INTO table VALUES (value,value,value,...); If you don’t have every value to insert, you can also list the columns to insert data in and then include the values in the same order that the columns are listed in. The second form specifies both the column names and the values to insert: INSERT INTO table (column,column,column,...) VALUES (value,value,value,...); Below is a selection from the “Customers” table that I put in the first article. We will use it to run some SQL statements using the INSERT keyword so that we can add data to our little database: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US To insert a new row in the “Customers” table, we’ll add a row for Huffington Post with Arianna Huffington as the owner, her address as “The Library, with a city of Los Angeles, a zip of 90077, and a country of the US. INSERT INTO Customers (Site, Contact, Address, City, Zip, Country) VALUES ('Huffington Post','Arianna Huffington','The Library','Los Angeles','90077','US'); The selection from the “Customers” table will now look like this, after the ID record incremented on its own, taking the next available integer: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US 6 Huffington Post Arianna Huffington The Library Los Angeles 90077 US As mentioned, you can also Insert Data Only in Specified Columns It is also possible to only insert data in specific columns. The following SQL statement will insert a new row, but only insert data in the Site, Contact, and Country columns: INSERT INTO Customers (Site, Contact, Country) VALUES ('Spotify', 'Daniel Elk', 'SE'); The selection from the “Customers” table will now look like this: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US 6 Huffington Post Arianna Huffington The Library Los Angeles 90077 US 7 Spotify Daniel Elk SE Overall, adding rows to SQL tables is really straight forward. I mean, you’re not replacing anything… Yet…

February 3rd, 2016

Posted In: SQL

Tags: , , , , , , , ,

Previously, we looked at the SQL SELECT and WHERE options to obtain output and then constrain what would be displayed in a query. The AND & OR operators are used to add additional logic so you can filter records based on more than one condition. Which is to say to search based on the contents of multiple columns within a single table. AND is used to show information if two different conditions are true and OR is used to show information if either condition is true. Below is a selection from the “Customers” table that showed in our first article an we will use it to run some SQL sorting statements using the ORDER BY keyword: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US The following example SQL statement outputs customers from the country “US” AND the city “55418”, in the “Customers” table from above: SELECT * FROM Customers WHERE Country='US' AND Zip='55418'; The following SQL statement selects all customers from the city “Minneapolis” OR the city “Cupertino”, in the “Customers” table: SELECT * FROM Customers WHERE City='Cupertino' OR City='Minneapolis'; As the logic of your searches expands, you can combine AND and OR by nesting logic within parenthesis (who said pre-algebra would be useless?!?!). For example, the following SQL statement selects all customers from the country “US” AND the city must be equal to “Minneapolis” OR “Cupertino”, in the “Customers” table: SELECT * FROM Customers WHERE Country='US' AND (City='Minneapolis' OR City='Cupertino');

February 2nd, 2016

Posted In: SQL

Tags: , , , , , , , ,

Previously, we covered the SQL SELECT Statement, to pull data from a SQL database. Here, we’ll constrain our search for items that match a given string, or pattern using the WHERE clause to filter search results, rather than getting all of the records and parsing the output. The WHERE clause extracts records that fulfill a specified string and follows the general syntax as follows, replacing the word column with the name of the column in one of your tables and the word table with the name of a table that you’d like to search within: SQL WHERE Syntax SELECT column,column FROM table WHERE column operator value; Below is a selection from the “Customers” table that showed in our first article an we will use it to run some SQL sorting statements using the ORDER BY keyword: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US The following SQL statement selects all the customers with the zip code (Zip column) matching “55418”, in the “Customers” table: SELECT * FROM Customers WHERE Zip=55418; In the above search, I didn’t have to quote what I was looking for. The reason is that an integer doesn’t require quoting; however, if we were searching for a name, or any other text record, we should use quotes. So to repeat the search for Site, looking for Krypted, we would use the following: SELECT * FROM Customers WHERE Site=Krypted; We used an = operator, but it’s worth noting that there are a number of others that can be super-helpful. The following operators are available when using a WHERE clause:
    • = Equal
    • <>  or != Not equal to
    • > Greater than
    • IN Indicates multiple potential values for a column
    • < Less than
    • >= Greater than or equal
    • <= Less than or equal
    • BETWEEN Between an inclusive range
    • LIKE Looks for a provided pattern
So using another operator from above, we can also search for all sites that do not contain Krypted, using the following: SELECT * FROM Customers WHERE Site!=Krypted; So far, we’ve looked at searching for exact matches. We can also fuzzy our logic up by looking for items that contain a pattern AND something else, using the LIKE operator in a WHERE clause to search for items that contain part of a pattern in a search. The syntax for a SQL LIKE is similar, it begins with the SELECT statement that we’ve used throughout these articles so far, and then continues with the WHERE and then a LIKE to define the pattern, as follows, replacing column with the name of one of your columns, table with the name of the table that column is in and pattern with the actual search you’re looking to run: SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern; The following SQL statement selects all customers with a City starting with the letter “M” with the % inside a single quote to show where the wildcard data is (in this case, anything that appears after the letter M in a city name): SELECT * FROM Customers WHERE City LIKE 'M%'; The following SQL statement selects all customers with a City containing the pattern “Park (e.g. “Menlo Park”): SELECT * FROM Customers WHERE Country LIKE '%Park%'; You can also add NOT in front of Like to search for records that do not match a pattern (note that you don’t have all the same operators available, so this helps to get a little more logic in searches when needed). The following SQL statement selects all customers with Country NOT containing the pattern “US” (of which there are none in our sample data set): SELECT * FROM Customers WHERE Country NOT LIKE '%US%'; Or if we weren’t sure that we wanted to work in a spaceship, we could search for all addresses that weren’t spaceship: SELECT * FROM Customers WHERE Address NOT LIKE '%spaceship%'; Overall, the SELECT statement has some pretty basic logic and operations, because searching for data within a given table is a pretty straight forward task, you have a pattern, you look for items that match that pattern. As these articles continue, we’ll get into slightly more complex operations, but much of the work that we do with SQL is done after a query with another tool in order to get our data to display or be manipulated in ways that SQL doesn’t do on its own. One of the beauties of SQL, and why it can be so fast, is that it’s simple.

February 1st, 2016

Posted In: SQL

Tags: , , , , , , , , , ,

Most tasks you will execute against a database are done with SQL statements. Think of statements as a query, an insert, a change, or a delete operating. For example, to see all of your data, you would select all of the records from a database using the SELECT statement. Then we’ll ask for all, or *, and tell the command to show us where the data is coming from, which is the Customers table. Finally, we’ll be nice and tidy and put a semi-colon at the end; although if you forget, you can always do so after you hit return: SELECT * FROM Customers; As can be seen above, the SELECT statement is used to select data from a database. Results are stored in a result table that is simply called the result-set. The syntax to run a select is to run SELECT followed by a list of columns separated by commas and then a FROM statement to indicate which table of a database you’ll query followed by the name of the table and then a semi-colon (;). SELECT column name,column name FROM table name; The initial SELECT that we ran used an * instead of any column names. This worked because the * is a wild card that pulls all data. As indicated in the above syntax though, we can constrain our output to only the columns we care about. Below is the “Customers” table from our first article: ID Site Contact Address City Zip Country 1 Krypted Charles Edge my house Minneapolis 55418 US 2 Apple Tim Cook spaceship Cupertino 95014 US 3 Microsoft Satya Nadella campus Redmond 98053 US 4 Facebook Mark Zuckerberg foodhall Menlo Park 94025 US 5 JAMF Dean Hager Grain Exchange Minneapolis 55418 US This SQL statement selects the “ID” and “Site” columns from the “Customers” table: SELECT ID,Site FROM Customers; The result would be as follows: ID Site 1 Krypted 2 Apple 3 Microsoft 4 Facebook 5 JAMF In a table, each column can contain duplicate values (for example, multiple parties could be in the same city or multiple items for sale can have the same price. The DISTINCT keyword can be used to return only unique values, similar to the uniq command in bash. The syntax for the DISTINCT statement is as follows: SELECT DISTINCT column,column FROM table; The following SQL statement selects only the distinct values from the “City” columns from the “Customers” table in our example database: SELECT DISTINCT City FROM Customers; You can also search for data The WHERE clause is used to extract only those records that fulfill a specified criterion. SQL WHERE Syntax SELECT column_name,column_name FROM table_name WHERE column_name operator value; SELECT * FROM Customers WHERE Country=’US’; As you can see in the above, we used single quotes around text. We could have also used double-quotes. You do not need to quote numbers, as seen below: SELECT * FROM Customers WHERE ID=1; We used an = operator, but it’s worth noting that there are a number of others that can be super-helpful. The following operators are available when using a WHERE clause:
  • = Equal
  • <>  or != Not equal to
  • > Greater than
  • IN Indicates multiple potential values for a column
  • < Less than
  • >= Greater than or equal
  • <= Less than or equal
  • BETWEEN Between an inclusive range
  • LIKE Looks for a provided pattern

January 30th, 2016

Posted In: SQL

Tags: , , , , ,

Database won’t start? InnoDB errors are a pain. Where was krypted for a month? Did everything finally get to me and I gave up blogging? No, the site ended up having some problems with corruption in some rows of the InnoDB tables. But, I was able to get the site back up by putting the database into recovery mode. How did I do this? It’s pretty straight forward. Open my.cnf and paste these lines in there: innodb_force_recovery=3 innodb_purge_threads=0 Once the corruption is resolved, bring up empty databases and import your mysqldump into the new databases and link your site back up. But, the InnoDB force recovery puts the database into recovery mode, which is read only. So I wasn’t actually able to use the site, just look at it. At least the content was available, right? When MySQL isn’t writeable, you can’t log in as an admin, etc. The rest is one of the bigger pains I’ve encountered that didn’t result in an all nighter at a customer. I’ll write that up when I have time some day. In the meantime, next time someone changes my root password and breaks my backup scripts so I can’t just bring in a mysqldump, I’m breaking their arms. You’ve been warned.

May 15th, 2015

Posted In: Mac OS X, Ubuntu, Unix, WordPress

Tags: , , , , , , ,

« Previous PageNext Page »