zoukankan      html  css  js  c++  java
  • sql 表,字段(列),表数据(行)相关命令

     随便转载,保留出处:http://www.cnblogs.com/aaron-agu/  

    注: 以下所有操作都在登陆数据库后执行 命令use test;  test为数据库名

    查看表

    show tables;
    #如果没有登陆数据库 show tables
    from test;

    新建表

    create table student_t(
    id int primary key auto_increment, --(设置主键、自增) student_name
    varchar(20), student_brithday datetime, student_phone int, student_score float, ) AUTO_INCREMENT = 100 ; --(设置自增ID从100开始)

    create table task_check_info(
    id int primary key auto_increment,
    task_id_id int,
    type smallint(6),
    intro varchar(255),
    status tinyint(1),
    foreign key(task_id_id) references task(id) --(外键)
    );

    student_t 为表名
    student_name 第一个字段(列);字段数据类型varchar; primary key(主关键字)是表中的一个或多个字段,它的值用于唯一地标识表中的某一条记录。在两个表的关系中,主关键字用来在一个表中引用来自于另一个表中的特定记录。主关键字是一种唯一关键字,表定义的一部分。一个表不能有多个主关键字,并且主关键字的列不能包含空值。主关键字是可选的,并且可在 CREATE TABLE 或 ALTER TABLE 语句中定义。
    注意:
    student_name,student_phone,等字段前最好不要有空格等,不然数据库会把空格加student_name整个作为第一个字段。

    修改表名

    alter table student_t rename to students;

    删除表

    drop table students;

    查看列数据(各字段的详细属性)

    desc students;    #已有的属性
    show full columns from students;    #所有属性

      field:字段

      type: 格式

      collation: (核对)编码格式

      null: 是否可以为空

      key: 关键字MySQL中有四种Key: Primary Key, Unique Key, Key 和 Foreign Key。

        Foreign Key 就是表与表之间的某种约定的关系,由于这种关系的存在,能够让表与表之间的数据,更加的完整,关连性更强。foreign key是一列或多列的组合, 其定义必须和父表的父键保持一致。每一个非空的foreign key都必须在父表的父键里面找到对应的值

        剩下的三种都要在原表上建立索引。

        Primary Key的提出就是为了唯一标示表中的字段,就像我们的身份证号一样。此外,所有字段都必须是not null。

        Unique Key则是为了保证表中有些字段是唯一的。比如有些单位领导叫“张三”,所以下面招人的时候是决不可招一个有同样名字的。

        Key其实某个字段标记为Key,是不能保证这个字段的值在表中是唯一出现的。它的目的就是建立索引。

      default: 默认值

      extra: 额外的

      privileges  权限

      comment  补充

    增加一个字段

    alter table students add student_sex char(10) not null after student_name;
    alter table students add student_sex char(10) default '' after student_name;

    只修改字段的某一属性

    alter table students modify column student_name char(30);

    修改字段

    alter table students change student_name student_N varchar(9) not null;

    删除字段

    alter table students drop column student_sex;

    查看表数据

    select * from students;    #查看所有字段
    select student_name, student_phone from students; #只看两给字段

    插入一条数据

    insert into students(student_name, student_phone) valus('aaron', 911);

    更新一条数据

    update students set student_phone=110 where student_name='aaron';

    删除一条数据

    delete from students where student_name='aaron';


    联合查询

    SELECT `order`.id, `order`.title, task.id, task.title, task.pretend FROM `order` JOIN task on task.id=`order`.task_id_id WHERE task.pretend>0;

    在原字段上追加,联合查询

    UPDATE `order` SET `order`.title=concat(`order`.title, '-补贴') where `order`.task_id_id in (select id from task where pretend>0);

    修改原字段

    UPDATE `order` SET `order`.title=replace(`order`.title, '-补贴-补贴-补贴', '') WHERE `order`.task_id_id in (select id from task where pretend>0);


    文章出处:http://www.cnblogs.com/aaron-agu/ 只有毅力和决心才能使人真正具有价值!
  • 相关阅读:
    plsql Developer11的工具栏没有了如何找回来
    postman发送HTTP请求自动生成MD5/SHA1签名
    Redis的安装+哨兵模式+集群
    SpringBoot笔记 --- @JsonFormat和@DateTimeFormat的作用
    HBase异常 -- hbase list报错 ERROR: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing
    Windows使用进阶
    SpringBoot笔记 -- @RequestParam、@RequestBody、@PathVariable、@param
    SpringBoot笔记 -- 注解
    Mybatis笔记 -- 批量操作(查询、插入、更新、删除)
    Git异常 -- 汇总
  • 原文地址:https://www.cnblogs.com/aaron-agu/p/5271122.html
Copyright © 2011-2022 走看看