1.查看所有的数据库

1
show databases;

2.创建数据库  后面的时编码格式

1
create database dbName charset='utf8';

3.使用/切换数据库

1
use dbName

4.查看正在使用的数据库

1
select database();

5.删除数据库

1
drop database dbName;  

MySQL的数据类型

    整数类型

整数类型 字节数 无符合数的取值范围 有符合数的取值范围
INTYINT 1 0~255 -128~127
SMALLINT 2 0~65535 -32768~32767
MEGIUMINT 3 0~16777215 -8388608~8388607
INT 4 0~4294967295 -2147483648~2147483647
INTEGER 4 0~4294967295 -2147483648~2147483647
BIGINT 8 0~18446744073709551615 -9223372036854775808-9223372036854775807

    浮点类型

    字符串类型

  字符串的常用类型时CHAR和VARCHAR ,下面时他们的区别

插入值 CHAR(5) 占用字节数 VARCHAR(5) 占用字节数
'' '' 五个字节 '' 一个字节
'1' '1' 五个字节 '1' 两个字节
'123' '123' 五个字节 '123' 四个字节
'123 ' '123 ' 五个字节 '123 ' 五个字节
'12345' '12345' 五个字节 '12345' 六个字节

   TEXT类型是一种特殊的字符串类型。TEXT只能保存字符数据。如新闻的内容等。

   类型包括 TINYTEXT、TEXT、MEDIUMTEXT 和LONGTEXT.

      下面将从4中TEXT类型允许的长度的存储空间进行对比

类型 允许的长度 存储空间
TINYTEXT 0~255字节 值的长度+2个字节
TEXT 0~65535字节 值的长度+2个字节
MEDIUMTEXT 0~167772150字节 值的长度+3个字节
LONGTEXT 0~4294967295字节 值的长度+4个字节

    日期与时间类型

日期类型 字节数 取值范围 零值
YEAR 1 1901~2155 0000
DATE 4 1000-01-01~9999-12-31 0000:00:00
TIME 3 -838:59:59~838:59:59 00:00:00
DATETIME 8 1000-01-01 00:00:00~9999-12-31 23:59:59 0000-00-00 00:00:00
TIMESTAMP 4 19700101080001 00000000000000

    表中常见的操作

1.查看当前数据库中的所有数据表

1
show tables;

2.创建表

create table tablename(字段1 数据类型,字段2 数据类型 ...) [charset set 字符集 collate 校对规则]

3.查看表结构

1
desc tablename;

4.重命名表

1
alter table 表原名 rename to 新表明;

5.添加字段

  添加字段(默认添加在最后一个位置)

  alter table tablename add 字段 数据类型;

  添加字段:在表的第一个位置添加字段

  alter table tablename add 字段数据类型 first;

  添加字段: 在指定的位置添加字段

  alter table tablename add 字段 new 数据类型 after 字段old;

6.修改字段

  修改字段: 修改字段数据类型

  alter table tablename modify 字段 数据类型;

  修改字段: 修改字段到第一个位置

  alter table tablename modify 字段数据类型 first;

  修改字段:修改字段到指定位置

  alter table tablename modify 字段数据类型 after 字段;

  修改字段:只修改字段名称 不修改数据类型

  alter table tablename change 字段 newname 原数据类型;

  修改字段 修改字段名称 同时修改数据类型

  alter table tablename change 字段 newname 新数据类型;  

7.删除字段

  alter table tablename drop 字段;

8.删除表

  drop table tablename;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
常用sql语句
  
查看数据库: show  databases;
  
创建一个HA的数据库: create database HA;
  
查看自己所处的位置: select database();
  
删除数据库: drop database 'wg';
  
创建表:
  
语法:**create table** 表名 (**字段名** 类型**,** 字段名 类型**,** 字段名 类型**);**
  
mysql> create table student(id int(20),name char(40),age int);
  
查看表的相关信息:
  
 use mysql ;
  
show tables;
  
查看表结构: desc student;
  
可以指定默认存储引擎和字符集:
  
mysql> create table student2(id int(20),name char(40),age int)ENGINE=MyISAM DEFAULT CHARSET=utf8;
  
  
  
删除表: drop table student2;
  
修改表名称:
  
语法  alter table 表名  rename 新表名
  
alter table student rename students;
  
修改表中的字段类型
  
语法:
  
alter table 表名 modify 要修改的字段名  要修改的类型
  
desc student;
  
alter table students modify column id int(10);
  
修改表中的字段类型和字段名称:
  
语法:**alter table** 表名 change 原字段名 新字段名 新字段类型**;**
  
alter table students change name stname char(20);
  
  
  
在表中添加字段:
  
语法: alter table students add sex enum('M','W');#enum是枚举类型,表示这个字段中的数据只能为F,M,S三个值中的一个
  
在制定位置添加字段
  
如在第一列添加一个字段
  
alter table students add uid int(10) frist;
  
在age后面添加一个字段:
  
alter table students add address char(40) after age;
  
删除表中的字段:
  
 alter table students drop address;
  
插入字段
  
语法:
  
