MySql基础

MySQL的数据类型

mysql有三大数据类型,分别为数字,日期\时间,字符串,这三大类中又更细致的划分了许多子类型:

  1. 数字类型
    • 整数: tinyint, smallint, mediumint, int, bligint
    • 浮点数: float, double, real, decimal
  2. 日期和时间:date,time,datetime,timestamp,year
  3. 字符串类型
    • 字符串:char,varchar
    • 文本:tinytext,text,mediumtext,longtext
    • 二进制(可用来存储图片,音乐等):tinyblob,blob,mediumblob,longblob
登录到MySQL

mysql -h 主机名 -u 用户名 -p

-h 参数用于指定客户端要登录的MySQL主机名,登录当前机器该参数可以省略。

创建一个数据库
create database 数据库名 [其他选项];

character set gbk将数据库字符编码指定我gbk。

create database test_db character set gbk;
查看已经创建的数据库
show databases;
选择要操作的数据库
  • 在登录数据库时指定,命令:mysql -D 所选择的数据库名 -h 主机名 -u 用户名 -p
例如登录时选择刚刚创建的数据库
msyql -D test_db -u root -p
  • 登录后使用use,use语句可以不加分号
use 数据库名;
创建数据库表

create table 表声明(列声明);

create table students
(
    id int unsigned not null auto_increment primary key,
    name char(8) not null,
    sex char(4) not null,
    age tinyint unsigned not null,
    tel char(13) null default "-"
);
  • 小括号()– 列声明
  • 逗号隔开列声明
  • unsigned

如果需声明无符号类型的话就需要在类型前加上unsigned,无符号类型能保存2倍于有符号类型的正整数数据,比如16位系统中一个short能存储的数据的范围为-32768~32767,而unsigned能存储的数据范围则是0 ~ 65535。
- not null
- auto_increment
- primary key

查看所有的数据库表

show tables;

mysql> show tables;
+------------------+
| Tables_in_testhu |
+------------------+
| students         |
+------------------+
1 row in set (0.00 sec)
查看具体的一个数据库表

describe 表名

mysql> describe students;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| name  | char(8)             | NO   |     | NULL    |                |
| sex   | char(4)             | NO   |     | NULL    |                |
| age   | tinyint(3) unsigned | NO   |     | NULL    |                |
| tel   | char(13)            | YES  |     | -       |                |
+-------+---------------------+------+-----+---------+----------------+
5 rows in set (0.03 sec)
向表中插入数据

insert [into] 表名 [(列名1,列名2,列名3,…] values (值1,值2, …) ;

mysql> insert into students values(null, "胡明远", "男", 24, "17721079930");
Query OK, 1 row affected (0.02 sec)
查询表中的数据

select 列名称 from 表名称 [查询条件];

mysql> select name, age from students;
+-----------+-----+
| name      | age |
+-----------+-----+
| 胡明远    |  24 |
+-----------+-----+
1 row in set (0.00 sec)
mysql> select * from students where sex = "男";
+----+-----------+-----+-----+-------------+
| id | name      | sex | age | tel         |
+----+-----------+-----+-----+-------------+
|  1 | 胡明远    | 男  |  24 | 17721079930 |
+----+-----------+-----+-----+-------------+
1 row in set (0.00 sec)
查询年龄在21岁以上的所有人信息: select * from students where age > 21;

查询名字中带有 "王" 字的所有人信息: select * from students where name like "%王%";

查询id小于5且年龄大于20的所有人信息: select * from students where id<5 and age>20;
更新表中的数据

update 表名称 set 列名称=新值 [where 更新条件]

mysql> update students set tel=17721078830 where id=1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0
将id为5的手机号改为默认的"-": update students set tel=default where id=5;

将所有人的年龄增加1: update students set age=age+1;

将手机号为 13288097888 的姓名改为 "张伟鹏", 年龄改为 19: update students set name="张伟鹏", age=19 where tel="13288097888";
删除表中的数据

delete from 表名称 where 删除条件;

删除id为2的行: delete from students where id=2;

删除所有年龄小于21岁的数据: delete from students where age<20;

删除表中的所有数据: delete from students;
创建后表的修改
  • ##### 添加列
    alter table 表名 add 列名 列数据类型 [after 插入位置];
在表的最后追加列 address: alter table students add address char(60);

在名为 age 的列后插入列 birthday: alter table students add birthday date after age;
  • ##### 修改列
    alter table 表名 change 列名称 列新名称 新数据类型;
将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";

将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;
  • ##### 删除列
    alter table 表名 drop 列名称;
删除 birthday 列: alter table students drop birthday;
  • ##### 重命名表
    alter table 表名 rename 新表名;
重命名 students 表为 workmates: alter table students rename workmates;
  • ##### 删除整张表
    drop table 表名;
删除 workmates 表: drop table workmates;
  • ##### 删除整个数据库
    drop database 数据库名;
删除 samp_db 数据库: drop database samp_db;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值