数据库_01_增删改

登录:win+r --> cmd --> mysql -hloaclhost -uroot -ppassword
退出:exit 或 quit

一,了解数据库

  1. 数据库:database,保存有组织的数据的容器
  2. 表: table, 某种特定类型数据的结构化清单
  3. 模式: schema, 关于数据库和表的布局及特性信息
  4. 列: column, 表中的一个字段,所有表都是由一个或多个列组成
  5. 行: row, 表中的一个记录
  6. 主键 primarykey 能够唯一区分表中每个行,唯一标识每行的这个列称为主键
  7. 外键 foreignkey
  8. 关键字:keyword 不要用关键字命名一个表或列

主键的好习惯

  • 不更新主键列中的值
  • 不重用主键列的值
  • 不在主键列中使用可能会更改的值

二,SQL:structured query language

三,基本操作

  1. 创建数据库:create database mcc;

  2. 查看数据库:show databases;

  3. 使用数据库:use mcc
    需先用use打开数据库,才能读取该库中的信息

  4. 查看数据库中的表:show tables;
    目前还没有表,创建表

  5. 创建表
    create table customers IF NOT EXISTS(
    cust_id int NOT NULL AUTO_INCREMENT,
    cust_name char(50) NOT NULL,
    cust_address cahr(50) NULL,
    cust_city cahr(50) NULL,
    cust_state cahr(50) NOT NULL DEFAULT 1,
    cust_zip cahr(50) NULL,
    cust_country cahr(50) NULL ,
    cust_contact cahr(50) NULL,
    cust_email cahr(255) NULL,
    PRIMARY KEY (cust_id)
    )ENGINE=InnoDB;

    IF NOT EXISTS :查看表名是否存在,仅在表名不存在时创建
    NULL:允许插入行时不给出该列的值,为默认设置
    NOT NULL:插入行时,该列必须有值
    注:NULL是没有值,‘’空串是一个有效的值
    PRIMARY KEY
    可以在字段后面指定:xxx字段 primary key
    也可以在最后专门一行指定:primary key(xxx)或primary key(xxx,yyy)
    主键是唯一标识,只能使用不允许NULL值的列
    auto_increment:每个表只允许一个此种列,且它必须被索引,如使其成为主键
    插入数据时,由于该字段是自增的且非空的,所以该字段值可以使用NULL
    也可以是插入数据时指出需要插入数据的字段,该auto_increment字段可以不指定
    insert into customers(cust_name,cust_city) values (‘llll’,‘shanghai’)
    default:设置默认值,默认值支持常量,不允许使用函数

  6. 查看表结构
    1)desc test2;
    2)describe test2;
    3)show columns from test2;

  7. 更改表结构
    注一般创建表时应该花大量事件来设计,尽量不更改表结构
    要改的话,最好先进行完整备份,包括模式和数据
    1)alter table customers
    add test_column char(30);
    2)drop column test_column

  8. 删除表:drop table customers;

  9. 重命名表:rename table customers to customers_bak

  10. 插入数据:insert
    1)insert low_priority into test2 values(2,‘lilili’)
    2) 插入多行
    insert into test2 values (1,‘li’);
    insert into test2 values (2,‘lili’);
    3)插入多行
    insert into test2 values (xxx,xxxxx),(yyy,yyyy)
    4)insert select
    insert low_priority into test2(name) select name from test55;
    insert low_priority into test2 select * from test55;
    insert low_priority into test2 select * from test55 where age > 18;

  11. 更新数据:update 也可删除某列数据
    update IGNORE test2 set age = 18;
    update IGNORE test2 set age = 18 where name = ’ lihao ';
    update IGNORE test2 set age = 18 ,id = 666 where name = ’ lihao ';
    删除某列的值,可将其设置为NULL
    update test2 set age=NULL;

  12. 删除数据:delete 删除某行数据
    delete from test2 where name=‘lihao’;
    delete from test2;
    truncate table test2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值