数据库
首页 > 数据库> > mysql数据具体操作

mysql数据具体操作

作者:互联网

1.建表操作

前面提到的是简单的建表,这里需要提到一下外键。

create table userinfo2(
                    id int auto_increment primary key,
                    name char(10),
                    gender char(10),
                    email varchar(64)
                )engine=innodb default charset=utf8;

                create table host(
                    id int auto_increment primary key,
                    hostname char(64)
                )engine=innodb default charset=utf8;


                create table user2host(
                    id int auto_increment primary key,
                    userid int not null,
                    hostid int not null,
                    unique uq_user_host (userid,hostid),
                    CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),
                    CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)
                )engine=innodb default charset=utf8;
View Code

补充:建表时还可以设置自增列从几开始

CREATE TABLE s2(
    tid INT auto_increment PRIMARY KEY,
    tname VARCHAR(32)
)ENGINE=INNODB auto_increment=4 DEFAULT charset=utf8;
View Code

如果建表时没有设置,后续可以修改

ALTER TABLE s2 AUTO_INCREMENT = 100;
View Code

2.增

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 …;

INSERT INTO s2(name) SELECT tname FROM teacher;
View Code

3.删

delete from tb12;
delete from tb12 where id !=2 
delete from tb12 where id =2 
delete from tb12 where id > 2 
delete from tb12 where id >=2 
delete from tb12 where id >=2 or name='alex'
View Code

4.改

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

示例:
    UPDATE mysql.user SET password=password(‘123’) 
        where user=’root’;
View Code

5.查

查是重点另开一贴再写

 

标签:Code,数据,tb12,表名,字段,具体操作,mysql,where,id
来源: https://www.cnblogs.com/xufengnian/p/10579267.html