MySQL(二)

时间:2019-07-11
本文章向大家介绍MySQL(二),主要包括MySQL(二)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一:命令行连接数据库

  打开终端,运行命令mysql -uroot -p (p后面加密码,可以直接加,也可以回车在下一行输入,为了不暴露密码,回车在下行输入)

  连接成功如下图所示:

  

  退出:exit或quit

  查看版本信息: select version();

  显示当前时间: select now();

  查看所有的数据库:show databases;

  使用数据库: use + 库名;

  查看当前使用的数据库: select database();

  创建数据库:create database 数据库名 charset=utf8;

        例:create database python charset=utf8;

  删除数据库:delete database 数据库名;

        例:delete database python;

  查看当前数据库中所有的表:show tables;

  查看表结构: desc 表名;

  创建表:auto_increment表示自动增长 

CREATE TABLE table_name(
    column1 datatype contrai,
    column2 datatype,
    column3 datatype,
    .....
    columnN datatype,
    PRIMARY KEY(one or more columns)
);

  创建班级表:

create table classes(
    id int unsigned auto_increment primary key not null,
    name varchar(10)
);

  创建学生表:

create table students(
    id int unsigned primary key auto_increment not null,
    name varchar(20) default '',
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum('男','女','人妖','保密'),
    cls_id int unsigned default 0
)

   修改表-添加字段:   

alter table 表名 add 列名 类型;

例:alter table students add birthday datetime;

   修改表-修改字段:重命名版       

alter table 表名 change 原名 新名 类型及约束;
例:alter table students change birthday birth datetime not null;

   修改表-修改字段:不重命名版      

alter table 表名 modify 列名 类型及约束;

例:alter table students modify birth date not null;

    修改表-删除字段 :

alter table 表名 drop 列名; 
例:alter table students drop birth
  删除表:
drop table 表名;
例:drop table students;

  查看建表语句:


show create table 表名;
例: show create table classes;

二: 数据库的怎删改查(curd)
  
  curd的解释: 代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete) 
  
  1.查询基本使用
    
  (1)
查询所有列
     
select * from 表名;   例: select * from classes;    
  (2)查询指定列,可以用as 为列或者表指定别名
     
select 列1,列2 from 表名;   例: select id,name from classes;  
  2.增加
格式:INSERT [INTO] tb_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),...

 说明:主键列是自动增长,但是在全列插入时需要占位,通常使用0或者 default 或者 null 来占位,插入成功后以实际数据为准

 (1)全列插入:值的顺序与表中字段的顺序对应
insert into 表名 values(...);      例:insert into students values(0,’郭靖‘,1,'蒙古','2016-1-2');
 (2)部分列插入,值的顺序与给出的列顺序对应
 insert into 表名(列1,列2,...)values(值1,值2,..);

例: insert into students(name,hometown,birthday) values('黄蓉','桃花岛','2016-3-2');
   (3)全列多行插入:值的顺序与给出的列顺序对应
      
insert into 表名 values(...),(...)...;
  
 例:insert into classes values(0,'python1'),(0,'python2');



insert into 表名(列1,...) values(值1,...),(值1,...)...;
例:insert into students(name) values('杨康'),('杨过'),('小龙女');
      
   3.修改
   
 格式: UPDATE tb_name SET col1={expr1|DEFAULT} [,col2={expr2|default}]...[where 条件判断]

    update 表名 set 列1=值1,列2=值2... where 条件
  
    例:update students set gender=0,hometown='北京' where id=5;
        
   4.删除
格式:DELETE FROM tbname [where 条件判断]
   TRUNCATE tbname


delete from 表名 where 条件    例:delete from students where id=5;
    如果全部清空表中的数据delete from tbname;但是自增字段没有被重置
               truncate taname; 速度更快,并将自增字段重置

原文地址:https://www.cnblogs.com/huiyichanmian/p/11173274.html