博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql
阅读量:5122 次
发布时间:2019-06-13

本文共 6320 字,大约阅读时间需要 21 分钟。

一、MySQL的SQL语句

First column Second column
SQL Structure Query Language
DDL(Data Definition Language ) create , drop , alter
DML(Data Manipulation Language ) insert , update , delete
manipulation [me nipu'leition] 操纵
update 修改

二、Creating and Using a Database

Suppose that you have serveral pets in your home (your menagerie) and you would like to keep track of various types of information about them.You can do so by creating tables to hold your data and loading them with the desired information.

  • Creating a database

  • Create a table

  • Load data into table

  • Retrieve data from the table in various ways

  • use multiple tables

word translation
menagerie [me'naigeri]小动物园
UTF-8 8-bit Unicode Transformation Format,万国码
format 版式
transformation 变换
snoop 窥探,探听

三、Creating and Selecting a Database

  • If the administrator creates your database for you when setting up your permissions,you can begin using it.Otherwise,you need to create it yourself:
mysql>CREATE DATABASE menagerie;
  • Creating a database does not select it for use;you must do that explicitly.To make menagerie the current database,use this statement:
mysql>USE menagerieDatabase changed

四、Creating a Table

The harder part is deciding what the structure of your database should be:what tables you need and what columns should be in each of them.

You want a table that contains a record for each of your pets.This can be the pet table,and it should contain,as a bare minimum,each animal's name.Because the name by itself is not very interesting,the table should contains other information.For example,if more than one person in your family keeps pets,you might want to list each animal's owner.You might also want to record some basic descriptive information such as species and sex.

How about age?That might be of interesting,but it is not a good thing to store in a database.Age changes as time passes,which means you'd have to update your records often.Instead,it is better to store a fixed value such as data of birth.Then,whenever you need age,you can calculate it as the difference between the current data and the birth data.MySQL provides functions for doing date arithmetic,so this is not difficult.Storing birth data rather than age has other advantages,too:

  • You can use the databases for tasks such as generating reminder for upcoming pet birthdays.(If you think this type of query is somewhat silly,note that it is the same question you might ask in the context of a business database to identify clients to whom you need to send out birthday greetings in the current week or month,for that computer-assisted personal touch.)

  • You can calculate age in relation to dates other than the current date.For example,if you store death date in the database,you can easily calculate how old a pet was when it died.

You can probably think of other types of information that would be useful in the pet table,but the ones identified so far are sufficient:name,owner,species,sex,birth,and death.

Use a CREATE TABLE statement to specify the layout of your table:

mysql>CREATE TABLE pet (name VARCHAR(20),owner VARCHAR(20),species VARCHAR(20),sex CHAR(1),birth DATA,death DATA);
word translation
descriptive 描写的,描述的,分类的
bare minimum 最低限度
generating 生成
somewhat 稍微,有点
sufficient 充足的,足够的
specify 指定,详述
specify the layout 指定布局
species 物种

四、Loading Data into a Table

After creating your table,you need to populate it.The LOAD DATA and INSERT statements are useful for this.

Suppose that your pet records can be described as shown here.(Observe that MySQL expects dates in 'YYYY-MM-DD'format;this may be different from what you are used to)

name owner species sex birth death
Fluffy Harold cat f 1993-02-04 NULL
Claws Gwen cat m 1994-03-17 NULL
Buffy Harold dog f 1989-05-13 NULL
Fang Benny dog m 1990-08-27 NULL
Bowser Diane dog m 1979-08-31 1995-07-29
Chirpy Gwen bird f 1998-09-11 NULL
Whislter Gwen bird NULL 1997-12-09 NULL
Slim Benny Snake m 1996-04-29 NULL
Puffball Diane hamster f 1999-03-30

五、Deleting Data from a Table

delete from pet where name='Whistler'

六、Loading Data into a Table

mysql>LOAD DATA LOCAL INFILE'/path/pet.txt'INTO TABLE pet     >LINES TERMINATED BY '\r\n';

七、pet.txt

Buffy   Harold  dog         f   1989-05-13  NULLFang    Benny   dog         m   1990-08-27  NULLBowser  Diane   dog         m   1979-08-31  NULLChirpy  Gwen    bird        f   1998-09-11  NULLWhistlerGwen    bird      NULL  1997-12-09  NULLSlim    Benny   Snake       m   1996-04-29  NULLPuffballDiane   hamster     f   1999-03-30  NULL

1433065-20180706134446430-1210666693.png

八、MySQL的SQL语句

First column Second column
SQL Structure Query Language
DDL(Data Definition Language ) create , drop , alter
DML(Data Manipulation Language ) insert , update , delete
manipulation [me nipu'leition] 操纵
update 修改

九、Loading Data into a Table

You could create a text file pet.txt containing one record per line,with values separated by tabs,and given in the order in which the columns were listed in the CREATE TABLE statement.For missing values (shch as unknown sexes or death dates for animals that are still living),you can use NULL valuesl.To represent these in your text file,use \N(blackflash,capital-N).For example,the record for Whistler the bird would look like this(where the whitespace between values is a single tab character):

Whistler    Gwen    bird    \N  1997-12-09  \N

If you created the file on Windows with an editor that uses \r\n as a line terminator,you should use this statement instead:

mysql>LOAD DATA LOCAL INFILE'/path/pet.txt'INTO TABLE pet     >LINES TERMINATED BY '\r\n';

十、Retrieving Informatica from a Table

The SELECT statement is used to pull information from a table.The general form the statement is:

SELECT what_to_selectFROM which_tableWHERE conditions_to_satisfy;
  • what_to_select indicates what you want to see.This can be a list of columns

    • indicate "all columns"

-which_table indicates the tables from which you want to retrieve data.The WHERE clause is optional.

  • conditions_to_satisfy specifies one or more conditions that rows must satisfy to quality for retrieval.
word translation
retrieval 检索
specify 指定;详述
terminated 结束

十一、DELETING A TABLE

delete from table where 1=1;

after that,we can use the statement :

DESCRIBE pet;

十二、Loading data into a table

mysql>LOAD DATA LOCAL INFILE 'E:\pet.txt' INTO TABLE pet     >LINES TERMINATED BY '\r\n';

1433065-20180706220144943-1350003379.png

转载于:https://www.cnblogs.com/hugeng007/p/9278541.html

你可能感兴趣的文章
032. asp.netWeb用户控件之一初识用户控件并为其自定义属性
查看>>
Ubuntu下安装MySQL及简单操作
查看>>
前端监控
查看>>
clipboard.js使用方法
查看>>
移动开发平台-应用之星app制作教程
查看>>
leetcode 459. 重复的子字符串(Repeated Substring Pattern)
查看>>
伪类与超链接
查看>>
centos 7 redis-4.0.11 主从
查看>>
永远的动漫,梦想在,就有远方
查看>>
springboot No Identifier specified for entity的解决办法
查看>>
慵懒中长大的人,只会挨生活留下的耳光
查看>>
"远程桌面连接--“发生身份验证错误。要求的函数不受支持
查看>>
【BZOJ1565】 植物大战僵尸
查看>>
VALSE2019总结(4)-主题报告
查看>>
浅谈 unix, linux, ios, android 区别和联系
查看>>
51nod 1428 活动安排问题 (贪心+优先队列)
查看>>
中国烧鹅系列:利用烧鹅自动执行SD卡上的自定义程序(含视频)
查看>>
Solaris11修改主机名
查看>>
latex for wordpress(一)
查看>>
如何在maven工程中加载oracle驱动
查看>>