General References on SQL (structured query language) SQL tutorial.

Size: px
Start display at page:

Download "General References on SQL (structured query language) SQL tutorial."

Transcription

1 Week 8 Relational Databases Reading DBI - Database programming with Perl Appendix A and B, Ch 1-5 General References on SQL (structured query language) SQL tutorial A complete SQL course with SQL interpreter MariaDB (open source fork of MySQL) Tutorialspoint - Biol Practical Biocomputing 1

2 RDBMS Relational Database Management System Proposed in the 1970s First commercial system 1981 Today databases are a pervasive element of commercial, government, and scientific information systems Open source free systems MySQL PostgreSQL Commercial Oracle Microsoft SQL Server IBM DB2 document store, column store systems Biol Practical Biocomputing 2

3 Information in relational databases is stored in tables Tables are broken up into rows and columns A database is a collection of one or more tables A RDBMS may contain multiple databases Table: Employee Column names Name Jane Doe Richard Roe Edgar Poe Address 127 Elm St. 915 W. State St. 666 Usher House Rows Columns values Biol Practical Biocomputing 3

4 Other terminology you may see Table relation file Row tuple record Column attribute field Name Jane Doe Richard Roe Edgar Poe Address 127 Elm St. 915 W. State St. 666 Usher House Phone Employee Name Address Phone Another representation of a table Biol Practical Biocomputing 4

5 Basics Each row of a table describes one thing (an entity) Each row has a unique identifier or primary key Order of the rows is arbitrary information is looked up using primary key RDBMS have highly optimized functions to look up indexed information Biol Practical Biocomputing 5

6 SQL structured query language Used to get information into and out of relational databases Official standard ANSI SQL (1992) Many vendor specific extensions Beware! Use of extensions ties you to a particular vendor Biol Practical Biocomputing 6

7 SQL Structured Query Language A high level programming language Designed to be like natural language (LOL) Gives independence from physical data storage Biol Practical Biocomputing 7

8 SQL Putting information into a relational database INSERT mysql> INSERT INTO employee VALUES ("Jane Doe", "127 Elm St.", " "); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO employee VALUES ("Richard Roe", "915 W. State St.", " "); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO employee VALUES ("Edgar Poe", "666 Usher House", " "); Query OK, 1 row affected (0.00 sec) How do you define the columns? RDBMS can be efficient because they have precise information on the type of information in the columns Makes it easy to index and look up Biol Practical Biocomputing 8

9 SQL Retrieving information from a relational database SELECT * is a wildcard select everything mysql> select * from employee; name address phone Jane Doe 127 Elm St Richard Roe 915 W. State St Edgar Poe 666 Usher House rows in set (0.00 sec) Biol Practical Biocomputing 9

10 SQL - selecting specific records SELECT column FROM table mysql> select name from employee; name Jane Doe Richard Roe Edgar Poe rows in set (0.00 sec) Biol Practical Biocomputing 10

11 SQL Selecting specific records SELECT columns FROM table mysql> select name,phone from employee; name phone Jane Doe Richard Roe Edgar Poe rows in set (0.00 sec) Biol Practical Biocomputing 11

12 SQL selecting specific records SELECT columns FROM table WHERE condition Condition is a logical condition that acts on a column (attribute) > (greater than) >= (greater or equal) < (less than) <= (less or equal) = (equal to) <> (not equal to) like (string comparison) mysql> select * from employee; name address phone age department Jane Doe 127 Elm St sales Richard Roe 915 W. State St sales Edgar Poe 666 Usher House sales Elmer Fudd 27 Pigsty Rd admin Bugs Bunny 1 Rabbit Hole Dr research Daffy Duck 43 Pond St sales Road Runner 177 Mesa Canyon Rd admin rows in set (0.00 sec) Biol Practical Biocomputing 12

13 SQL selecting specific records SELECT columns FROM table WHERE condition mysql> SELECT name FROM employee WHERE age >35; name Edgar Poe Elmer Fudd rows in set (0.00 sec) mysql> SELECT name,address FROM employee WHERE department="sales"; name address Jane Doe 127 Elm St. Richard Roe 915 W. State St. Edgar Poe 666 Usher House Daffy Duck 43 Pond St rows in set (0.00 sec) Biol Practical Biocomputing 13

14 SQL selecting specific records SELECT columns FROM table WHERE condition Conditions can contain logical operators AND OR NOT logical operators work just like in Perl - use parentheses, if needed, to group mysql> SELECT name,address FROM employee WHERE department="sales" AND age>30; name address Edgar Poe 666 Usher House Daffy Duck 43 Pond St Biol Practical Biocomputing 14

15 Counting SELECT COUNT(column) FROM table count counts the number of rows in the result mysql> SELECT COUNT(name) FROM employee; COUNT(name) row in set (0.00 sec) mysql> SELECT COUNT(*) FROM employee WHERE age>30; COUNT(*) row in set (0.01 sec) Biol Practical Biocomputing 15

16 Getting only unique things SELECT DISTINCT column FROM table counts the number of unique results mysql> SELECT department FROM employee; department sales sales sales admin research sales admin rows in set (0.01 sec) mysql> SELECT DISTINCT department FROM employee; department sales admin research rows in set (0.00 sec) Biol Practical Biocomputing 16

17 Limiting results Especially important during testing SELECT_STATEMENT LIMIT integer; mysql> SELECT * FROM employee LIMIT 3; name address phone age department Jane Doe 127 Elm St sales Richard Roe 915 W. State St sales Edgar Poe 666 Usher House sales rows in set (0.00 sec) Limit only affects the output, not the select! Biol Practical Biocomputing 17

18 Ordering results Like sorting in Perl SELECT_STATEMENT ORDER BY column ASC (ascending) SELECT_STATEMENT ORDER BY column DESC (descending) mysql> SELECT * FROM employee ORDER BY age ASC; name address phone age department Richard Roe 915 W. State St sales Road Runner 177 Mesa Canyon Rd admin Jane Doe 127 Elm St sales Bugs Bunny 1 Rabbit Hole Dr research Daffy Duck 43 Pond St sales Edgar Poe 666 Usher House sales Elmer Fudd 27 Pigsty Rd admin rows in set (0.00 sec) Biol Practical Biocomputing 18

19 Combine ORDER BY with LIMIT SQL clauses like LIMIT and ORDER BY must be in a specific order. This is where the reference manual is useful In MariaDB, just type in search box mysql> SELECT * FROM employee ORDER BY age DESC LIMIT 5; name address phone age department Elmer Fudd 27 Pigsty Rd admin Edgar Poe 666 Usher House sales Bugs Bunny 1 Rabbit Hole Dr research Daffy Duck 43 Pond St sales Jane Doe 127 Elm St sales rows in set (0.00 sec) Biol Practical Biocomputing 19

