MySQL语句整理


1.MySQL服务的启动、停止与卸载

在Windows命令提示符下运行:
启动: net start MySQL
停止: net stop MySQL
卸载: sc delete MySQL

2. MySQL中的数据类型

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

数字类型
整数: tinyint、smallint、mediumint、int、bigint
浮点数: float、double、real、decimal

日期和时间 : date、time、datetime、timestamp、year

字符串类型
字符串: char、varchar
文本: tinytext、text、mediumtext、longtext
二进制(可用来存储图片、音乐等): tinyblob、blob、mediumblob、longblob

3.登录到MySQL

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

-h : 该命令用于指定客户端所要登录的MySQL主机名, 登录当前机器该参数可以省略;
-u : 所要登录的用户名;
-p : 告诉服务器将会使用一个密码来登录, 如果所要登录的用户名密码为空, 可以忽略此选项。


4.创建一个数据库

create database 数据库名 [其他选项];

5.选择所要操作的数据库

一: 在登录数据库时指定,                 命令: mysql -D 所选择的数据库名 -h 主机名 -u 用户名 -p
二: 在登录后使用 use 语句指定,     命令: use 数据库名;

6. 创建数据库表

create table 表名称(列声明);

eg:
 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 "-"
    );

7. 向表中插入数据

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

8. 查询表中的数据

select 列名称 from 表名称 [查询条件];
where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;

9. 更新表中的数据
update 表名称 set 列名称=新值 where 更新条件;

10. 删除表中的数据
delete from 表名称 where 删除条件;

11. 添加列
基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];
示例: 在表的最后追加列 address: alter table students add address char(60);
在名为 age 的列后插入列 birthday: alter table students add birthday date after age;

12. 修改
基本形式: 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;

12. 删除列
基本形式: alter table 表名 drop 列名称;
示例: 删除 birthday 列: alter table students drop birthday;

13. 重命名表
基本形式: alter table 表名 rename 新表名;
示例: 重命名 students 表为 workmates: alter table students rename workmates;

14.删除整张表
基本形式: drop table 表名;
示例: 删除 workmates 表: drop table workmates;

15.删除整个数据库
基本形式: drop database 数据库名;
示例: 删除 samp_db 数据库: drop database samp_db;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值