zoukankan      html  css  js  c++  java
  • MySQL 命令整理1

    1. 数据库登陆操作:
    数据库登陆

    mysql -hlocalhost -p3306 -uroot -p
     

    本地登陆:

    mysql -u root -p

    2. 数据库管理操作:
    查看所有数据库:

    show databases;

    新建数据库,并设定数据库编码为utf8:

    create database db character set utf8;

    修改数据库编码为utf8:

    alter database db charset=utf8;

    显示创建数据库信息:

    show create database db;

    删除数据库:

    drop database db; 
    drop database if exists db;

    3. 数据表管理操作
    查看数据表:

    show tables;

    创建表:

    create table stu(字段名 int, name char(20), 字段名 int);

    显示创建表信息:

    show create table 表名; 

    增加字段:

    alter table 表名 add 字段名 char(4);

    删除字段:

    alter table 表名 drop 字段名;

    修改字段的数据类型:

    alter table stu modify name varchar(20);

    修改字段的数据类型并更改字段名称:

    alter table stu change id number smallint

    删除数据表:

    drop table if exists stu;

    4. 数据管理操作:
    数据查询:

    select * from stu;

    插入数据:
    a.插入所有字段

    insert into stu value(1, 'tom', 20);

    b.插入指定字段

    insert into stu(id, name) value(2, 'jack');

    c.插入多条数据

    insert into stu value(3, 'rose', 18),(4, 'john', 22);

     修改数据:

    a.全部修改

    update stu set age=25;

    b.选择性修改

    update stu set name='alice' where name='jack';

    改字段,基本语法:

    alter table + 表名 + modify + 字段名 + 数据类型 + [列属性][位置]; 

    示例:

    alter table student modify name char(10) after id;

    删除数据:

    a.删除全部数据,且无法恢复

    truncate stu;

    b.删除全部数据,可恢复

    delete from stu;

    c.删除满足条件的数据

    delete from stu where id=1;

     5. 数据库约束

    主键约束

    create table tpk(id int primary key, name char(10));

     自动增长

    create table tpk(id int auto_increment primary key, name varchar(10))

    唯一性约束:

    create table tpk(id int unique, name char(10));

    非空约束:

    create table tpk(id int, name char(10) not null);

     默认约束:

    create table tpk(id int, name char(10) default 'NoName');

     外键约束:

    create table fClass(id int primary key, name char(10));
    create table fStudent(id int primary key auto_increment, name char(10), cid int, foreign key(cid) references fClass(id));

    删除外键关联表(先删除外键关联的表格)

    drop table fStudent;
    drop table fClass; 

    检查约束:

    create table tchk(id int,age int check(age > 0 and age < 150), gender char(10) check(gender='boy' or gender='girl'));

    增加主键约束:

    alter table tpk add constraint PK_id primary key(id);

    删除主键约束:

    alter table tpk drop primary key;

    添加外键约束:

    alter table tfk add constraint FK_id foreign key(id) references tpk(id);

    删除外键约束:

    alter table tfk drop foreign key FK_id
    Thought is already is late, exactly is the earliest time.
  • 相关阅读:
    IMDB情感分类学习
    torchtext入门学习
    3.1日学习笔记|3.2日学习笔记
    2.24日学习笔记|2.26日学习笔记|2.27|2.28
    快慢指针问题
    2.21日学习笔记|2.22日学习笔记|2.23学习笔记
    dinic模板
    P1247 取火柴游戏 博弈nim
    博弈论
    P2161 [SHOI2009]会场预约 树状数组
  • 原文地址:https://www.cnblogs.com/aaxwhxwh/p/9256856.html
Copyright © 2011-2022 走看看