六 .数据库(表的记录操作)

一 .MySQL记录操作

1 .数据插入(insert into)

1. 插入完整数据(顺序插入)
    语法一:
    INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n);

    语法二:
    INSERT INTO 表名 VALUES (值1,值2,值3…值n);

2. 指定字段插入数据
    语法:
    INSERT INTO 表名(字段1,字段2,字段3…) VALUES (值1,值2,值3…);

3. 插入多条记录
    语法:
    INSERT INTO 表名 VALUES
        (值1,值2,值3…值n),
        (值1,值2,值3…值n),
        (值1,值2,值3…值n);
        
4. 插入查询结果
    语法:
    INSERT INTO 表名(字段1,字段2,字段3…字段n) 
                    SELECT (字段1,字段2,字段3…字段n) FROM 表2
                    WHERE …;

 2. 更新数据(update)

语法:
    UPDATE 表名 SET
        字段1=值1,
        字段2=值2,
        WHERE CONDITION;

示例:
    UPDATE mysql.user SET password=password('123') 
        where user='root' and host='localhost';

   update runoob_tbl set runoob_title='学习 C++' where runoob_id=3;
  从结果上看,runoob_id 为 3 的 runoob_title 已被修改。

 3. 删除数据(delete from)

语法:
    DELETE FROM 表名 
        WHERE CONITION;

示例:
    DELETE FROM mysql.user 
        WHERE password='';


delete from runoob_tbl where runoob_id=3;
将删除 runoob_tbl 表中 runoob_id 为 3 的记录:

创建数据表 :  
               create table  表名();
               create table  表名(id int,username varchar(20),age int);
               create table student(id int,username varchar(20),age int);
               create table huiyuan(id int,username varchar(20),age int);
               create table hh(id int,username varchar(20),age int);


记录的操作:
             新增记录 :
              insert into 表名(字段列表) values(值的列表)    注意:值必须与字段列表一一对应;
              insert into student(username) values('张三');
              insert into student(username,age) values(张三,25);
              insert into student(id,username,age) values(3,张三,25);
              
              insert into student vales(4,老五,24);值的个数与数据表中字段的个数完全一致时 可以忽略不写.....


查看表:
     show tables  查看所有数据表
     
     show create table (数据库表名)student;            查看数据表的建表语句 
     
     desc  (数据库表名)student;                        查看表结构
     desc  users;   


删除表:
        drop table (数据库表名)student;
        
修改表:
        alter table (数据库表名)student;    可以修改默认引擎  和字符集  
        alter table (数据库表名)student engine=innodb;    可以修改默认引擎  和字符集  
        alter table student charset=utf8;    可以修改默认引擎  和字符集 
        rename table  (数据库表名)student to (数据库表名1)student2;  修改数据表名
        
        
        
对字段操作:

        创建字段在创建数据表时进行   ,在已经拥有了表名之后 还可以对字段进行操作
        create table 表名(字段名 字段类型 字段属性,字段名2 字段类型 字段属性,.......);     
        create table  表名(id int,username varchar(20),age int);
        create table classes(id int,clsaaname varchar(10),classroom varchar(3));
        create table hello(id int,clsaaname varchar(10),classroom varchar(3));
        
增加新字段:    
        alter table (数据库表名)student add column(行) gender(性别)  varchar(4); 意思在student数据表中新增 gender字段  
        alter table info add column namee varchar(2);
        

修改字段类型和属性  modify 修改

alter table student(数据表名) modify column gender(性别) varchar(
10);修改gender字段定义 alter table student modify column gender varchar(10); 修改字段名字 alter table student change column gender sex varchar(2); 删除字段 alter table student drop column gender;

 set names gbk;  设置 编码

猜你喜欢

转载自www.cnblogs.com/Sup-to/p/11235324.html