MySQL(二)

数据之表操作

    1.创建表

  语法:CREATE TABLE table_name (column_name column_type);

CREATE TABLE student(
    -> id INT NOT NULL AUTO_INCREMENT,
    -> name CHAR(32) NOT NULL,
    -> age INT NOT NULL,
    -> register_data DATE,
    -> PRIMARY KEY(id)
    -> );

  auto_increment 表示:自增1。写入内容为空时,默认从1,2,3...往下填充写入表格中。primary key: 表示约束(不能重复且不能为空); 加速查找not null: 不为空

    2.查看表

show tables;      -->查看有哪些表
desc student;     --> 查看student表的信息
show create table student;    -->查看表student创建的信息

    3.删除表

#drop table 表名

drop table student;

    4.修改表

方法:

添加列:alter table 表名 add 列名 类型
删除列:alter table 表名 drop column 列名
修改列:
        alter table 表名 modify column 列名 类型;  -- 类型
        alter table 表名 change 原列名 新列名 类型; -- 列名,类型
   
添加主键:
        alter table 表名 add primary key(列名);
删除主键:
        alter table 表名 drop primary key;
        alter table 表名  modify  列名 int, drop primary key;
   
添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除外键:alter table 表名 drop foreign key 外键名称
   
修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
1.增加
ALTER TABLE student ADD sex CHAR(32);    #-->增加一列

2.删除
ALTER TABLE student DROP sex;     #-->删除一列

3.修改表名
ALTER TABLE student RENAME TO students;   #-->重命名

4.修改列名
ALTER TABLE students CHANGE regisiter_date register_date DATE;

#change 字段名,类型都可以改,modify只能改类型

表内容操作

    1.插入数据

  语法: INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );

  插入数据:

INSERT INTO students(name,age,register_dates)  VALUES('derek',22,'2017-01-01');
INSERT INTO students(name,age,register_dates)  VALUES('jack',20,'2017-03-03');

  

原文地址:https://www.cnblogs.com/Black-rainbow/p/9168967.html