MySQL数据库(第一课)
广告投放★自助友情CMS落伍广告联盟晒乐广告联盟脉动广告联盟品味广告联盟
广告位可自定样式联系QQ:38355018个文字广告月20元广告联系QQ:3835501广告位可自定样式
8个文字广告月20元黄金广告位每月20元广告位可自定样式联系QQ:3835501广告位可自定样式
左旋肉碱、全国包邮
买二送一、无效退款

文章浏览→编程相关Mysql→MySQL数据库(第一课)

MySQL数据库(第一课)
MySQL数据库(第一课)

操作命令:

//查看数据库

show databases;
#使用数据库  
usetest;   
#查看数据库中的表   
show tables;  
#创建一个表  
create tablebook_db(   
    id int notnull auto_increment primarykey,   
    BookNamevarchar(20) notnull,   

    RegTimetimestamp,   
    Amountint(8) default0,   
    pricedecimal(12,0)   
); 

#查看表的结构   
show create book_dbG   
desc book_db;  
#查看帮助   
\h;   
help;   
\?;    
#通过文件创建数据库  
#在D盘建一个文件:book.sql   
#在文件中写入以下内容,然后保存   
drop database if existsbook;   
create databasebook;   
usebook;   
drop table if existsbook_db;   
create tablebook_db(   
    Id int not null auto_increment primarykey,   
    BookName varchar(20) notnull,   
    RegTime timestamp,   
    Amount int(8) default0,   
    Pricedecimal(12,0)   
);   
#再回到DOS窗口,输入\.或者source   
source d:\book.sql; 

\. d:\book.sql;  
#然后回车,就创建了一个数据库和表。    
showdatabases;   
usebook;   
showtables;   
desc book_db;

具体操作步骤: 

//查看数据库
mysql> show databases;
+-----------------------------------+
|Database                         |
+-----------------------------------+
|information_schema               |
|bank                             |
|bank1                            |
|db_jxc                           |
|db_person                        |
|equipment                        |
|example                          |
| hibernate_struts_training_itemmgr |
|javatest                         |
|logs                             |
|mysql                            |
|pagedb                           |
|tb_user                          |
|test                             |
|wh2_mysql                        |
|ygwy                             |
+-----------------------------------+
16 rows in set (0.27 sec)
//打开数据库
mysql> use test;
Database changed
//查看数据库中的表
mysql> show tables;
+--------------------+
|Tables_in_test    |
+--------------------+
|area              |
|building          |
|cb_view           |
|chaobiao          |
|detailstatement   |
|dinnermenu        |
|dinnertable       |
|discount          |
|dish              |
|dishcategory      |
|dwtable           |
|eprice            |
|favor             |
|guest             |
|hotel             |
|houseclass        |
|invocation        |
|menuitem          |
|ownerinfo         |
|paytype           |
|person            |
|qy_view           |
|reject            |
| residencecommunity |
|room              |
|unit              |
|user_db           |
|usertable         |
|waiter            |
|waitertype        |
|wprice            |
|wyprice           |
+--------------------+
32 rows in set (0.25 sec)
//创建表book_db
mysql> create table book_db(
   -> Id int not null auto_increment primary key,
   -> BookName varchar(20) not null,
   -> RegTime timestamp ,
   -> Amount int(8) default 0,
   -> Price decimal(12,0)
   -> );
Query OK, 0 rows affected (0.36 sec)

//查看表结构
mysql> show create table book_db G
*************************** 1. row***************************
      Table: book_db
Create Table: CREATE TABLE `book_db` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `BookName` varchar(20) NOT NULL,
  `RegTime` timestamp NOT NULL DEFAULTCURRENT_TIMESTAMP ON UPDATE CURRENT_TIM
TAMP,
  `Amount` int(8) DEFAULT '0',
  `Price` decimal(12,0) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.02 sec)

//查看表结构

mysql> desc book_db;
+----------+---------------+------+-----+-------------------+-----------------------------+
| Field    |Type         | Null | Key |Default          |Extra                      |
+----------+---------------+------+-----+-------------------+-----------------------------+
|Id      |int(11)      | NO   | PRI |NULL             |auto_increment             |
| BookName | varchar(20)   |NO     |NULL                                        |
| RegTime  |timestamp    | NO     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| Amount   |int(8)       | YES    |0                                           |
| Price    |decimal(12,0) | YES    |NULL                                        |
+----------+---------------+------+-----+-------------------+-----------------------------+
5 rows in set (0.03 sec)

//查看帮助

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 Network 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 command.
connect   (\r) Reconnect to theserver. Optional arguments are db and host.
delimiter (\d) Set statement delimiter. NOTE: Takes the rest of theline as new delimiter.
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.
notee    (\t) Don't write into outfile.
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.
tee      (\T) Set outfile [to_outfile]. Append everything into givenoutfile.
use      (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to anothercharset. Might be needed for processing binlog with multi-bytecharsets.
warnings  (\W) Show warnings after everystatement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

//删除数据库
mysql> drop database  if existsbook;
Query OK, 0 rows affected, 1 warning (0.00 sec)
//创建数据库
mysql> create database book;
Query OK, 1 row affected (0.05 sec)
//打开数据库
mysql> use book;
Database changed
//删除表book_db
mysql> drop table if exists book_db;
Query OK, 0 rows affected, 1 warning (0.00 sec)
//创建表book_db
mysql> create table book_db(
   -> Id int not null auto_increment primary key,
   -> BookName varchar(20) not null,
   -> RegTime timestamp ,
   -> Amount int(8) default 0,
   -> Price decimal(12,0)
   -> );
Query OK, 0 rows affected (0.09 sec)

mysql> \. d:\book.sql
Query OK, 1 row affected (0.25 sec)

所属分类:编程相关Mysql    作者:新浪博客    时间:2010-11-20 0:00:00

文章导航