码迷,mamicode.com
首页 > 数据库 > 详细

[mysql系列02]mysql常用语法

时间:2015-03-15 18:24:49      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:

本文结合具体的实例讲解mysql基础常用语法

CREATE DATABASE

CREATE DATABASE db_name

CREATE DATABASE用给定的名字创建一个数据库,创建数据库mysqllearn。

create database mysqllearn;

CREATE TABLE

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
[table_options] [select_statement]

create_definition:
  col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
            [PRIMARY KEY] [reference_definition]
  or    PRIMARY KEY (index_col_name,...)
  or    KEY [index_name] (index_col_name,...)
  or    INDEX [index_name] (index_col_name,...)
  or    UNIQUE [INDEX] [index_name] (index_col_name,...)
  or    [CONSTRAINT symbol] FOREIGN KEY index_name (index_col_name,...)
            [reference_definition]
  or    CHECK (expr)

CREATE TABLE在当前数据库中用给出的名字创建一个数据库表,创建数据库表t_video

DROP TABLE IF EXISTS `t_video`;
CREATE TABLE `t_video` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `code_number` varchar(20) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  `director` varchar(50) DEFAULT NULL,
  `starring` varchar(100) DEFAULT NULL,
  `release_time` date DEFAULT NULL,
  `production_area` varchar(50) DEFAULT NULL,
  `showtimes` int(11) DEFAULT 0,
  `hits` int(11) DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

ALTER TABLE

ALTER [IGNORE] TABLE tbl_name alter_spec [, alter_spec ...]

alter_specification:
        ADD [COLUMN] create_definition [FIRST | AFTER column_name ]
  or    ADD INDEX [index_name] (index_col_name,...)
  or    ADD PRIMARY KEY (index_col_name,...)
  or    ADD UNIQUE [index_name] (index_col_name,...)
  or    ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
  or    CHANGE [COLUMN] old_col_name create_definition
  or    MODIFY [COLUMN] create_definition
  or    DROP [COLUMN] col_name
  or    DROP PRIMARY KEY
  or    DROP INDEX index_name
  or    RENAME [AS] new_tbl_name
  or    table_options

 ALTER TABLE允许你修改一个现有表的结构。例如,你可以增加或删除列、创造或消去索引、改变现有列的类型、或重新命名列或表本身。你也能改变表的注释和表的类型。

现在我们尝试去修改刚刚定义的表t_video中showtimes和hits字段的数据格式,修改前表结构可用describe tb_name来查看:

技术分享

讲主演starring字段长度修改为200,修改语句如下:

alter table t_video modify starring varchar(200);

 INSERT

    INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
        [INTO] tbl_name [(col_name,...)]
        VALUES (expression,...),(...),...
或  INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
        [INTO] tbl_name [(col_name,...)]
        SELECT ...
或  INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
        [INTO] tbl_name
        SET col_name=expression, col_name=expression, ...

INSERT把新行插入到一个存在的表中,INSERT ... VALUES形式的语句基于明确指定的值插入行,INSERT ... SELECT形式插入从其他表选择的行。

插入部分实例数据如下:

INSERT INTO `t_video` VALUES (‘1‘, ‘F_C_001‘, ‘心花怒放‘, ‘宁浩‘, ‘黄渤,徐峥,袁泉,周冬雨,陶慧‘, ‘2014-09-30‘, ‘中国‘, ‘0‘, ‘0‘);
INSERT INTO `t_video` VALUES (‘2‘, "F_C_002","黄金时代","许鞍华","汤唯 冯绍峰 王志文 朱亚文 黄轩","2014-10-01","中国","100","102400");
INSERT INTO `t_video` VALUES (‘3‘, "F_C_003","食人虫","严嘉","夏梓桐 张梓琳 王传君 丁春诚 ","2014-10-10","中国","121","102100");
INSERT INTO `t_video` VALUES (‘4‘, "F_C_004","一生一世","邹佡","谢霆锋 高圆圆 杜海涛 洛诗 林雪 车晓","2014-09-05","中国","100","102421");
INSERT INTO `t_video` VALUES (‘5‘, "F_C_005","亲爱的","陈可辛","赵薇 黄渤 佟大为 郝蕾 张译 张雨绮","2014-09-25","中国","100","102422");
INSERT INTO `t_video` VALUES (‘6‘, "F_C_006","闺蜜","黄真真","陈意涵 薛凯琪 杨子姗 余文乐 钟汉良 吴建豪","2014-07-31","中国","100","102434");

DELETE

DELETE [LOW_PRIORITY] FROM tbl_name
    [WHERE where_definition] [LIMIT rows]

DELETEtbl_name表中删除满足由where_definition给出的条件的行,并且返回删除记录的个数。如果你发出一个没有WHERE子句的DELETE,则所有行都被删除。

删除id=6的记录示意如下:

技术分享

SELECT

SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [HIGH_PRIORITY]
       [DISTINCT | DISTINCTROW | ALL]
    select_expression,...
    [INTO {OUTFILE | DUMPFILE} ‘file_name‘ export_options]
    [FROM table_references
        [WHERE where_definition]
        [GROUP BY col_name,...]
        [HAVING where_definition]
        [ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] ,...]
        [LIMIT [offset,] rows]
        [PROCEDURE procedure_name] ]

SELECT被用来检索从一个或多个表中精选的行。select_expression指出你想要检索的列。SELECT也可以用来检索不引用任何表的计算行。

 

 LOAD DATA INFILE

LOAD DATA [LOW_PRIORITY] [LOCAL] INFILE file_name.txt [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [FIELDS
        [TERMINATED BY ‘\t‘]
        [OPTIONALLY] ENCLOSED BY ‘‘]
        [ESCAPED BY ‘\\‘ ]]
    [LINES TERMINATED BY ‘\n‘]
    [IGNORE number LINES]
    [(col_name,...)]

MySQL的LOAD DATA INFILE语句用于高速地从一个文本文件中读取行,并装入一个表中。文件名称必须为一个文字字符串。下面以MySQL5为例说明,说明如何使用MySQL的LOAD DATA命令实现文本数据的导入。

数据库中,最常见的写入数据方式是通过SQL INSERT来写入,另外就是通过备份文件恢复数据库,这种备份文件在MySQL中是SQL脚本,实际上执行的还是在批量INSERT语句。在实际中,常常会遇到两类问题:一类是数据导入,比如从word、excel表格或者txt文档导入数据(这些数据一般来自于非技术人员通过OFFICE工具录入的文档);一类数据交换,比如从MySQL、Oracle、DB2数据库之间的数据交换。

接下来介绍一下mysql的数据导入和导出

技术分享

将表t_video表中的数据导出到d:/data.txt目录文件中:

SELECT * FROM t_video INTO OUTFILE d:/data.txt
           FIELDS TERMINATED BY ,
           lines terminated by \r\n;

文件导出后将t_video表清空,再执行导入

LOAD DATA INFILE d:/data.txt into table t_video
           FIELDS TERMINATED BY ,
           lines terminated by \r\n;

技术分享

 

[mysql系列02]mysql常用语法

标签:

原文地址:http://www.cnblogs.com/molyeo/p/4339577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!