20 SELECT syntax (from help SELECT) SELECT [ALL DISTINCT DISTINCTROW ] select_expr,... [INTO OUTFILE 'file_name'] [FROM table_reference [WHERE where_definition] [GROUP BY {col_name [ASC DESC] ] [ORDER BY col_name [ASC DESC] ] [LIMIT row_count] ] SQL expressions [] optional {} choice options, choose 1 Biol Practical Biocomputing 20

21 Pooling things together GROUP BY SELECT_STATEMENT GROUP BY column mysql> SELECT name,phone,department FROM employee GROUP BY department; name phone department Elmer Fudd admin Bugs Bunny research Jane Doe sales rows in set (0.00 sec) Note the difference between ORDER BY and GROUP BY GROUP BY only reports the first record in the group Biol Practical Biocomputing 21

22 Counting types of things Combine GROUP BY with COUNT mysql> SELECT department,count(*) FROM employee GROUP BY department; department count(*) admin 2 research 1 sales rows in set (0.01 sec) Biol Practical Biocomputing 22

23 Sending output to a file SELECT_STATEMENT INTO OUTFILE filename Default location for output file is the mysql data directory. If you do not have privilege to write there, you must supply a complete path mysql> SELECT * FROM employee INTO OUTFILE "employee.txt"; Query OK, 7 rows affected (0.11 sec) mysql> SELECT * FROM employee INTO OUTFILE "/home/gribskov/employee.txt"; Biol Practical Biocomputing 23

24 Company Example Employee table has one-to-one relationships each employee has only one name, address, phone, etc. Employee Name Address Phone Age Biol Practical Biocomputing 24

25 Multiple tables SQL is mostly used for gathering information from multiple tables Say we want to add information about the department head and mailcode to our little example Employee Name Address Phone Age Department Employee Name Address Phone Age Department Manager Mailcode Since there are only three departments, admin, sales, and research much of this information is redundant Redundancy is bad because it wastes space it makes updating hard (stale information) error prone Biol Practical Biocomputing 25

26 Multiple tables More efficient: Break the information into two tables Employee and Department In the Department table the Department is the "primary key" In the Employee table, the Department is a foreign key There is a one-to-many relationship between department and employee (each department has many employees; each employee has only one department) Employee Name Address Phone Age Department Department Department Manager Mailcode Biol Practical Biocomputing 26

27 Relational Database Department & Employee tables mysql> SELECT * FROM department; department manager mailcode sales Bob Brown 156 admin Susan Sepia 162 research Roger Rabbit rows in set (0.00 sec) mysql> SELECT * FROM employee; name address phone age department Jane Doe 127 Elm St sales Richard Roe 915 W. State St sales Edgar Poe 666 Usher House sales Elmer Fudd 27 Pigsty Rd admin Bugs Bunny 1 Rabbit Hole Dr research Daffy Duck 43 Pond St sales Road Runner 177 Mesa Canyon Rd admin rows in set (0.00 sec) Biol Practical Biocomputing 27

28 Multiple tables The lookup of information across two tables requires the tables to be joined based on a common field The common field is the foreign key / primary key field Columns in tables may have the same names When multiple tables are involved the you must provide the table name for clarity - table_name.column_name Join finds every matching row in table 1, and concatenates it to every matching row in table 2 If you do this without specifying a column to join on this is called a cartesian join it generates the number of rows in table 1 times the number of rows in table 2 DANGER! What if each table contains 10,000 records? Biol Practical Biocomputing 28

29 Cartesian join Employee x Department mysql> SELECT * FROM employee,department; name address phone age department department manager mailcode Jane Doe 127 Elm St sales sales Bob Brown 156 Richard Roe 915 W. State St sales sales Bob Brown 156 Edgar Poe 666 Usher House sales sales Bob Brown 156 Elmer Fudd 27 Pigsty Rd admin sales Bob Brown 156 Bugs Bunny 1 Rabbit Hole Dr research sales Bob Brown 156 Daffy Duck 43 Pond St sales sales Bob Brown 156 Road Runner 177 Mesa Canyon Rd admin sales Bob Brown 156 Jane Doe 127 Elm St sales admin Susan Sepia 162 Richard Roe 915 W. State St sales admin Susan Sepia 162 Edgar Poe 666 Usher House sales admin Susan Sepia 162 Elmer Fudd 27 Pigsty Rd admin admin Susan Sepia 162 Bugs Bunny 1 Rabbit Hole Dr research admin Susan Sepia 162 Daffy Duck 43 Pond St sales admin Susan Sepia 162 Road Runner 177 Mesa Canyon Rd admin admin Susan Sepia 162 Jane Doe 127 Elm St sales research Roger Rabbit 102 Richard Roe 915 W. State St sales research Roger Rabbit 102 Edgar Poe 666 Usher House sales research Roger Rabbit 102 Elmer Fudd 27 Pigsty Rd admin research Roger Rabbit 102 Bugs Bunny 1 Rabbit Hole Dr research research Roger Rabbit 102 Daffy Duck 43 Pond St sales research Roger Rabbit 102 Road Runner 177 Mesa Canyon Rd admin research Roger Rabbit rows in set (0.00 sec) Biol Practical Biocomputing 29

30 Selecting certain columns In a cartesian join we don't get the correct manager because we haven't insisted that employee.department and department.department match mysql> SELECT employee.name,employee.department,department.manager FROM employee,department; name department manager Jane Doe sales Bob Brown Richard Roe sales Bob Brown Edgar Poe sales Bob Brown Elmer Fudd admin Bob Brown Bugs Bunny research Bob Brown Daffy Duck sales Bob Brown Road Runner admin Bob Brown Jane Doe sales Susan Sepia Richard Roe sales Susan Sepia Edgar Poe sales Susan Sepia Elmer Fudd admin Susan Sepia Bugs Bunny research Susan Sepia Daffy Duck sales Susan Sepia Road Runner admin Susan Sepia Jane Doe sales Roger Rabbit Richard Roe sales Roger Rabbit Edgar Poe sales Roger Rabbit Elmer Fudd admin Roger Rabbit Bugs Bunny research Roger Rabbit Daffy Duck sales Roger Rabbit Road Runner admin Roger Rabbit rows in set (0.00 sec) Biol Practical Biocomputing 30

31 Joining on a column that is equal (equijoin) Avoids Cartesian join mysql> SELECT employee.name,employee.department,department.manager -> FROM employee,department -> WHERE employee.department=department.department; name department manager Jane Doe sales Bob Brown Richard Roe sales Bob Brown Edgar Poe sales Bob Brown Daffy Duck sales Bob Brown Elmer Fudd admin Susan Sepia Road Runner admin Susan Sepia Bugs Bunny research Roger Rabbit rows in set (0.00 sec) Biol Practical Biocomputing 31

32 Joining on a column that is equal (equijoin) with conditions mysql> SELECT employee.name,employee.department,department.manager,department.mailcode -> FROM employee,department -> WHERE employee.department=department.department AND employee.age>40; name department manager mailcode Edgar Poe sales Bob Brown 156 Elmer Fudd admin Susan Sepia rows in set (0.00 sec) Biol Practical Biocomputing 32

33 Joins with conditions Conditions can be on any attribute (column) Think of conditions as working on the cartesian product Who is the manager of each employee whose name ends "oe" or works in the admin department? mysql> SELECT employee.name,employee.department,department.manager -> FROM employee,department -> WHERE employee.department=department.department -> AND employee.name like "%oe%" -> OR employee.department="admin"; Note: % is the "match any number of characters" symbol in SQL for LIKE, not * as in Perl? Biol Practical Biocomputing 33

34 Joins with conditions Conditions can be on any attribute (column) Think of conditions as working on the cartesian product mysql> SELECT employee.name,employee.department,department.manager -> FROM employee,department -> WHERE employee.department=department.department -> AND employee.name like "%oe%" -> OR employee.department="admin"; name department manager Jane Doe sales Bob Brown Richard Roe sales Bob Brown Edgar Poe sales Bob Brown Elmer Fudd admin Bob Brown Road Runner admin Bob Brown Elmer Fudd admin Susan Sepia Road Runner admin Susan Sepia Elmer Fudd admin Roger Rabbit Road Runner admin Roger Rabbit rows in set (0.00 sec) Biol Practical Biocomputing 34

35 Joins with conditions Conditions can be on any attribute (column) Think of conditions as working on the cartesian product SQL has operator precedence (similar to perl) mysql> SELECT employee.name,employee.department,department.manager -> FROM employee,department -> WHERE employee.department=department.department -> AND employee.name like "%oe%" -> OR employee.department="admin"; name department manager Jane Doe sales Bob Brown Richard Roe sales Bob Brown employee.name like %oe% Edgar Poe sales Bob Brown Elmer Fudd admin Bob Brown Road Runner admin Bob Brown Elmer Fudd admin Susan Sepia Road Runner admin Susan Sepia employee.department = admin Elmer Fudd admin Roger Rabbit Road Runner admin Roger Rabbit rows in set (0.00 sec) SQL interpretation: (where employee,department=department.department AND employee.name like "%oe%" ) OR ( where employee.department = "admin") Biol Practical Biocomputing 35

36 -> WHERE employee.department=department.department -> AND employee.name like "%oe%" -> OR employee.department="admin" name address phone age department department manager mailcode Jane Doe 127 Elm St sales sales Bob Brown 156 Richard Roe 915 W. State St sales sales Bob Brown 156 Edgar Poe 666 Usher House sales sales Bob Brown 156 Elmer Fudd 27 Pigsty Rd admin sales Bob Brown 156 Bugs Bunny 1 Rabbit Hole Dr research sales Bob Brown 156 Daffy Duck 43 Pond St sales sales Bob Brown 156 Road Runner 177 Mesa Canyon Rd admin sales Bob Brown 156 Jane Doe 127 Elm St sales admin Susan Sepia 162 Richard Roe 915 W. State St sales admin Susan Sepia 162 Edgar Poe 666 Usher House sales admin Susan Sepia 162 Elmer Fudd 27 Pigsty Rd admin admin Susan Sepia 162 Bugs Bunny 1 Rabbit Hole Dr research admin Susan Sepia 162 Daffy Duck 43 Pond St sales admin Susan Sepia 162 Road Runner 177 Mesa Canyon Rd admin admin Susan Sepia 162 Jane Doe 127 Elm St sales research Roger Rabbit 102 Richard Roe 915 W. State St sales research Roger Rabbit 102 Edgar Poe 666 Usher House sales research Roger Rabbit 102 Elmer Fudd 27 Pigsty Rd admin research Roger Rabbit 102 Bugs Bunny 1 Rabbit Hole Dr research research Roger Rabbit 102 Daffy Duck 43 Pond St sales research Roger Rabbit 102 Road Runner 177 Mesa Canyon Rd admin research Roger Rabbit rows in set (0.00 sec) Biol Practical Biocomputing 36

37 Joins with conditions mysql> SELECT employee.name,employee.department,department.manager -> FROM employee,department -> WHERE employee.department=department.department -> AND (employee.name like "%oe%" -> OR employee.department="admin"); name department manager Jane Doe sales Bob Brown Richard Roe sales Bob Brown Edgar Poe sales Bob Brown Elmer Fudd admin Susan Sepia Road Runner admin Susan Sepia rows in set (0.00 sec) Biol Practical Biocomputing 37

38 Project table Add a table with the projects on which each employee works mysql> select * from project; number title name database Jane Doe 2 space shuttle Richard Roe 3 database Edgar Poe 4 space shuttle Bugs Bunny 5 space shuttle Road Runner 6 database Elmer Fudd 7 nation building Daffy Duck 8 nation building Bugs Bunny Neither project.name nor project.title are unique Use autoincrementing number as primary key many-to-many relationship: each project has many employees, each employee has many projects Biol Practical Biocomputing 38

39 What Projects does department manager X have? Department and Project have no common attribute but can be joined via Employee Project Number Title Name Employee Name Address Phone Age Department Department Department Manager Mailcode Biol Practical Biocomputing 39

40 What Projects does department manager X have? mysql> SELECT department.department,department.manager,project.title -> FROM department,employee,project -> WHERE department.department=employee.department -> AND employee.name=project.name -> AND department.department="sales"; department manager title sales Bob Brown database sales Bob Brown space shuttle sales Bob Brown database sales Bob Brown nation building rows in set (0.00 sec) database appears twice Biol Practical Biocomputing 40

41 What Projects does department manager X have? Use DISTINCT to get unique list mysql> SELECT DISTINCT department.department,department.manager, project.title -> FROM department,employee,project -> WHERE department.department=employee.department -> AND employee.name=project.name -> AND department.department="sales"; department manager title sales Bob Brown database sales Bob Brown space shuttle sales Bob Brown nation building Biol Practical Biocomputing 41

42 Shorthand for table names I'm getting tired of typing table names, let's abbreviate them mysql> SELECT DISTINCT d.department,d.manager, p.title -> FROM department d, employee e, project p -> WHERE d.department=e.department -> AND e.name=p.name -> AND d.department="sales"; department manager title sales Bob Brown database sales Bob Brown space shuttle sales Bob Brown nation building Biol Practical Biocomputing 42

43 MySQL Creating tables Data in RDBMS are carefully defined so that they can be efficiently stored and accessed CREATE table_name ( column_spec ); column_spec = column_name column_type [, column_spec] Column types are often vendor-specific Types numeric date and time string Biol Practical Biocomputing 43

44 MySQL Creating tables Column Types (Numeric) UNSIGNED, if specified, disallows negative values, useful for unique IDs TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255. Can also use BIT, BOOL, BOOLEAN SMALLINT[(M)] [UNSIGNED] [ZEROFILL] A small integer. The signed range is to The unsigned range is 0 to MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL] A medium-sized integer. The signed range is to The unsigned range is 0 to INT[(M)] [UNSIGNED] [ZEROFILL] A normal-size integer. The signed range is to The unsigned range is 0 to can also use INTEGER[(M)] [UNSIGNED] [ZEROFILL] BIGINT[(M)] [UNSIGNED] [ZEROFILL] A large integer. The signed range is to The unsigned range is 0 to FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] A small (single-precision) floating-point number. Allowable values are E+38 to E-38, 0, and E-38 to E+38. These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system. M is the total number of decimal digits and D is the number of digits following the decimal point. If M and D are omitted, values are stored to the limits allowed by the hardware. A single-precision floating-point number is accurate to approximately 7 decimal places. DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] A normal-size (double-precision) floating-point number. Allowable values are E+308 to E-308, 0, and E-308 to E+308. These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system. can also use DOUBLE PRECISION[(M,D)] [UNSIGNED] [ZEROFILL], REAL[(M,D)] [UNSIGNED] [ZEROFILL] DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL] An unpacked fixed-point number. Behaves like a CHAR column; unpacked means the number is stored as a string, using one character for each digit of the value. M is the total number of digits and D is the number of digits after the decimal point. The decimal point and (for negative numbers) the - sign are not counted in M, although space for them is reserved. If D is 0, values have no decimal point or fractional part. The maximum range of DECIMAL values is the same as for DOUBLE, but the actual range for a given DECIMAL column may be constrained by the choice of M and D. If D is omitted, the default is 0. If M is omitted, the default is 10. can also use DEC[(M[,D])] [UNSIGNED] [ZEROFILL], NUMERIC[(M[,D])] [UNSIGNED] [ZEROFILL], FIXED[(M[,D])] [UNSIGNED] [ZEROFILL] Biol Practical Biocomputing 44

