标签:特定 creat and 数据库 mys query tab pac charset
create table class(
id int primary key auto_increment,
sname varchar(10) not null default ‘‘,
gender char(1) not null default ‘‘,
company varchar(20) not null default ‘‘,
salary decimal(6,2) not null default 0.00,
fanbu smallint not null default 0
)engine myisam charset utf8;
mysql> desc class;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(10) | NO | | | |
| gender | char(1) | NO | | | |
| company | varchar(20) | NO | | | |
| salary | decimal(6,2) | NO | | 0.00 | |
| fanbu | smallint(6) | NO | | 0 | |
+---------+--------------+------+-----+---------+----------------+
6 rows in set
insert into class
(id,sname,gender,company,salary,fanbu)
values
(1,‘张三‘,‘男‘,‘百度‘,8888.67,234);
**添加部分属性**
insert into class
(sname,gender,salary)
values
(‘刀锋‘,‘男‘,8765.43);
**添加所有属性,第2行可省略**
insert into class
values
(3,‘李四‘,‘女‘,‘新浪‘,5678.99,125);
**添加多行数据**
insert into class
(sname,company,salary)
values
(‘刘备‘,‘皇室成员‘,15.28),
(‘孙策‘,‘江东集团‘,56.34),
(‘曹操‘,‘宦官后裔‘,88.56);
mysql> select * from class;
+----+-------+--------+----------+---------+-------+
| id | sname | gender | company | salary | fanbu |
+----+-------+--------+----------+---------+-------+
| 1 | 张三 | 男 | 百度 | 8888.67 | 234 |
| 2 | 刀锋 | 男 | | 8765.43 | 0 |
| 3 | 李四 | 女 | 新浪 | 5678.99 | 125 |
| 4 | 刘备 | | 皇室成员 | 15.28 | 0 |
| 5 | 孙策 | | 江东集团 | 56.34 | 0 |
| 6 | 曹操 | | 宦官后裔 | 88.56 | 0 |
+----+-------+--------+----------+---------+-------+
6 rows in set
记住添加元素的3大要素:
往哪张表添加行: class
添加哪几列:(sname,gender,salary)
分别添加的值是:(‘刀锋‘,‘男‘,8765.43);
注意:
1、即使是自增型数据,也是需要赋值,在insert中列与值要严格对应,列与值必须一一对应,即使用逗号隔开也不行
但是可以用null来占位,但是不建议这样,会出现兼容性等问题。
insert into class
values
(‘李四‘,‘女‘,‘新浪‘,5678.99,125);
**全部都改了**
update class
set
gender=‘女‘,
company=‘千度‘;
**只改特定行**
update class
set fanbu=123
where id=6;
**选择其他属性值作为确定行的关键字**
update class set gender=‘男‘,fanbu=‘212‘
where sname=‘孙策‘;
**改性别为男,且工资>8000的用户**
update class set fanbu=159
where gender=‘男‘ and salary>8000;
**当表达一直为真,则全改**
update class set fanbu=99 where 1;
**删除salary>8888的用户**
delete from class where salary>8888;
**删除salary大于8000,且性别为女的用户
delete from class where salary>8000 and gender=‘女‘;
**删除表中所有数据,与truncate比较**
delete from class;
**查找特定行,特定列的数据**
select sname,company,salary from class where id=6;
**查找表中所有数据**
*代表所有列,表名后不加where条件,则选择所有行
select * from class;
**部分列,所有行,取所有的人的姓名和工资**
select sname,salary from class;
**查id>3的人的所有列**
select * from class where id>3;
**取部分行部分列**
select sname,fanbu from class where id < 5;
以下为在MySQL数据库中查询数据通用的 SELECT 语法:
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]
标签:特定 creat and 数据库 mys query tab pac charset
原文地址:https://www.cnblogs.com/Stephanie-boke/p/11664477.html