MYSQL(2)(连接数据库--插入数据-关系函数)

MYSQL

连接数据库

  • 连接数据库

     mysql -uroot -p
    
  • 显示数据库

     show databases;
    
  • 创建数据库

     create database  userinfo character set'utf8';    userinfo是创建的名字
    
  • 显示创建数据库信息

     show create database userinfo;
    
  • 删除数据库

     drop database userinfo;
    
  • 进入(使用数据库)

     use test;     test 是创建的名字
    
  • 显示当前打开的数据库

     select database();
    
  • 创建新用户

     create user 'limeiwei'@'localhost' identified by'123456'; 
    
  • 修改用户密码

     set password for'limeiwei'@'localhost'=password('1234');
    
  • 删除用户

     drop user'limeiwei'@'localhost';
    
  • 创建数据表

     create table students(id int,name varchar(20),age int,height float);
    
  • //创建表

     create table students(id int,name varchar(20),sex ENUM('w','m'),birthday date,tel char(11),hobby set('1','2','3','4'));
     insert into students VALUES(1,'王刚','w','2020-12-12','13911111111','5');
    
  • //带属性的创建表

     create table t1(id int PRIMARY KEY AUTO_INCREMENT,name varchar(20) NOT null,age tinyint 		DEFAULT 15,tel char(11) UNIQUE);
     INSERT into t1(name,tel) VALUES('王刚','13911111111');
    
  • 删除数据表

     drop table teacher;
    
  • 显示数据里的表

     show tables;
    
  • 查看表带通配符的命令

     show tables like 'help%';
    
  • 查看表创建的信息

     show create table  teacher;
    
  • 查看数据表结构

     desc teacher;
    
  • 注释

     select 1+1;#直到该行结束
    
  • 中间注释

     select 1/*中间注释*/+1
    
     create table student(id bigint,name varchar(30),age tinyint,stuprice smallint);
    

插入数据

  • 指定字段插入数据

     insert into t1(name,age) VALUES('张三',25);
    
  • 省略字段插入数据

     insert into t1  VALUES(null,'李四',30,'12222222222');
    
  • 插入多条记录

     insert into t1  VALUES(null,'李刚',23,'12235562222'),(null,'李六',23,'122352346222');
    
  • 指定条件删除数据 ,删除id等于4的数据

     delete from t1 where id=4;
    
  • 删除所有记录,并且id重新从1开始

     truncate table t1;
    

修改数据

  • 修改单条记录

     update t1 set name='张三',age='34' where id=2;
    
  • 修改多条记录

     update t1 set name=case id
     when 1 then'王刚1'
     when 1 then'王王2'
     when 1 then'王五3'
     end,
     age=case id
     when 1 then 20
     when 2 then 30
     when 3 then 40	
     end
     where id in (1,2,3);
    

查看数据

  • 查看当前使用的数据库

     select database();
    
  • 查看当前MISQL版本

     select version();
    
  • 查看当前用户

     select user();
    
  • 查看运算结果

     select 1+2;
    
  • 查看所有列的数据, *代表所有制

     select *from help_topic;
    
  • 选择指定列的数据

     select name,example,url from help_topic;
    
     select distinct age from t1;
     select count(distinct age) from t1;
    

关系函数

  • or或

     select *from help_keyword where help_keyword_id=1 or help_keyword_id=38787;
    
  • and 并

     select *from help_keyword where help_keyword_id>1 && help_keyword_id<37;
    
  • 查询id奇数的记录

     select *from help_keyword where help_keyword_id % 2=1;
     select *from help_keyword where help_keyword_id=1  || help_keyword_id=3 ||help_keyword;
    
  • in查询id在集合里的记录

     select *from help_keyword where help_keyword_id in (1,3,4,2323);
    
  • 一次删除多条记录

     delete from t1 where id=1 || id =3;
     delete from t1 where id in(2,4,12,15,23,34);
    
  • 查询id在5和8之间的数据

     select *from t1 where id>1 && id<=3;
    
  • 包括id=5和id=8的记录

     select *from t1 where id between 5 and 8;
    
  • 删除id在5和7之间的数据

     delete from t1 where id between 5 and 7;
    
  • 查询id不在8和11之间的数据

     select *from t1 where id  not between 8 and 11;
    
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值