• MySQL常用sql语句-----数据表的增删改操作


    常用sql操作如下:

    1.查看当前数据库的所有表
    show tables;

    2.创建表
    create table stu(sid int,sname char(20),sage int default 20 );

    3.查看表的创建信息
    show create table stu;

    4.查看表的字段信息
    desc stu;

    5.添加字段
    alter table stu add gender char(4);

    6.删除字段
    alter table stu drop gender;

    7.修改某个字段的数据类型
    alter table stu modify sname varchar(20);

    8.修改某个字段的名和数据类型
    alter table stu change sid snumber smallint;

    9.查询数据(记录)初体验
    select * from stu;

    10.插入数据(记录)
    insert into stu values(1, "tom",20);
    insert into stu (snumber,sname) values(2,"jack);
    insert into stu values(3,"jane",18),(4,"Tim",19),(5,"kangkang",27);

    11.修改数据(记录)
    update stu set sage = 20;

    12.修改满足条件的记录
    update set stu sname = "michael" where sname = "kangkang";

    13.删除数据(记录)
    delete from stu where snumber =1;

    14.添加主键约束
    <1>建表时添加:

    create table tpk(id int primary key,name char(20));
    <2>建表时没添加,建表后添加<PK_id:主键名,一般以PK_开头>
    create table t_test(id int ,name char(20));
    alter table t_test add constraint PK_id primary key(id);

    16.删除主键约束
    alter table t_test drop primary key;

    17.设置自动增长约束<必须依赖主键存在>
    create table t_test2(id int primary key auto_increment,name char(20));

    18.设置非空约束
    create table tnn(id int ,name char(10) not null);

    19.设置默认约束
    create table tdt(id int ,name char(20) default "NoName");

    20.设置唯一性约束
    create table tun(id int unique, name char(20));

    21.添加外键约束=唯一性+非空
    <1>创建表的时候添加
    create table fClass(id int primary key ,name char(10));
    create table fStudent(id int primary key auto_increment,name char(20),cid int,foreign key(cid) references fClass(id));
    <2>建完表后添加
    alter table tfk add constraint FK_id foreign key (id) references tpk(id);

    22.删除外键
    alter table tfk drop foreign key FK_id;

    如果你和我有共同爱好,我们可以加个好友一起交流!

  • 相关阅读:
    javascript类继承系列一
    Update Statistics用法
    FOR XML PATH
    SQL Server 中WITH (NOLOCK)
    ROW_NUMBER () 与 PARTITION组合拳
    sql脚本的格式
    存储过程
    动态sql
    尽量不要用select into 复制表
    杂谈
  • 原文地址:https://www.cnblogs.com/ywk-1994/p/9543014.html
Copyright © 2020-2023  润新知