45 MySQL Creating tables Date and time columns DATE A date. The supported range is ' ' to ' '. MySQL displays DATE values in 'YYYY-MM-DD' format, but allows you to assign values to DATE columns using either strings or numbers. DATETIME A date and time combination. The supported range is ' :00:00' to ' :59:59'. MySQL displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format, but allows you to assign values to DATETIME columns using either strings or numbers. TIMESTAMP[(M)] A timestamp. The range is ' :00:00' to partway through the year A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation. By default, the first TIMESTAMP column in a table is automatically set to the date and time of the most recent operation if you do not assign it a value yourself. You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value. TIME A time. The range is '-838:59:59' to '838:59:59'. MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME columns using either strings or numbers. YEAR[(2 4)] A year in two-digit or four-digit format. The default is four-digit format. In four-digit format, the allowable values are 1901 to 2155, and In two-digit format, the allowable values are 70 to 69, representing years from 1970 to MySQL displays YEAR values in YYYY format, but allows you to assign values to YEAR columns using either strings or numbers. The YEAR type is unavailable prior to MySQL Biol Practical Biocomputing 45

46 MySQL Creating tables String column types CHAR and VARCHAR Types Character strings of up to a specified length BINARY and VARBINARY Types Byte strings rather than character strings. This means that they have no character set, and sorting and comparison are based on the numeric values of the bytes in the values. BLOB and TEXT Types Binary large object - large char and binary ENUM Type A single string object that must be one of a list of values specified when the table is created For example, a column specified as ENUM('one', 'two', 'three') can have values NULL, "", "one","two","three" An enumeration can have a maximum of 65,535 elements. SET Type A SET is a string object that can have zero or more values, each of which must be chosen from a list of allowed values specified when the table is created. Biol Practical Biocomputing 46

