Erste Schritte mit mysql. Der Umgang mit einer relationalen Datenbank



Similar documents
A table is a collection of related data entries and it consists of columns and rows.

A Brief Introduction to MySQL

!"# $ %& '( ! %& $ ' &)* + ! * $, $ (, ( '! -,) (# *&23. mysql> select * from from clienti;

B.1 Database Design and Definition

Database Administration with MySQL

SQL. Short introduction

SQL Server An Overview

MS ACCESS DATABASE DATA TYPES

4 Logical Design : RDM Schema Definition with SQL / DDL

2/3/04 Doc 7 SQL Part 1 slide # 1

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

How To Create A Table In Sql (Ahem)

J2EE-Application Server

2. Installieren des MySQL Workbench (Version ) 3. Unter Database > Manage Connection folgende Werte eintragen

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

MySQL 5.1 INTRODUCTION 5.2 TUTORIAL

New Features in MySQL 5.0, 5.1, and Beyond

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

MYSQL DATABASE ACCESS WITH PHP

CSC 443 Data Base Management Systems. Basic SQL

Using SQL Server Management Studio

Introduction to SQL and database objects

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Introduction to Microsoft Jet SQL

sqlite driver manual

Working with DB2 UDB objects

SQL 2014 CTP1. Hekaton & CSI Version 2 unter der Lupe. Sascha Götz Karlsruhe, 03. Dezember 2013

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Data Warehousing Metadata Management

Data Warehousing Metadata Management

Database Design and Programming

Database Migration from MySQL to RDM Server

Tutorial on Relational Database Design

Financial Data Access with SQL, Excel & VBA

Developing Web Applications for Microsoft SQL Server Databases - What you need to know

Introduction to SQL for Data Scientists

A basic create statement for a simple student table would look like the following.

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

Linas Virbalas Continuent, Inc.

Database: SQL, MySQL

MySQL for Beginners Ed 3

Technische Alternative elektronische Steuerungsgerätegesellschaft mbh. A-3872 Amaliendorf, Langestr. 124 Tel +43 (0)

3.GETTING STARTED WITH ORACLE8i

UQC103S1 UFCE Systems Development. uqc103s/ufce PHP-mySQL 1

Chapter 30 Exporting Inventory Management System Data

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

SQL Server Database Coding Standards and Guidelines

Websense SQL Queries. David Buyer June 2009

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Best Practices in SQL Programming. Madhivanan

Oracle Database 10g Express

DDL is short name of Data Definition Language, which deals with database schemas and descriptions, of how the data should reside in the database.

SQL Injection. Blossom Hands-on exercises for computer forensics and security

How-To: MySQL as a linked server in MS SQL Server

SQL 2: GETTING INFORMATION INTO A DATABASE. MIS2502 Data Analytics

Choosing a Data Model for Your Database

QAS DEBUG - User und Computer

Creating Database Tables in Microsoft SQL Server

Intro to Databases. ACM Webmonkeys 2011

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

IT2304: Database Systems 1 (DBS 1)

6. SQL/XML. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. XML Databases 6. SQL/XML. Creating XML documents from a database

Apache Sqoop. A Data Transfer Tool for Hadoop

Information Systems SQL. Nikolaj Popov

Server side scripting and databases

PassMark Software BurnInTest Management Console. Quick start guide

A brief MySQL tutorial. CSE 134A: Web Service Design and Programming Fall /28/2001

Database Query 1: SQL Basics

DIPLOMA IN WEBDEVELOPMENT

PUBLIC. How to Use in SAP Business One. Solutions from SAP. SAP Business One 2005 A SP01

Die Versant-DB ist ein wesentlicher Bestandteil von CADISON.

MySQL Cluster: HA als StorageEngine

XML Databases 6. SQL/XML

Full Text Search with Sphinx

Redundant Storage Cluster

Designing for Dynamic Content

Package sjdbc. R topics documented: February 20, 2015

14 Triggers / Embedded SQL

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Facebook Twitter YouTube Google Plus Website

EMC Greenplum. Big Data meets Big Integration. Wolfgang Disselhoff Sr. Technology Architect, Greenplum. André Münger Sr. Account Manager, Greenplum

Fundamentals of Database Design

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Abstract. For notes detailing the changes in each release, see the MySQL for Excel Release Notes. For legal information, see the Legal Notices.

ODBC Driver Version 4 Manual

Serious Threat. Targets for Attack. Characterization of Attack. SQL Injection 4/9/2010 COMP On August 17, 2009, the United States Justice

