MySQL基本语法

一、连接

我用的可视化软件是navicat,打开navicat双击自己的local-mysql就连接上了本地的数据库了
或者利用语句,如下所示:

1. 启动mysql服务器

net start mysql

2. 关闭

net stop mysql

3. 进入

mysql -h 主机地址 -u 用户名 -p 用户密码

4. 退出

exit

5. 显示当前mysql的version的各种信息

status

二、 基本操作

1.显示上数据库

我在mysql-localhost的基础上显示我本机的数据库

show databases;

显示数据库

2.删除数据库
  • 直接删除

drop database 数据库名字;

  • 首先会进行判断,有的话删除

drop database if exists 数据库名字;

3. 创建数据库

create database 数据库名字;

4. 使用数据库

如果是navicat,可以直接点击要进入的数据库,右键运行命令行界面就可以直接使用,而不需要语句;

use 数据库名字;

5. 显示数据库中的表

show tables;

下图是我做实验时自己建立的表:
建立的表

6. 删除表
  • 直接删除
    例如我这里删除student表

drop student;

  • 先判断表是否存在,存在先删除

drop table if exists student;

7. 创建表
create table student(
id int auto_increment primary key,  //设为主键
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
);

如下图所示,是我创建的表:
是在语句后边定义的CustomerID为主键
并且对CName设置的是否为NULL,
对Tel 设置为特殊值
创建表

8. 查看表结构

desc student;

表结构

9. 插入数据

只需用将数据按照顺序输入就是,字符串用单引号括起来即可;

insert into student values(02151043, ‘刘备’, ‘男’, 20, ‘15963789546’, ‘软件工程’);

10. 修改某一条数据

例如,我要把小乔的city改为浙江
修改1

update customers set City=‘浙江’ where CustomerD=123;

修改结果如下图所示
修改结果

11. in 查询制定集合内的数据

select * from student where id in (1,3,5);

12. 排序 asc 升序 desc 降序

select * from student order by id asc;

13. and or between

and

select * from student where date>‘1988-1-2’ and date<‘1988-12-1’;

or 或

select * from student where date<‘1988-11-2’ or date>‘1988-12-1’;

between

select * from student where date between ‘1988-1-2’ and ‘1988-12-1’;

14. 聚集函数
select max(id),name,sex from student group by sex;
 
select min(date) from student;
 
select avg(id) as '求平均' from student;
15. 统计

统计表中总数

select count(*) from student;

统计表中性别总数 若有一条数据中sex为空的话,就不予以统计~

select count(sex) from student;

查询第i条以后到第j条的数据(不包括第i条)

select sum(id) from student;

显示3-5条数据

select * from student limit 2,5;

三、巩固操作

修改表的名字

格式:

alter table tbl_name rename to new_name
alter table c rename to a;

表position 增加列test

alter table position add(test char(10));

表position 修改列test

alter table position modify test char(20) not null;

表position 修改列test 默认值

alter table position alter test set default ‘system’;

表position 去掉test 默认值

alter table position alter test drop default;

表position 去掉列test

alter table position drop column test;

表depart_pos 删除主键

alter table depart_pos drop primary key;

表depart_pos 增加主键

alter table depart_pos add primary key PK_depart_pos
(department_id,position_id);

查询常用语句
select name,age ,id from c
select * from c where age>40 and age<60;  //and
select * from c where age<40 or age<60;  //or
select * from c where age between 40 and 60 //between
select * from c where age in (30,48,68,99);     //in 查询指定集合内的数据
select * from c order by age desc;      //order by (asc升序 des降序)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值