47 MySQL Creating tables String columns CHAR This type is a synonym for CHAR(1). [NATIONAL] VARCHAR(M) [BINARY] A variable-length string. M represents the maximum column length. The range of M is 0 to 255 Note: Trailing spaces are removed when VARCHAR values are stored. This differs from the standard SQL specification. VARCHAR is shorthand for CHARACTER VARYING. BINARY(M) (MySQL 4.1.2) The BINARY type is similar to the CHAR type, but stores binary byte strings rather than non-binary character strings. VARBINARY(M) (MySQL 4.1.2) The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than non-binary character strings. ENUM('value1','value2',...) An enumeration. A string object that can have only one value, chosen from the list of values 'value1', 'value2',..., NULL or the special '' error value. An ENUM column can have a maximum of 65,535 distinct values. ENUM values are represented internally as integers. SET('value1','value2',...) A set. A string object that can have zero or more values, each of which must be chosen from the list of values 'value1', 'value2',... A SET column can have a maximum of 64 members. SET values are represented internally as integers. TEXT[(M)] A TEXT column with a maximum length of 65,535 (216 1) characters. an optional length M can be given for this type. MySQL creates the column as the smallest TEXT type large enough to hold values M characters long. TINYTEXT - A TEXT column with a maximum length of 255 (28 1) characters. MEDIUMTEXT - A TEXT column with a maximum length of 16,777,215 (224 1) characters. LONGTEXT - A TEXT column with a maximum length of 4,294,967,295 or 4GB (232 1) characters. BLOB[(M)] Binary Large Objects (e.g., images) come in the same sizes as TEXT. TINYBLOB, BLOB, MEDIUM BLOB, AND LONGBLOB Biol Practical Biocomputing 47

48 MySQL Creating tables mysql> CREATE TABLE employee ( mysql> name tinytext, address tinytext, phone tinytext, age int); Query OK, 0 rows affected (0.04 sec) mysql> DESCRIBE employee; Field Type Null Key Default Extra name tinytext YES NULL address tinytext YES NULL phone tinytext YES NULL age int(11) YES NULL rows in set (0.00 sec) Biol Practical Biocomputing 48

49 MySQL Creating tables PRIMARY KEY DEFAULT AUTO_INCREMENT NULL /NOT NULL column_definition: col_name type [NOT NULL NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] [PRIMARY] KEY] type: TINYINT[(length)] [UNSIGNED] [ZEROFILL] SMALLINT[(length)] [UNSIGNED] [ZEROFILL] MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL] INT[(length)] [UNSIGNED] [ZEROFILL] INTEGER[(length)] [UNSIGNED] [ZEROFILL] BIGINT[(length)] [UNSIGNED] [ZEROFILL] REAL[(length,decimals)] [UNSIGNED] [ZEROFILL] DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL] FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL] DECIMAL(length,decimals) [UNSIGNED] [ZEROFILL] NUMERIC(length,decimals) [UNSIGNED] [ZEROFILL] DATE TIME TIMESTAMP DATETIME YEAR CHAR(length) [BINARY ASCII UNICODE] VARCHAR(length) [BINARY] BINARY(length) VARBINARY(length) TINYBLOB BLOB MEDIUMBLOB LONGBLOB TINYTEXT [BINARY] TEXT [BINARY] MEDIUMTEXT [BINARY] LONGTEXT [BINARY] ENUM(value1,value2,value3,...) SET(value1,value2,value3,...) Biol Practical Biocomputing 49

50 MySQL Null values Null values are values that are missing in a column similar to undefined value in Perl Null is different from 0 or "" 0 is the value zero "" is a zero length string NULL (null) is nothing at all, an undefined value NULL/NOT NULL indicates whether NULL values are allowed default is NULL Biol Practical Biocomputing 50

51 MySQL INSERT Putting data into tables mysql> CREATE TABLE project ( -> number INTEGER AUTO_INCREMENT PRIMARY KEY, -> title TINYTEXT, -> name TINYTEXT ); Query OK, 0 rows affected (0.02 sec) mysql> INSERT INTO project VALUES ( NULL, "database", "Jane Doe" ); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO project VALUES ( NULL, "space shuttle", "Richard Roe" ); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO project VALUES ( NULL, "nation building", "Bugs Bunny" ); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM project; number title name database Jane Doe 2 space shuttle Richard Roe 3 nation building Bugs Bunny rows in set (0.00 sec) Biol Practical Biocomputing 51

