Sql 库和表的基本操作、基本数据类型

一、数据库的基本操作

  基本操作:

  1、查看当前数据库:show databases;

  2、进入到指定的数据库:use [数据库名],

  数据库的增删改查:

  1、创建数据库:create database [数据库名]

  2、删除数据库:drop database [数据库名]

  3、修改数据库的编码:alter database [数据库名] charset   gbk;

  4、查看当前数据库:select database();

    show create database [数据库名];

二、表的基本操作

  基本操作:

  1、查看当前数据库里的表:show tables;

  表的增删改查

  1、创建表:

    create table [表名]  (字段+约束)

  2、删除表:

    drop table [表名],

  3、修改表:

    update db1.t1 set name='Andey' where id=2;

  4、查询表

    查询所有select * from [表名]

    根据条件查询:where 条件

    查询字段:select id,name.......

  5、复制表

    create table t2 select * from t1  ( 既复制表结构也复制表内容 );

    create table t2 select * from t1 where 1>2; (只复制表结构)  或:create table t2 like db1.t1;

三、表的存储引擎

  create table t1 ( id int ) engine = innodb;(默认引擎)

  create table t2 (id int )  engine = myisam;

  create table t3(id int)engine=memory;(做缓存,退出后表的数据消失)
  create table t4(id int)engine=blackhole;(无论往表里怎么插数据,都为空)

四、数据的基本类型:

  1、整型(默认使用就行)

  包括tinyint 、smallint 、int 、bigint

  有符号:

  无符号:

   2、float单精度类型:

      float(m,d)m表示小数点前后位的个数,d 表示小数点后的个数

    double双精度类型:

      double(m,d)相同

    float、double两者表示的范围不一样随着小数点后的位数增多,表示的数值越来越不准确

    decimal 精确表示小数,随着小数点后的位数增多,数值一直精准

  3、日期类型

    year:表示年份如1998,2000

    date:YYYY—MM—DD用now()表示年 月 日

    time:HH:MM:SS 用now()表示 时 分 秒

    datetime:YYYY—MM—DD , HH:MM:SS  Now() 年 月 日,时 分 秒

   4、char()和varchar()

    查询: select @@sql_mode;

      https://www.cnblogs.com/majj/p/9167178.html

    在使用char_length()查询长度的时候char 和varchar()

    如:select x,length(x),y,length(y) from t1;

              char()会将字符里的空格删除,显示非空的字符长度(可以通过修改sql_mode修改char的显示)

      varchar()会将字符完全显示出来 包括空格

  5、枚举和集合类型

    enum('x1 ' , ' x2' ,' x3 '......)相当于单选

    set('b1 ' , 'b2 ' ,  ' b3 ' , ' b4 ' ......)可以选中多个

     create table usetable(
          -> id int,
          -> name varchar(20),
          -> sex enum('male','female','other'),
          -> fav set('football','basketball')
          -> );

      insert into usetable values
         -> (1,'alex','male','football,basketball');

五、完整性约束:  

  PRIMARY KEY (PK)    #标识该字段为该表的主键,可以唯一的标识记录
  FOREIGN KEY (FK)    #标识该字段为该表的外键
  NOT NULL    #标识该字段不能为空
  UNIQUE KEY (UK)    #标识该字段的值是唯一的
  AUTO_INCREMENT    #标识该字段的值自动增长(整数类型,而且为主键)
  DEFAULT    #为该字段设置默认值
  UNSIGNED #无符号
  ZEROFILL #使用0填充

  1、not null 与 default 约束同一个字段

   当未添加数据时 会自动设置为 默认值

      create table student2(
        -> id int not null,
        -> name varchar(50) not null,
        -> age int(3) unsigned not null default 18,
      );

   2 、unique 分单列唯一、 多列唯一、组合唯一(联合唯一)

    单列唯一:在创建表时 为某一个字段设置unique 约束

    多列唯一:在创建表时 为对多个字段设置unique约束

    组合唯一

    create table services(
          -> id int,
          -> ip char(15),
          -> port int,
          -> unique(id),
          -> unique(ip,port)
         -> );


    组合唯一:插入两行记录 只要有一行不相同,就符合组合唯一

    insert into services values
        -> (1,'192,168,11,23',80),
        -> (2,'192,168,11,23',81),
        -> (3,'192,168,11,25',80);

   3、primary key:主键,一个表里唯一标示的字段

    not null + unique ==primary key

原文地址:https://www.cnblogs.com/liaopeng123/p/9794669.html