Sie sind auf Seite 1von 9

Setting up account starting, terminating, and Writing your ownSQL program y Mysql database system uses a client-server architecture.

. yServer is the program that manipulates database. yClient program dont do that directly, instead communicate your intent to the server by means of statement written in SQL. yClient-locally , Server-anywhere In Linux operating system: [csk@localhost ~]$ su Password: [root@localhost csk]# /etc/init.d/mysqld start Starting mysqld: [ OK ] [root@localhost csk]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.1.52 Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> ---------------------------------------------------------------------Setting up account Starting, terminating and writing your ownSQL programs consist of following: y y y y y y y y y Setting up mysql user account Creating database, table, fields, and sample records Starting and terminating mysql Specifying connection parameters Issuing sql statements Repeating and editing Queries Making long output lines more readable Using mysql as a calculator ownSQL programs

1) Setting up mysql user account: Use GRANT statement to setup the mysql user account. Mysql>grant all privileges on *.* to root@localhost; Mysql>grant all on book.* to dbuser @ localhost identified by dbpass; Query OK, 0 rows affected (0.09 sec)

2) Creating database, table, fields, and sample records: mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | library | | mysql | | student | | test | +--------------------+ 5 rows in set (2.21 sec) mysql> CREATE DATABASE book; Query OK, 1 row affected (2.19 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | book | | library | | mysql | | student | | test | +--------------------+ 6 rows in set (0.00 sec) mysql> create table limbs(thing varchar(25), legs int, arms int);

ERROR 1046 (3D000): No database selected mysql> use book; Database changed mysql> create table limbs(thing varchar(25), legs int, arms int); Query OK, 0 rows affected (0.13 sec) mysql> show tables; +----------------+ | Tables_in_book | +----------------+ | limbs | +----------------+ 1 row in set (0.00 sec) mysql> insert into limbs(things, legs, arms) values ('human',2,2); ERROR 1054 (42S22): Unknown column 'things' in 'field list' mysql> insert into limbs(thing, legs, arms) values ('human',2,2); Query OK, 1 row affected (0.00 sec) mysql> insert into limbs(thing, legs, arms) values ('fish',0,0); Query OK, 1 row affected (0.00 sec) mysql> insert into limbs(thing, legs, arms) values ('space alien',null,null); Query OK, 1 row affected (0.00 sec) mysql> insert into limbs(thing, legs, arms) values ('octopus',0,8); Query OK, 1 row affected (0.00 sec) mysql> select * from limbs; +-------------+------+------+ | thing | legs | arms | +-------------+------+------+ | human | 2| 2| | fish | 0| 0| | space alien | NULL | NULL | | octopus | 0 | 8 | +-------------+------+------+ 4 rows in set (0.00 sec) 3) Starting and terminating mysql: Starting: [csk@localhost ~]$ su Password: [root@localhost csk]# /etc/init.d/mysqld start Starting mysqld: [ OK ]

[root@localhost csk]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.1.52 Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> \h For information about MySQL products and services, visit: http://www.mysql.com/ For developer information, including the MySQL Reference Manual, visit: http://dev.mysql.com/ To buy MySQL Enterprise support, training, or other products, visit: https://shop.mysql.com/ List of all MySQL commands: Note that all text commands must be first on line and end with ';' ? (\?) Synonym for `help'. clear (\c) Clear the current input statement. connect (\r) Reconnect to the server. Optional arguments are db and host. delimiter (\d) Set statement delimiter. edit (\e) Edit command with $EDITOR. ego (\G) Send command to mysql server, display result vertically. exit (\q) Exit mysql. Same as quit. go (\g) Send command to mysql server. help (\h) Display this help. nopager (\n) Disable pager, print to stdout. notee (\t) Don't write into outfile. pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. print (\p) Print current command. prompt (\R) Change your mysql prompt. quit (\q) Quit mysql. rehash (\#) Rebuild completion hash. source (\.) Execute an SQL script file. Takes a file name as an argument. status (\s) Get status information from the server. system (\!) Execute a system shell command. tee (\T) Set outfile [to_outfile]. Append everything into given outfile. use (\u) Use another database. Takes database name as argument.

charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. warnings (\W) Show warnings after every statement. nowarning (\w) Don't show warnings after every statement. For server side help, type 'help contents' mysql> \s mysql Ver 14.14 Distrib 5.1.52, for redhat-linux-gnu (i386) using readline 5.1 Connection id: Current database: Current user: SSL: Current pager: Using outfile: Using delimiter: ; Server version: Protocol version: 10 Connection: Server characterset: Db characterset: Client characterset: Conn. characterset: UNIX socket: Uptime: 15 csk@localhost Not in use stdout '' 5.1.52 Source distribution Localhost via UNIX socket latin1 latin1 latin1 latin1 /var/lib/mysql/mysql.sock 27 min 57 sec

Threads: 1 Questions: 42 Slow queries: 0 Opens: 17 Flush tables: 1 Open tables: 9 Queries per second avg: 0.25 Terminating: mysql> exit Bye mysql> quit Bye mysql> Ctrl-C -- exit! Aborted [root@localhost csk]#

4) Specifying connection parameters: In unix shell: % mysql h localhost p u root Enter password: % mysql <welcome message> <connection id> <version> Mysql> In linux; [root@localhost csk]# /etc/init.d/mysqld start Starting mysqld: [ OK ] [root@localhost csk]# mysql <welcome message> <connection id> <version> <copyright information> Mysql>

Parameter type Hostname Username Password 5) Issuing sql statements: mysql> select now(); +---------------------+ | now() | +---------------------+ | 2011-02-11 00:13:01 | +---------------------+ 1 row in set (0.03 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema |

Option syntax forms -h hostname -u username -p password

Default value localhost Your login name None

| book | | library | | mysql | | student | | test | +--------------------+ 6 rows in set (0.00 sec) mysql> select database(); +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> use book; Database changed mysql> select database(); +------------+ | database() | +------------+ | book | +------------+ 1 row in set (0.00 sec) 6) Repeating and editing Queries: mysqls built-in editor (statement entered contain error, and you want to fix it without typing whole things again) Editing Key Up Arrow Down Arrow Left Arrow Right Arrow Ctrl-A Ctrl-E Backspace Ctrl-D Effect of Key Scroll up through statement history Scroll down through statement history Move left within line Move right within line Move to beginning of line Move to end of line Delete previous character Delete character under cursor

7) Making long output lines more readable: mysql> Show full columns from limbs;

+-------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +-------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+ | thing | varchar(25) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references | | | legs | int(11) | NULL | YES | | NULL | | select,insert,update,references | | | arms | int(11) | NULL | YES | | NULL | | select,insert,update,references | | +-------+-------------+-------------------+------+-----+---------+-------+--3 rows in set (0.03 sec) mysql> show full columns from limbs \G *************************** 1. row *************************** Field: thing Type: varchar(25) Collation: latin1_swedish_ci Null: YES Key: Default: NULL Extra: Privileges: select,insert,update,references Comment: *************************** 2. row *************************** Field: legs Type: int(11) Collation: NULL Null: YES Key: Default: NULL Extra: Privileges: select,insert,update,references Comment: *************************** 3. row *************************** Field: arms Type: int(11) Collation: NULL Null: YES Key: Default: NULL Extra: Privileges: select,insert,update,references Comment: 3 rows in set (0.01 sec)

8) Using mysql as a calculator: mysql> select (17 + 23) / sqrt(64); +----------------------+ | (17 + 23) / sqrt(64) | +----------------------+ | 5| +----------------------+ 1 row in set (2.16 sec) mysql> select 'ABC' = 'abc'; +---------------+ | 'ABC' = 'abc' | +---------------+ | 1| +---------------+ 1 row in set (2.16 sec) mysql> select 'abc' = 'abcd; +----------------+ | 'abc' = 'abcd' | +----------------+ | 0| +----------------+ 1 row in set (0.00 sec) 9) ownSQL programs: y y y y y y Updating Modifying Count Delete Truncate Drop

Das könnte Ihnen auch gefallen