52 MySQL LOAD inserting data from a file load is much faster than inserting line by line LOAD DATA INFILE 'file_name' INTO TABLE tbl_name [FIELDS [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char'] ] [LINES [STARTING BY 'string'] [TERMINATED BY 'string'] ] [IGNORE number LINES] [(col_name,...)] Biol Practical Biocomputing 52

53 MySQL LOAD DATA INFILE "filename" INTO TABLE tablename Remember, default file location is mysql directory, provide a path mysql> LOAD DATA INFILE "/home/gribskov/project.txt" INTO TABLE project; Query OK, 10 rows affected, 22 warnings (0.01 sec) Records: 10 Deleted: 0 Skipped: 0 Warnings: 22 mysql> SELECT * FROM project; number title name database Jane Doe 2 space shuttle Richard Roe 3 nation building Bugs Bunny 4 space shuttle Bugs Bunny 5 space shuttle Road Runner 6 database Elmer Fudd 7 nation building Daffy Duck 8 nation building Bugs Bunny 9 database Edgar Poe rows in set (0.00 sec) "project.txt" 10 lines, 216 characters NULL database "Jane Doe" NULL "space shuttle" "Richard Roe" NULL "space shuttle" "Bugs Bunny" NULL "space shuttle" "Road Runner" NULL database "Elmer Fudd" NULL "nation building" "Daffy Duck" NULL "nation building" "Bugs Bunny" NULL database "Edgar Poe" Biol Practical Biocomputing 53

54 MySQL INSERT Inserting values from one table into another INSERT [INTO] tbl_name [(col_name,...)] SELECT_STATEMENT Inserting only some values INSERT [INTO] tbl_name [(list_of_col_name)] VALUES (list_of_values); works for INSERT...SELECT and LOAD Biol Practical Biocomputing 54

55 MySQL DELETE Remove rows - be very careful, if you do not specify a condition it deletes the entire contents of the table Rule: always select first and confirm that you get the rows you want, then change SELECT to DELETE DELETE FROM table_name WHERE condition mysql> SELECT * FROM project WHERE number>21; number title name NULL NULL 23 NULL NULL rows in set (0.00 sec) mysql> SELECT * FROM project WHERE title IS NULL; number title name NULL NULL 23 NULL NULL rows in set (0.00 sec) Biol Practical Biocomputing 55

56 MySQL DELETE mysql> DELETE FROM project WHERE title IS NULL; Query OK, 2 rows affected (0.00 sec) mysql> SELECT * FROM project; number title name database Jane Doe 2 space shuttle Richard Roe 3 nation building Bugs Bunny 17 "space shuttle" "Bugs Bunny" 16 database "Edgar Poe" 15 "space shuttle" "Richard Roe" 14 database "Jane Doe" 18 "space shuttle" "Road Runner" 19 database "Elmer Fudd" 20 "nation building" "Daffy Duck" 21 "nation building" "Bugs Bunny" rows in set (0.00 sec) Biol Practical Biocomputing 56

57 MySQL DROP Removes tables and their contents DROP table_name; Removes databases, all tables, and contents DROP database_name; Be careful with DROP and DELETE When its gone... its gone for good Biol Practical Biocomputing 57

58 MySQL ALTER TABLE - Change tables after they are created Add columns delete columns change column definitions ALTER TABLE tbl_name alter_specification alter_specification: ADD [COLUMN] column_definition [FIRST AFTER col_name ] ADD [COLUMN] (column_definition,...) ADD INDEX [index_name] [index_type] (index_col_name,...) ALTER [COLUMN] col_name {SET DEFAULT literal DROP DEFAULT} CHANGE [COLUMN] old_col_name column_definition [FIRST AFTER col_name] MODIFY [COLUMN] column_definition [FIRST AFTER col_name] DROP [COLUMN] col_name DROP PRIMARY KEY DROP INDEX index_name RENAME [TO] new_tbl_name ORDER BY col_name Biol Practical Biocomputing 58

59 MariaDB Read the MariaDB documentation to find out User privileges Restricting access to databases Data dumps How data is stored in tables Biol Practical Biocomputing 59

TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255. MySQL: Data Types 1. Numeric Data Types ZEROFILL automatically adds the UNSIGNED attribute to the column. UNSIGNED disallows negative values. SIGNED (default) allows negative values. BIT[(M)] A bit-field

More information

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia Lab # 2 Hands-On DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia Part A: Demo by Instructor in Lab a. Data type of MySQL b. CREATE table c. ALTER table (ADD, CHANGE,

More information

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus CSCI-UA:0060-02 Database Design & Web Implementation Professor Evan Sandhaus sandhaus@cs.nyu.edu evan@nytimes.com Lecture #10: Open Office Base, Life on the Console, MySQL Database Design and Web Implementation

More information

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus CSCI-UA:0060-02 Database Design & Web Implementation Professor Evan Sandhaus sandhaus@cs.nyu.edu evan@nytimes.com Lecture #15: Post Spring Break Massive MySQL Review Database Design and Web Implementation

More information

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018 Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018 Background Information 2 Background Information What is a (Relational) Database 3 Dynamic collection of information. Organized into tables,

More information

Data and Tables. Bok, Jong Soon

Data and Tables. Bok, Jong Soon Data and Tables Bok, Jong Soon Jongsoon.bok@gmail.com www.javaexpert.co.kr Learning MySQL Language Structure Comments and portability Case-sensitivity Escape characters Naming limitations Quoting Time

More information

MySQL Introduction. By Prof. B.A.Khivsara

MySQL Introduction. By Prof. B.A.Khivsara MySQL Introduction By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use. Introduction

More information

Chapter 3 Introduction to relational databases and MySQL

Chapter 3 Introduction to relational databases and MySQL Chapter 3 Introduction to relational databases and MySQL Murach's PHP and MySQL, C3 2014, Mike Murach & Associates, Inc. Slide 1 Objectives Applied 1. Use phpmyadmin to review the data and structure of

More information

Pagina 1 di 7 13.1.7. SELECT Syntax 13.1.7.1. JOIN Syntax 13.1.7.2. UNION Syntax SELECT [ALL DISTINCT DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]

More information

The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014

The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014 The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014 MySQL Setup, using console Data types Overview Creating users, databases and tables SQL queries INSERT, SELECT, DELETE WHERE, ORDER

More information

Exact Numeric Data Types

Exact Numeric Data Types SQL Server Notes for FYP SQL data type is an attribute that specifies type of data of any object. Each column, variable and expression has related data type in SQL. You would use these data types while

More information

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time. - Database P&A Data Types in MySQL MySQL Data Types Data types define the way data in a field can be manipulated For example, you can multiply two numbers but not two strings We have seen data types mentioned

More information

Provider: MySQLAB Web page:

Provider: MySQLAB Web page: Provider: MySQLAB Web page: www.mysql.com Installation of MySQL. Installation of MySQL. Download the mysql-3.3.5-win.zip and mysql++-.7.--win3-vc++.zip files from the mysql.com site. Unzip mysql-3.3.5-win.zip

More information

Chapter 8 How to work with data types

Chapter 8 How to work with data types Chapter 8 How to work with data types Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 1 Objectives Applied Code queries that convert data from one data type to another. Knowledge Describe

More information

Careerarm.com. 1. What is MySQL? MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle)

Careerarm.com. 1. What is MySQL? MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle) 1. What is MySQL? MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle) 2. What are the technical features of MySQL? MySQL database software is a client

More information

Model Question Paper. Credits: 4 Marks: 140

Model Question Paper. Credits: 4 Marks: 140 Model Question Paper Subject Code: BT0075 Subject Name: RDBMS and MySQL Credits: 4 Marks: 140 Part A (One mark questions) 1. MySQL Server works in A. client/server B. specification gap embedded systems

