MySQL Common commands

Source: Internet
Author: User

Tag: Ack drop includes table structure data alter DEF from Field

本文参考《MySQL5.6从零开始学》3、4、5、7、8章。

生活每天都是一种煎熬,尤其是在夜晚,因为白天不懂夜的黑嘛。

我觉得有句话说的真对:女生如果觉得一个男生帅就嫁给他,这是好色;男生因为女生漂亮而娶她,这叫审美。

不知道大家是怎么看的。。。。。。

我不好色,我审美。

开始正文

范围:数据库database 》 数据表table 》 字段 》 数据

一、数据库的基本操作

1.创建数据库                   create database [数据库名];

2.删除数据库                   drop database [数据库名];

3.查看所有数据库                show databases;

4.使用数据库                    use [数据库名];

 

二、数据表的基本操作

1.创建数据表(先使用—use [数据库])

create table <表名>

字段名1 数据类型 [列级别约束条件] [默认值],

字段名2 数据类型 [列级别约束条件] [默认值],

字段名3 数据类型 [列级别约束条件] [默认值],

[表级别约束条件]

);

例子:

create table tb_emp1

(

id int(11),

name varchar(25)

);

2.使用主键约束—主键能够唯一地标识表中的一条记录

例子(定义id为主键)

create table tb_emp2

(

id int(11) primary key,

name varchar(25),

salary float

);

例子(定义id和name为主键)

create table tb_emp3

(

id int(11),

name varchar(25),

salary float,

primary key(id,name)

)

3.使用非空约束

字段名 数据类型 not null,

4.使用唯一性约束

字段名 数据类型 unique

5.使用默认约束

字段名 数据类型 default 默认值

6.设置表的属性值自动增加(比如id)

字段名 数据类型 auto_increment,

7.查看数据表结构

describe 表名;

8.修改数据表名

alter table <旧表名> rename <新表名>

9.修改字段的数据类型

alter table <表名> modify <字段名> <数据类型>

10.修改字段名

alter table <表名> change <旧字段名> <新字段名> <新数据类型>

11.添加字段

Alter table <表名> add <新字段名> <数据类型>

12.删除字段

alter table <表名> drop <字段名>

13.删除数据表

drop table 表1,表2,表3

 

三、查询数据

1. 数据表查询

查询所有字段

select * from 表名

查询多字段

select 字段1,字段2 from 表名;

查询单字段

select 字段1(列名) from 表名;

2. 查询指定记录

select 字段1,字段2,字段3 from 表名 where 查询条件

例子:

Select 字段1,字段2 from 表名 where 字段2 = 数值;

Select f_name, f_price from fruits where f_name = ‘apple’;

Select f_name, f_price from fruits where f_price < 10.2

3. 范围是between and 的

Select f_name, f_price from fruits where f_price between 2 and 10;

4. 带like的匹配,%表示任意长度字符(0-正无穷),_表示只能匹配一次。

查询f_name中以b开头的内容

Select f_id,f_name from fruits where f_name like ‘b%’

查询f_name中包含字母‘g’的记录

Select f_id,f_name from fruits where f_name like ‘%g%’

5. 查询空值(如查询字段1中的空值)

Select 字段1,字段2,… from 表名 where 字段1 is null

6. 带and的多条件查询

Select f_name,f_price from fiuts where f_name = ‘qaz’and f_price=10;

7. 带or的多条件查询

Select f_name,f_price from fiuts where f_name = ‘qaz’or f_price=10;

8. Count函数—计算总行数

Select count(*) as cust_num from customers;

Count(*)返回customers表中记录的总行数,返回的总数名称为cust_num。

9. Max函数

Select max(f_price) as max_price from fruits;

10. Min函数

Select min(f_price) as min_price from fruits;

11. sum函数(返回指定列的总和)

12. avg()函数(求一行的平均值)

 

四、插入、更新与删除数据

1. 插入数据

insert into 表名(列名) values (数据)

例如:

Insert into person(id,name,age) values(1,“qwer”,23)

向person表中插入一条数据包括(id,name,age),其他列为默认值。

同时插入多条数据

Inert into 表名(列名)values(数据1 ),(数据2),(数据3);

2. 更新数据(添加完数据后需要更新一下数据)

Updata 表名 set 列名1 = 数据1 ,列名2 = 数据2,列名3 = 数据3 where 位置;

例子:

Update person set age=15,name=’liming’ where id=1;

Update person set info=‘student’ where id between 19 and 23;

3. 删除数据

Delete from 表名 where 位置

例子:

Delete from person where id=13;

Delete from person; 删全表

 

五、MySQL数据类型和运算符

1. 整数类型

int()

2. 浮点数类型

float

double

3. 日期与时间

year YYYY

time HH:MM:SS

date YYYY-MM-DD

datatime YYYY-MM-DD HH:MM:SS

4. 字符串类型

char() 固定长度的非二进制字符串

varchar() 变长非二进制字符串

5. 二进制类型

bit() 位字段类型

 

内容并不是很全,只是一些常用的基本的操作。

但是对于数据库的增、删、改、查已经可以做到了。

明天还要搬砖啊,就写到这了。

MySQL Common commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.