Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Instructor: Bei Kang.

Similar presentations


Presentation on theme: "Database Instructor: Bei Kang."— Presentation transcript:

1 Database Instructor: Bei Kang

2 MySQL https://dev.mysql.com/downloads/installer/
MySQL is released under an open-source license. So you have nothing to pay to use it. MySQL is a very powerful program in its own right. It handles a large subset of the functionality of the most expensive and powerful database packages. MySQL uses a standard form of the well-known SQL data language. MySQL works on many operating systems and with many languages including PHP, PERL, C, C++, JAVA, etc.

3 Database Introductions
Database − A database is a collection of tables, with related data. Table − A table is a matrix with data. A table in a database looks like a simple spreadsheet. Column − One column (data element) contains data of one and the same kind, for example the column postcode. Row − A row (= tuple, entry or record) is a group of related data, for example the data of one subscription.

4 Database Example Table 2 Table 3 Student Info Database Table 1

5 Keys Primary Key − A primary key is unique. A key value can not occur twice in one table. With a key, you can only find one row. Foreign Key − A foreign key is the linking pin between two tables. Compound Key − A compound key (composite key) is a key that consists of multiple columns, because one column is not sufficiently unique.

6 Data Type Data type Description CHAR(size)
Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. Can store up to 255 characters VARCHAR(size) Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis. Can store up to 255 characters. Note: If you put a greater value than 255 it will be converted to a TEXT type TINYTEXT Holds a string with a maximum length of 255 characters TEXT Holds a string with a maximum length of 65,535 characters BLOB For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data

7 Data Type Data type Description TINYINT(size)
-128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits may be specified in parenthesis SMALLINT(size) to normal. 0 to UNSIGNED*. The maximum number of digits may be specified in parenthesis MEDIUMINT(size) to normal. 0 to UNSIGNED*. The maximum number of digits may be specified in parenthesis INT(size) to normal. 0 to UNSIGNED*. The maximum number of digits may be specified in parenthesis BIGINT(size) to normal. 0 to UNSIGNED*. The maximum number of digits may be specified in parenthesis FLOAT(size,d) A small number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter DOUBLE(size,d) A large number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter DECIMAL(size,d) A DOUBLE stored as a string , allowing for a fixed decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter

8 Data Type Data type Description DATE()
A date. Format: YYYY-MM-DDNote: The supported range is from ' ' to ' ' DATETIME() *A date and time combination. Format: YYYY-MM-DD HH:MI:SSNote: The supported range is from ' :00:00' to ' :59:59' TIMESTAMP() *A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch (' :00:00' UTC). Format: YYYY-MM-DD HH:MI:SSNote: The supported range is from ' :00:01' UTC to ' :14:07' UTC TIME() A time. Format: HH:MI:SSNote: The supported range is from '-838:59:59' to '838:59:59' YEAR() A year in two-digit or four-digit format. Note: Values allowed in four-digit format: 1901 to Values allowed in two-digit format: 70 to 69, representing years from 1970 to 2069

9 Create a database in MySQL
create database <db_name> ; use <db_name>

10 Create a Table create table students (
student_id int unsigned not null, name varchar(20) not null, grade char(8) not null, department char(20) not null, primary key (student_id) );

11 Insert record into table
insert into students values('100001', "Student A", "3rd year", "Marketing"); insert into students values('100021', "Student B", "2nd year", "Accounting"); insert into students values('100002', "Student C", "3rd year", "Finance"); insert into students values('100500', "Student D", "4th year", "Marketing"); Or you may want to use a command to do a batch loading from the cvs file: LOAD DATA LOCAL INFILE “file path" INTO TABLE jobs COLUMNS TERMINATED BY ',' IGNORE 1 LINES;

12 Database query SELECT field1, field2,...fieldN
FROM table_name1, table_name2... [WHERE Clause] [ORDER BY fieldx] [LIMIT N] In where clause, you can specify ‘=‘, ‘>’, ‘<‘, ‘>=‘, ‘<=‘, for the conditions you want to compare. Example: SELECT student_id, student_name FROM students WHERE student_id > ;

13 Select data from one table into another
INSERT INTO Syntax Copy all columns into a new table: INSERT INTO <new table name> SELECT field1, field2… FROM <old table name> WHERE some conditions;

14 Database remove DELETE FROM table_name [WHERE Clause]


Download ppt "Database Instructor: Bei Kang."

Similar presentations


Ads by Google