More information

Databases: MySQL introduction

Databases: MySQL introduction Databases: MySQL introduction IT Learning Centre 2 How to Use this User Guide This handbook accompanies the taught sessions for the course. Each section contains a brief overview of a topic for your reference

More information

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation Basic SQL Dr Fawaz Alarfaj Al Imam Mohammed Ibn Saud Islamic University ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation MIDTERM EXAM 2 Basic

More information

Unit 1 - Chapter 4,5

Unit 1 - Chapter 4,5 Unit 1 - Chapter 4,5 CREATE DATABASE DatabaseName; SHOW DATABASES; USE DatabaseName; DROP DATABASE DatabaseName; CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,... columnn

More information

Create Basic Databases and Integrate with a Website Lesson 1

Create Basic Databases and Integrate with a Website Lesson 1 Create Basic Databases and Integrate with a Website Lesson 1 Getting Started with Web (SQL) Databases In order to make a web database, you need three things to work in cooperation. You need a web server

More information

Overview of MySQL Structure and Syntax [2]

Overview of MySQL Structure and Syntax [2] PHP PHP MySQL Database Overview of MySQL Structure and Syntax [2] MySQL is a relational database system, which basically means that it can store bits of information in separate areas and link those areas

More information

MySQL: an application

MySQL: an application Data Types and other stuff you should know in order to amaze and dazzle your friends at parties after you finally give up that dream of being a magician and stop making ridiculous balloon animals and begin

More information

Basic SQL. Basic SQL. Basic SQL

Basic SQL. Basic SQL. Basic SQL Basic SQL Dr Fawaz Alarfaj Al Imam Mohammed Ibn Saud Islamic University ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation Basic SQL Structured

More information

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client Lab 2.0 - MySQL CISC3140, Fall 2011 DUE: Oct. 6th (Part 1 only) Part 1 1. Getting started This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client host

More information

MySQL Introduction. By Prof. B.A.Khivsara

MySQL Introduction. By Prof. B.A.Khivsara MySQL Introduction By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use. Outline Design

More information

CSE 530A SQL. Washington University Fall 2013

CSE 530A SQL. Washington University Fall 2013 CSE 530A SQL Washington University Fall 2013 SELECT SELECT * FROM employee; employee_id last_name first_name department salary -------------+-----------+------------+-----------------+-------- 12345 Bunny

More information

Data Definition Language with mysql. By Kautsar