Conventional Files versus the Database. Files versus Database. Pros and Cons of Conventional Files. Pros and Cons of Databases. Fields (continued)

Database access for illiterate programmers. K.B.Swiatlowski

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

Partitioning under the hood in MySQL 5.5

Transcription:

Erste Schritte mit mysql Der Umgang mit einer relationalen Datenbank

Relationale Datenbanken Prinzip: Daten sind in Tabellen gespeichert Tabellen können verknüpft sein alter Name: RDBMS - Relational Database Management System erlaubt das geordnete Speichern und Abrufen großer Datenmengen dazu wird die Sprache SQL (Structured Query Language) benutzt

Structured Query Language SQL ist eine einfache Sprache Beispiel: mysql starten: mysql -uusername -p Datenbank erzeugen: mysql> CREATE DATABASE meinedb; Query OK, 1 row affected (0.01 sec)

Verfügbare Datentypen INT[(SIZE)] [UNSIGNED] [ZEROFILL] A simple integer between -2147483648 and +2147483647 or, if the UNSIGNED attribute is provided, between zero and 4294967295. The ZEROFILL attribute indicates that the number should be prefixed with zeros until the number is SIZE digits in length. VARCHAR[(SIZE)] [BINARY] A variable-length string that is a maximum of SIZE characters in length (where SIZE cannot exceed 255). Unless the BINARY attribute is provided, this data type is considered case-insensitive and obviously cannot hold binary data. TEXT A case-sensitive string that is a maximum of 65,535 characters in length. DATETIME A date and time ranging from 1000-01-01 00:00:00 to 9999-12-31 23:59:59 (dates are in YYYY-MM-DD HH: MM:SS format). DATE Similar to the DATETIME data type, except without the time in YYYY-MM-DD format.

Tabellen erzeugen festlegen, mit welcher Datenbank gearbeitet werden soll: mysql> USE meinedb; Database Changed 2 Tabellen anlegen: mysql> CREATE TABLE books( book_id INT AUTO_INCREMENT PRIMARY KEY, author_id INT, title VARCHAR(255), pub_date DATE); Query OK, 0 rows affected (0.02 sec) mysql> CREATE TABLE authors( author_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), state VARCHAR(2)); Query OK, 0 rows affected (0.02 sec)

Tabellenstruktur ansehen mysql> DESC books; Field Type Collation Null Key Default Extra book_id int(11) binary YES PRI NULL auto_increment author_id int(11) binary YES NULL title varchar(255) latin1_swedish_ci YES NULL pub_date date latin1_swedish_ci YES NULL 4 rows in set (0.00 sec)

Daten einfügen mysql> INSERT INTO books VALUES(1, 1, "PHP Unleashed", "2003-11-01"); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO authors VALUES(1, "John Coggeshall","MI"); Query OK, 1 row affected (0.01 sec)

Alle Tabellen einer DB anzeigen mysql> SHOW TABLES; +-----------------------------+ Tables in fundamentals +-----------------------------+ books authors +-----------------------------+ 2 rows in set (0.03 sec)

Alle Daten auslesen mysql> select * from books; +---------+-----------+------------------+------------+ book_id author_id title pub_date +---------+-----------+------------------+------------+ 1 1 PHP Unleashed 2003-11-01 2 1 PHP4 Programming 2002-10-01 3 2 Cool Stuff 2002-03-23 4 3 Another Book 2001-02-01 +---------+-----------+------------------+------------+ 4 rows in set (0.00 sec)

Bestimmte Spalten auslesen mysql> SELECT title FROM books; +------------------+ title +------------------+ PHP Unleashed PHP4 Programming Cool Stuff Another Book +------------------+ 4 rows in set (0.00 sec) mysql> SELECT title, pub_date FROM books; +------------------+------------+ title pub_date +------------------+------------+ PHP Unleashed 2003-11-01 PHP4 Programming 2002-10-01 Cool Stuff 2002-03-23 Another Book 2001-02-01 +------------------+------------+ 4 rows in set (0.00 sec)

Bestimmte Daten in bestimmten Spalten auswählen mysql> SELECT * FROM books WHERE author_id=1; +---------+-----------+------------------+------------+ book_id author_id title pub_date +---------+-----------+------------------+------------+ 1 1 PHP Unleashed 2003-11-01 2 1 PHP4 Programming 2002-10-01 +---------+-----------+------------------+------------+ 2 rows in set (0.00 sec)