insert   into 表名 values ( 字段值1,字段值2,字段值3);
  
insert into student values(1,'zhangs',21);
  
查询表中的记录
  
select from  student ;
  
select id,name from student;
  
删除id 为3的行:
  
delete from students where id=3;
  
删除age为空的行;
  
delete from students  where age is null;
  
更新记录:
  
update   students set sex='M' where id=2;
  
所有的都变为2
  
update students set id=2;
  
SQL 基础条件查询语句
  
select  name,age from stuendts;
  
去重复查询语句:
  
select distinct name,age from students;
  
select distinct id,name,age from students where id=3;
  
select id,name from students where id >3 and age >25;
  
select id,name from students where id >3 or age >25;
  
mysql 区分大小写查询
  
select name from studnets where name='jk';
  
select from students where binary name ='jk';
  
mysql 查询排序:
  
select distinct id from students order by id;
  
select distinct id from students order by id desc;
  
关于MySQL命令帮助
  
help show;
  
help select;

MySQL数据库的常见操作的更多相关文章

  1. 【代码学习】MYSQL数据库的常见操作

    ---恢复内容开始--- ============================== MYSQL数据库的常见操作 ============================== 一.mysql的连接与 ...

  2. MySQL数据库的常见操作(七)

    MySQL数据库的常见操作 1.创建数据库 2.创建重名的数据库以及如何查看警告信息 3.设置数据库的编码方式(默认为utf8) 4.修改和查看数据库的编码方式 5.删除数据库 6.6.删除已经删除了 ...

  3. 【代码总结】MYSQL数据库的常见操作

    ============================== MYSQL数据库的常见操作 ============================== 一.mysql的连接与关闭 -h:指定所连接的服 ...

  4. Flask中Mysql数据库的常见操作

    from flask import Flask,render_template #导入第三方链接库sql点金术 from flask_sqlalchemy import SQLAlchemy #建立对 ...

  5. Vc数据库编程基础MySql数据库的常见库命令.跟表操作命令

    Vc数据库编程基础MySql数据库的常见操作 一丶数据库常见的库操作 1.1查看全部数据库 命令:  show databases 1.2 创建数据库 命令: Create database 数据库名 ...

  6. python操作mysql数据库的相关操作实例

    python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称 ...

  7. linux系统上Mysql数据库导入导出操作

    需求:把MySQL数据库目录中的dz数据库备份到/home/dz_bak.sql ,然后再新建一个数据库dzbak,最后把/home/dz_bak.sql 导入到数据库dzbak中.操作如下:以下操作 ...

  8. PHP对MySQL数据库的相关操作

    一.Apache服务器的安装 <1>安装版(计算机相关专业所用软件---百度云链接下载)-直接install<2>非安装版(https://www.apachehaus.com ...

  9. mysql 数据库必备命令操作,入门练习一下

    mysql 数据库必备命令操作 show databases: 查看所有的数据库: create database jfedu: 创建名为jfedu数据库: use nihao: 进入jfedu数据库 ...

随机推荐

  1. buuctf@pwn1_sctf_2016

    from pwn import * sh=remote('pwn.buuoj.cn',20086) get_flag=0x08048F0D payload='I'*0x14+'a'*4+p32(get ...

  2. k8s测试容器test-for-k8s.yml

    生成容器 vim test-for-k8s.yml apiVersion: v1 kind: Pod metadata: name: busybox-curl labels: ccb: busybox ...

  3. three months timestamp

    1.有效期三个月 package com.hengqin.life.idps; import java.text.SimpleDateFormat; import java.util.Calendar ...

  4. jquery attribute选择器 语法

    jquery attribute选择器 语法 作用:[attribute] 选择每个带有指定属性的元素.可以选取带有任何属性的元素(对于指定的属性没有限制). 语法:$("[attribut ...

  5. String Compression

    F. String Compression 利用dp和前缀数组来写 dp[i] 所表示的东西是 字符串 s[0:i] (不包括 s[i])能够压缩的最短长度 bj[i][j] 表示的是字符串 s[i: ...

  6. sklearn.model_selection Part 2: Model validation

    1. check_cv() def check_cv(cv=3, y=None, classifier=False): if cv is None: cv = 3 if isinstance(cv, ...

  7. 生成json文件写入本地

    public class Json { public static void main(String[] args) { String fullPath = null; //例如:fullPath=& ...

  8. Springboot 使用 webSocket

    介绍 WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议.在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进 ...

  9. CountDownLatch和CyclicBarrier的比较

    1.CountDownLatch是线程组之间的等待,即一个(或多个)线程等待N个线程完成某件事情之后再执行:而CyclicBarrier则是线程组内的等待,即每个线程相互等待,即N个线程都被拦截之后, ...

  10. 高并发架构系列:Redis为什么是单线程、及高并发快的3大原因详解

    Redis的高并发和快速原因 1.redis是基于内存的,内存的读写速度非常快: 2.redis是单线程的,省去了很多上下文切换线程的时间: 3.redis使用多路复用技术,可以处理并发的连接.非阻塞 ...