Data Definition Language with mysql. By Kautsar Data Definition Language with mysql By Kautsar Outline Review Create/Alter/Drop Database Create/Alter/Drop Table Create/Alter/Drop View Create/Alter/Drop User Kautsar -2 Review Database A container (usually

More information

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 304 Introduction to Database Systems SQL DDL Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca SQL Overview Structured Query Language or SQL is the standard query language

More information

SQL Commands & Mongo DB New Syllabus

SQL Commands & Mongo DB New Syllabus Chapter 15 : Computer Science Class XI ( As per CBSE Board) SQL Commands & Mongo DB New Syllabus 2018-19 SQL SQL is an acronym of Structured Query Language.It is a standard language developed and used

More information

MySQL by Examples for Beginners

MySQL by Examples for Beginners yet another insignificant programming notes... HOME MySQL by Examples for Beginners Read "How to Install MySQL and Get Started" on how to install, customize, and get started with MySQL. 1. Summary of MySQL

More information

Basis Data Terapan. Yoannita

Basis Data Terapan. Yoannita Basis Data Terapan Yoannita SQL Server Data Types Character strings: Data type Description Storage char(n) varchar(n) varchar(max) text Fixed-length character string. Maximum 8,000 characters Variable-length

More information

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION SESSION ELEVEN 11.1 Walkthrough examples More MySQL This session is designed to introduce you to some more advanced features of MySQL, including loading your own database. There are a few files you need

More information

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1 COSC344 Database Theory and Applications Lecture 5 SQL - Data Definition Language COSC344 Lecture 5 1 Overview Last Lecture Relational algebra This Lecture Relational algebra (continued) SQL - DDL CREATE

More information

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109 Index A abbreviations in field names, 22 in table names, 31 Access. See under Microsoft acronyms in field names, 22 in table names, 31 aggregate functions, 74, 375 377, 416 428. See also AVG; COUNT; COUNT(*);

More information

CSC Web Programming. Introduction to SQL

CSC Web Programming. Introduction to SQL CSC 242 - Web Programming Introduction to SQL SQL Statements Data Definition Language CREATE ALTER DROP Data Manipulation Language INSERT UPDATE DELETE Data Query Language SELECT SQL statements end with

More information

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Tenth Edition Chapter 7 Introduction to Structured Query Language (SQL) Objectives In this chapter, students will learn: The basic commands and

More information

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas SQL SQL Functionality stands for Structured Query Language sometimes pronounced sequel a very-high-level (declarative) language user specifies what is wanted, not how to find it number of standards original

More information

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior SQL Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior 1 DDL 2 DATA TYPES All columns must have a data type. The most common data types in SQL are: Alphanumeric: Fixed length:

More information

Deepak Bhinde PGT Comp. Sc.

Deepak Bhinde PGT Comp. Sc. Deepak Bhinde PGT Comp. Sc. SQL Elements in MySQL Literals: Literals refers to the fixed data value. It may be Numeric or Character. Numeric literals may be integer or real numbers and Character literals

More information

*this is a guide that was created from lecture videos and is used to help you gain an understanding of what is mysql used for.

*this is a guide that was created from lecture videos and is used to help you gain an understanding of what is mysql used for. mysql. 1 what is mysql used for. *this is a guide that was created from lecture videos and is used to help you gain an understanding of what is mysql used for. MySQL Installation MySQL is an open source

More information

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 6 Basic SQL Slide 6-2 Chapter 6 Outline SQL Data Definition and Data Types Specifying Constraints in SQL Basic Retrieval Queries in SQL INSERT, DELETE, and UPDATE Statements in SQL Additional Features

More information

GridDB Advanced Edition SQL reference

GridDB Advanced Edition SQL reference GMA022C1 GridDB Advanced Edition SQL reference Toshiba Solutions Corporation 2016 All Rights Reserved. Introduction This manual describes how to write a SQL command in the GridDB Advanced Edition. Please

More information

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Outline SQL Data Definition and Data Types Specifying Constraints in SQL Basic Retrieval Queries

More information

Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3!

Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3! Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3! http://dev.mysql.com/doc/refman/ https://mariadb.com/kb/en/the-mariadb-library/documentation/ MySQL / MariaDB 1 College Database E-R Diagram

More information

INTRODUCTION TO MYSQL MySQL : It is an Open Source RDBMS Software that uses Structured Query Language. It is available free of cost. Key Features of MySQL : MySQL Data Types: 1. High Speed. 2. Ease of

More information

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210 SQL: Concepts Todd Bacastow IST 210: Organization of Data 2/17/2004 1 Design questions How many entities are there? What are the major entities? What are the attributes of each entity? Is there a unique

More information

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity SQL language: basics Creating a table Modifying table structure Deleting a table The data dictionary Data integrity 2013 Politecnico di Torino 1 Creating a table Creating a table (1/3) The following SQL

More information

Tool/Web URL Features phpmyadmin. More on phpmyadmin under User Intefaces. MySQL Query Browser

Tool/Web URL Features phpmyadmin.   More on phpmyadmin under User Intefaces. MySQL Query Browser To store data in MySQL, we will set up a database and then place tables, relationships and other objects in that database, following a design that maps to our application requirements. We will use a command-line

More information

Mysql Create Table Example Primary Key Foreign

Mysql Create Table Example Primary Key Foreign Mysql Create Table Example Primary Key Foreign Key Now, i want to connect this two table by the use of id primary key of girls and want to make it See: How to create a Minimal, Complete, and Verifiable

More information

Simple Quesries in SQL & Table Creation and Data Manipulation

Simple Quesries in SQL & Table Creation and Data Manipulation Simple Quesries in SQL & Table Creation and Data Manipulation Based on CBSE Curriculum Class -11 By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region Neha Tyagi, PGT CS II Shift Jaipur Introduction

More information

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types Dr. Shohreh Ajoudanian Course Topics Microsoft SQL Server 01 Installing MSSQL Server 2008 03 Creating a database 05 Querying Tables with SELECT 07 Using Set Operators 02 Data types 04 Creating a table,

More information

Advanced SQL. Nov 21, CS445 Pacific University 1

Advanced SQL. Nov 21, CS445 Pacific University 1 Advanced SQL Nov 21, 2017 http://zeus.cs.pacificu.edu/chadd/cs445f17/advancedsql.tar.gz Pacific University 1 Topics Views Triggers Stored Procedures Control Flow if / case Binary Data Pacific University

More information

SQL Data Definition and Data Manipulation Languages (DDL and DML)

SQL Data Definition and Data Manipulation Languages (DDL and DML) .. Cal Poly CPE/CSC 365: Introduction to Database Systems Alexander Dekhtyar.. SQL Data Definition and Data Manipulation Languages (DDL and DML) Note: This handout instroduces both the ANSI SQL synatax

More information

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects ASSIGNMENT NO 2 Title: Design and Develop SQL DDL statements which demonstrate the use of SQL objects such as Table, View, Index, Sequence, Synonym Objectives: To understand and demonstrate DDL statements

More information

Databases (MariaDB/MySQL) CS401, Fall 2015

Databases (MariaDB/MySQL) CS401, Fall 2015 Databases (MariaDB/MySQL) CS401, Fall 2015 Database Basics Relational Database Method of structuring data as tables associated to each other by shared attributes. Tables (kind of like a Java class) have

More information

Chapter 9: MySQL for Server-Side Data Storage

Chapter 9: MySQL for Server-Side Data Storage Chapter 9: MySQL for Server-Side Data Storage General Notes on the Slides for This Chapter In many slides you will see webbook as a database name. That was the orginal name of our database. For this second

More information

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL HOW TO CREATE AND MAINTAIN DATABASES AND TABLES By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate

More information

SELECT WHERE JOIN. DBMS - Select. Robert Lowe. Division of Mathematics and Computer Science Maryville College. February 16, 2016

SELECT WHERE JOIN. DBMS - Select. Robert Lowe. Division of Mathematics and Computer Science Maryville College. February 16, 2016 Division of Mathematics and Computer Science Maryville College February 16, 2016 Outline 1 2 3 Syntax [ALL DISTINCT DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]

More information

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates Chapter 13 : Informatics Practices Class XI ( As per CBSE Board) SQL Commands New Syllabus 2018-19 SQL SQL is an acronym of Structured Query Language.It is a standard language developed and used for accessing

More information

SQL functions fit into two broad categories: Data definition language Data manipulation language

SQL functions fit into two broad categories: Data definition language Data manipulation language Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition Chapter 7 Beginning Structured Query Language (SQL) MDM NUR RAZIA BINTI MOHD SURADI 019-3932846 razia@unisel.edu.my

More information

CS143: Relational Model

CS143: Relational Model CS143: Relational Model Book Chapters (4th) Chapters 1.3-5, 3.1, 4.11 (5th) Chapters 1.3-7, 2.1, 3.1-2, 4.1 (6th) Chapters 1.3-6, 2.105, 3.1-2, 4.5 Things to Learn Data model Relational model Database

More information

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1 SQL Fundamentals Chapter 3 Class 03: SQL Fundamentals 1 Class 03: SQL Fundamentals 2 SQL SQL (Structured Query Language): A language that is used in relational databases to build and query tables. Earlier

More information

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Chapter # 7 Introduction to Structured Query Language (SQL) Part I Chapter # 7 Introduction to Structured Query Language (SQL) Part I Introduction to SQL SQL functions fit into two broad categories: Data definition language Data manipulation language Basic command set

More information

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object.

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object. 1 WHAT IS A DATABASE? A database is any organized collection of data that fulfills some purpose. As weather researchers, you will often have to access and evaluate large amounts of weather data, and this

More information

user specifies what is wanted, not how to find it

user specifies what is wanted, not how to find it SQL stands for Structured Query Language sometimes pronounced sequel a very-high-level (declarative) language user specifies what is wanted, not how to find it number of standards original ANSI SQL updated

More information

Table Joins and Indexes in SQL

Table Joins and Indexes in SQL Table Joins and Indexes in SQL Based on CBSE Curriculum Class -11 By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region Neha Tyagi, PGT CS II Shift Jaipur Introduction Sometimes we need an information

More information

Lab # 4. Data Definition Language (DDL)

Lab # 4. Data Definition Language (DDL) Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM 4113: Lab # 4 Data Definition Language (DDL) Eng. Haneen El-Masry November, 2014 2 Objective To be familiar with

More information

Standard Query Language: Current standard Logical Schema Design: Schema Definition with SQL (DDL)

Standard Query Language: Current standard Logical Schema Design: Schema Definition with SQL (DDL) Standard Query Language: Current standard Logical Schema Design: Schema Definition with SQL (DDL) 1999: standard (ANSI SQL-3) about 2200 pages as full standard SQL history and standards SQL type system

More information

Oracle SQL Developer. Supplementary Information for MySQL Migrations Release 2.1 E

Oracle SQL Developer. Supplementary Information for MySQL Migrations Release 2.1 E Oracle SQL Developer Supplementary Information for MySQL Migrations Release 2.1 E15225-01 December 2009 This document contains information for migrating from MySQL to Oracle. It supplements the information

More information

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table Announcements (September 14) 2 SQL: Part I Books should have arrived by now Homework #1 due next Tuesday Project milestone #1 due in 4 weeks CPS 116 Introduction to Database Systems SQL 3 Creating and

More information

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity] What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity] MySQL is a database. A database defines a structure for storing information. In a database, there are tables. Just

More information

CIS 363 MySQL. Chapter 4 MySQL Connectors Chapter 5 Data Types Chapter 6 Identifiers

CIS 363 MySQL. Chapter 4 MySQL Connectors Chapter 5 Data Types Chapter 6 Identifiers CIS 363 MySQL Chapter 4 MySQL Connectors Chapter 5 Data Types Chapter 6 Identifiers Ch. 4 MySQL Connectors *** Chapter 3 MySQL Query Browser is Not covered on MySQL certification exam. For details regarding

More information

SQL Structured Query Language Introduction