Selektionskriterien Expression A = B A!= B A <= B A > B A < B A > B A <=> B A IS NULL Evaluation True if A equals B True if A does not equal B True if A is less than or equal to B True if A is greater than or equal to B True if A is less than B True if A is greater than B True if A is equal to B (NULL Safe) True if A is NULL A IS NOT NULL True if A is not NULL A BETWEEN M AND N True if A is between values M and N A NOT BETWEEN M AND N True if A is not between values M and N A IN(value, value2,...) True if A is one of the listed values A NOT IN (value, value2,...) True if A is not one of the listed values

Suchen mit Wildcards mysql> SELECT * FROM books WHERE title LIKE "%PHP%"; +---------+-----------+------------------+------------+ book_id author_id title pub_date +---------+-----------+------------------+------------+ 1 1 PHP Unleashed 2003-11-01 2 1 PHP4 Programming 2002-10-01 +---------+-----------+------------------+------------+ 2 rows in set (0.04 sec)

Einen Datensatz aktualisieren mysql> UPDATE authors SET state='fl' WHERE author_id=1; Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 allgemeine Syntax: UPDATE <table> SET <column>=<value>, <column>=<value>,... WHERE <conditions>

Datensatz löschen mysql> DELETE FROM authors WHERE author_id=1; Query OK, 1 row affected (0.06 sec) allgemeine Syntax: DELETE FROM <table> WHERE <conditions>

Tabelle erweitern mysql> ALTER TABLE books ADD isbn VARCHAR(25) AFTER pub_date; Query OK, 4 rows affected (0.10 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> DESC books; Field Type Collation Null Key Default Extra book_id int(11) binary YES PRI NULL auto_increment author_id int(11) binary YES NULL title varchar(255) latin1_swedish_ci YES NULL pub_date date latin1_swedish_ci YES NULL isbn varchar(25) latin1_swedish_ci YES NULL 5 rows in set (0.00 sec)

Tabelle verkleinern mysql> ALTER TABLE books DROP isbn; Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 allgemeine Syntax des ALTER-Befehls: ALTER TABLE <table> [ADD DROP] <column definition> [FIRST AFTER <column>]

DBs und Tabellen löschen DROP [TABLE DATABASE] <table or database name>

Datensätze sortieren mysql> SELECT name FROM authors ORDER BY name; +-----------------+ name +-----------------+ Jennifer Author Joe Coolguy John Coggeshall +-----------------+ 3 rows in set (0.00 sec) mysql> SELECT name FROM authors ORDER BY name DESC; +-----------------+ name +-----------------+ John Coggeshall Joe Coolguy Jennifer Author +-----------------+ 3 rows in set (0.00 sec) mysql> SELECT name, state FROM authors ORDER BY name, state; +-----------------+-------+ name state +-----------------+-------+ Jennifer Author KS Joe Coolguy AZ John Coggeshall MI +-----------------+-------+ 3 rows in set (0.00 sec)

Datensätze aus mehreren Tabellen zusammenstellen mysql> SELECT books.title, authors.name FROM books, authors WHERE books.author_id=authors.author_id; +------------------+-----------------+ title name +------------------+-----------------+ PHP Unleashed John Coggeshall PHP4 Programming John Coggeshall Cool Stuff Joe Coolguy Another Book Jennifer Author +------------------+-----------------+ Joins 4 rows in set (0.09 sec) mysql> DESC books; Field Type Collation Null Key Default Extra book_id int(11) binary YES PRI NULL auto_increment author_id int(11) binary YES NULL title varchar(255) latin1_swedish_ci YES NULL pub_date date latin1_swedish_ci YES NULL 4 rows in set (0.00 sec) mysql> DESC authors; Field Type Collation Null Key Default Extra author_id int(11) binary YES PRI NULL auto_increment name varchar(255) latin1_swedish_ci YES NULL state char(2) latin1_swedish_ci YES NULL 3 rows in set (0.01 sec)

Anzahl Datensätze in einer Tabelle zählen SQL-Funktion COUNT: mysql> SELECT COUNT(title) FROM books; +--------------+ COUNT(title) +--------------+ 4 +--------------+ 1 row in set (0.00 sec) mysql> SELECT authors.name, COUNT(books.title) AS books FROM authors,books WHERE books.author_id = authors.author_id GROUP BY name; +-----------------+-------+ name books +-----------------+-------+ Jennifer Author 1 Joe Coolguy 1 John Coggeshall 2 +-----------------+-------+ 3 rows in set (0.01 sec)