SQL Structured Query Language Introduction SQL Structured Query Language Introduction Rifat Shahriyar Dept of CSE, BUET Tables In relational database systems data are represented using tables (relations). A query issued against the database also

More information

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS Questions & Answers- DBMS https://career.guru99.com/top-50-database-interview-questions/ 1) Define Database. A prearranged collection of figures known as data is called database. 2) What is DBMS? Database

More information

SQL Data Definition Language: Create and Change the Database Ray Lockwood

SQL Data Definition Language: Create and Change the Database Ray Lockwood Introductory SQL SQL Data Definition Language: Create and Change the Database Pg 1 SQL Data Definition Language: Create and Change the Database Ray Lockwood Points: DDL statements create and alter the

More information

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one } The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of an ORDER BY clause is SELECT columnname1, columnname2, FROM tablename ORDER

More information

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014 Selections Lecture 4 Sections 4.2-4.3 Robb T. Koether Hampden-Sydney College Wed, Jan 22, 2014 Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 1 / 38 1 Datatypes 2 Constraints 3 Storage

More information

3.2.4 Enforcing constraints

3.2.4 Enforcing constraints 3.2.4 Enforcing constraints Constraints SQL definition of the schema Up to now: primary Key, foreign key, NOT NULL Different kinds of integrity constraints value constraints on attributes cardinalities

More information

MySQL Workshop. Scott D. Anderson

MySQL Workshop. Scott D. Anderson MySQL Workshop Scott D. Anderson Workshop Plan Part 1: Simple Queries Part 2: Creating a database: creating a table inserting, updating and deleting data handling NULL values datatypes Part 3: Joining

More information

CIS 363 MySQL. Chapter 10 SQL Expressions Chapter 11 Updating Data

CIS 363 MySQL. Chapter 10 SQL Expressions Chapter 11 Updating Data CIS 363 MySQL Chapter 10 SQL Expressions Chapter 11 Updating Data Expressions are a common element of SQL statements, and they occur in many contexts. Terms of expressions consist of Constants (literal

More information

CSED421 Database Systems Lab. Index

CSED421 Database Systems Lab. Index CSED421 Database Systems Lab Index Index of Index What is an index? When to Create an Index or Not? Index Syntax UNIQUE Index / Indexing Prefixes / Multiple-column index Confirming indexes Index types

More information

Intro to Database Commands

Intro to Database Commands Intro to Database Commands SQL (Structured Query Language) Allows you to create and delete tables Four basic commands select insert delete update You can use clauses to narrow/format your result sets or

More information

Database Management Systems,

Database Management Systems, Database Management Systems SQL Query Language (1) 1 Topics Introduction SQL History Domain Definition Elementary Domains User-defined Domains Creating Tables Constraint Definition INSERT Query SELECT

More information

Chapter-14 SQL COMMANDS

Chapter-14 SQL COMMANDS Chapter-14 SQL COMMANDS What is SQL? Structured Query Language and it helps to make practice on SQL commands which provides immediate results. SQL is Structured Query Language, which is a computer language

More information

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2 Outline SQL Data Definition and Data Types Specifying Constraints in SQL Basic Retrieval Queries in SQL INSERT, DELETE, and UPDATE Statements in SQL Additional Features of SQL Textbook Chapter 6 CSIE30600/CSIEB0290

More information

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS)

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS) Lecture 8 SQL (I) Money are kept by boxes buried in the ground in the backyard. Money are kept in the bank 1 Source: system analysis and design methods, by Jeffrey L Whitten et al., 2 McGraw-Hill/Irwin,

More information

Mysql Information Schema Update Time Null >>>CLICK HERE<<< doctrine:schema:update --dump-sql ALTER TABLE categorie

Mysql Information Schema Update Time Null >>>CLICK HERE<<< doctrine:schema:update --dump-sql ALTER TABLE categorie Mysql Information Schema Update Time Null I want to update a MySQL database schema (with MySQL code) but I am unfortunately not sure 'name' VARCHAR(64) NOT NULL 'password' VARCHAR(64) NOT NULL fieldname

More information

20 March 24 March. Relational Databases. Reading SQL. Perl DBI. SQL Tutorial or SQL Course MySQL tutorial

20 March 24 March. Relational Databases. Reading SQL. Perl DBI. SQL Tutorial or SQL Course MySQL tutorial 20 March 24 March Relational Databases Reading SQL SQL Tutorial or SQL Course MySQL tutorial Perl DBI Database programming with Perl Appendix A and B Ch 1-5 Biol 59500-033 - Practical Biocomputing 1 Database

More information

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement Chapter 4 Basic SQL Basic SQL SQL language Considered one of the major reasons for the commercial success of relational databases SQL Structured Query Language Statements for data definitions, queries,

More information

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4 SQL OVERVIEW CS121: Relational Databases Fall 2017 Lecture 4 SQL 2 SQL = Structured Query Language Original language was SEQUEL IBM s System R project (early 1970 s) Structured English Query Language Caught

More information

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL) Chapter 6 Introduction to Structured Query Language (SQL) Introduction Structured Query Language (SQL) is a data sublanguage that has constructs for defining and processing a database It can be Used stand-alone

More information

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture COGS 121 HCI Programming Studio Week 03 - Tech Lecture Housekeeping Assignment #1 extended to Monday night 11:59pm Assignment #2 to be released on Tuesday during lecture Database Management Systems and

More information

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013 CSE 530A ER Model to Relational Schema Washington University Fall 2013 Relational Model A relational database consists of a group of relations (a.k.a., tables) A relation (table) is a set of tuples (rows)

More information

SQL (and MySQL) Useful things I have learnt, borrowed and stolen

SQL (and MySQL) Useful things I have learnt, borrowed and stolen SQL (and MySQL) Useful things I have learnt, borrowed and stolen MySQL truncates data MySQL truncates data CREATE TABLE pets ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, type CHAR(3) NOT NULL, PRIMARY KEY

More information

Exploring Popular SQL Implementations. Query the database to retrieve the data stored therein.

Exploring Popular SQL Implementations. Query the database to retrieve the data stored therein. Exploring Popular SQL Implementations Any tour into the realm of writing SQL functions should begin with a solid foundation of the basic principles of SQL. In this chapter, we will be discussing the ins

More information

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford) DS 1300 - Introduction to SQL Part 1 Single-Table Queries By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford) Overview 1. SQL introduction & schema definitions 2. Basic single-table

More information

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language IT360: Applied Database Systems SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) 1 Goals SQL: Data Definition Language CREATE ALTER DROP SQL: Data Manipulation Language INSERT

More information

Short List of MySQL Commands

Short List of MySQL Commands Short List of MySQL Commands Conventions used here: MySQL key words are shown in CAPS User-specified names are in small letters Optional items are enclosed in square brackets [ ] Items in parentheses must

More information

Physical Design of Relational Databases

Physical Design of Relational Databases Physical Design of Relational Databases Chapter 8 Class 06: Physical Design of Relational Databases 1 Physical Database Design After completion of logical database design, the next phase is the design

More information

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE 9/27/16 DATABASE SCHEMAS IN SQL SQL DATA DEFINITION LANGUAGE SQL is primarily a query language, for getting information from a database. SFWR ENG 3DB3 FALL 2016 But SQL also includes a data-definition

More information