标签:ase 单引号 mysql常用命令 arc match 双引号 from 用户操作 内存
通过mysql客户端进入mysql后,实际位于所有仓库之外:
* 选中要操作的数据库实例
* 操作数据库实例中的表或其他实体.
注意: ";"表示一条SQL命令的结束,当命令结束后才会执行此命令
* 登陆数据库
建表:
? 命令:create table <表名> ( <字段名> <类型> [,..<字段名n> <类型n>]);
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default ’′,
> degree double(16,2));
获取表结构:
? 命令:desc 表名,或者show columns from 表名
mysql>DESCRIBE MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
删除表:
? 命令:drop table <表名>
mysql> drop table MyClass;
插入数据:
? 命令:insert into <表名> [( <字段名>[,..<字段名n > ])] values ( 值 )[, ( 值n )]
mysql> insert into MyClass values(1,’Tom’,96.45),(2,’Joan’,82.99), (2,’Wang’, 96.59);
查询表中的数据:
? 查询所有行
? 命令:select <字段,字段,...> from < 表名 > where < 表达式 >
mysql> select * from MyClass;
? 查询前几行数据
mysql> select * from MyClass order by id limit 0,2;
或者
mysql> select * from MyClass limit 0,2;
? 命令:update 表名 set 字段=新值,…where 条件
mysql> update MyClass set name=’Mary’where id=1;
删除表中数据
命令:delete from 表名 where 表达式
mysql> delete from MyClass where id=1;
在表中增加字段
命令:alter table 表名 add字段 类型 其他;
例如:在表MyClass中添加了一个字段passtest,类型为int(4),默认值为
mysql> alter table MyClass add passtest int(4) default ’′
删除字段
命令:alter table 表名 drop column 字段名;
alter table `t1` drop column money;
更改表名:
命令:rename table 原表名 to 新表名
例如:在表MyClass名字更改为YouClass
mysql> rename table MyClass to YouClass;
为什么约束条件?
统计社区用户的个人信息: 姓名,性别,年龄,地址
张三---> 张三 你猜 10000 火星
请问以上数据有意义? 对以上数据能够添加到数据库?
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into user values(1,'张三','你猜',10000,'火星');
Query OK, 1 row affected (0.06 sec)
mysql> select * from user;
+------+------+------+-------+---------+
| id | name | sex | age | address |
+------+------+------+-------+---------+
| 1 | 张三 | 你猜 | 10000 | 火星 |
+------+------+------+-------+---------+
为了方便数据的管理和实现数据的有效性,通常使用约束条件控制用户操作的数据.
mysql含有哪些约束条件?
主键约束 唯一约束 默认约束 非空约束 检查性约束 外键约束
说明: oracle约束条件类型和使用方式,与mysql完全相同
主键约束:
用于标示表中的数据.[通常要求主键字段没有实际意义,可有可无]
字段名 字段类型 primary key
使用了主键约束的字段,字段值非空且唯一.
create table t1(
id int primary key,
name varchar(20)
);
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into t1 values(1,'AA');
Query OK, 1 row affected (0.05 sec)
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | AA |
+----+------+
1 row in set (0.00 sec)
mysql> insert into t1 values(1,'BB');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
字段名 字段类型 unique
使用了唯一约束的字段,字段值不能重复[可以为空]
drop table if exists t1;
create table t1(
id int primary key,
name varchar(20) unique
);
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into t1 values(1,'AA');
Query OK, 1 row affected (0.05 sec)
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | AA |
+----+------+
1 row in set (0.00 sec)
mysql> insert into t1 values(2,'AA');
ERROR 1062 (23000): Duplicate entry 'AA' for key 'name'
mysql> insert into t1 values(2,'BB');
Query OK, 1 row affected (0.06 sec)
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | AA |
| 2 | BB |
+----+------+
2 rows in set (0.00 sec)
字段名 字段类型 default ‘默认值‘
使用了默认约束的字段,当添加数据时未给该字段赋值时,该字段自动使用默认值作为字段值
drop table if exists t1;
create table t1(
id int primary key,
name varchar(20) unique,
sex char(2) default '男'
);
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
| sex | char(2) | YES | | 男 | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.05 sec)
mysql> insert into t1 values(1,'AA','女');
Query OK, 1 row affected (0.05 sec)
mysql> select * from t1;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | AA | 女 |
+----+------+------+
1 row in set (0.00 sec)
mysql> insert into t1 values(2,'BB');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into t1(id,name) values(2,'BB');
Query OK, 1 row affected (0.36 sec)
mysql> select * from t1;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | AA | 女 |
| 2 | BB | 男 |
+----+------+------+
字段名 字段类型 check(条件)
使用了检查性约束的字段,字段值必须满足相应条件.
mysql中含有检查性约束,但是不强制执行. oracle中会强制执行.
drop table if exists t1;
create table t1(
id int primary key,
name varchar(20) unique,
sex char(2) default '男',
age int check(age>18)
);
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
| sex | char(2) | YES | | 男 | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
mysql> insert into t1 values(1,'AA','男',20);
Query OK, 1 row affected (0.08 sec)
mysql> select * from t1;
+----+------+------+------+
| id | name | sex | age |
+----+------+------+------+
| 1 | AA | 男 | 20 |
+----+------+------+------+
1 row in set (0.00 sec)
mysql> insert into t1 values(2,'BB','男',9);
Query OK, 1 row affected (0.36 sec)
mysql> select * from t1;
+----+------+------+------+
| id | name | sex | age |
+----+------+------+------+
| 1 | AA | 男 | 20 |
| 2 | BB | 男 | 9 |
+----+------+------+------+
2 rows in set (0.00 sec)
字段名 字段类型 not null
使用了非空约束的字段,当添加数据时必须设置该字段的值[或者含有默认约束]
drop table if exists t1;
create table t1(
id int primary key,
name varchar(20) unique,
sex char(2) default '男',
age int check(age>18),
address varchar(50) not null
);
mysql> desc t1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
| sex | char(2) | YES | | 男 | |
| age | int(11) | YES | | NULL | |
| address | varchar(50) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into t1 values(1,'AA','男',20,'郑州');
Query OK, 1 row affected (0.05 sec)
mysql> select * from t1;
+----+------+------+------+---------+
| id | name | sex | age | address |
+----+------+------+------+---------+
| 1 | AA | 男 | 20 | 郑州 |
+----+------+------+------+---------+
1 row in set (0.02 sec)
mysql> insert into t1(id,name,sex,age) values(2,'BB','男',20);
ERROR 1364 (HY000): Field 'address' doesn't have a default value
编写一张数据表: 含有ID,部门,姓名,工资.
部门名称不限,员工姓名不限,工资不限.
1.添加10个员工
2.修改技术部的员工的工资为5000
3.查询营销部员工的姓名和工资
4.由于经营原因,公司裁员,将工资高于4500的员工辞去.
5.查询当前公司的所有信息
create table t1(
id int(10),
section varchar(10),
name varchar(10),
money int(10)
);
insert into t1 value(1,"技术部","张三",8000);
insert into t1 value(2,"营销部","马三",8000);
update t1 set money=5000 where section="技术部";
select name,money from t1 where section="营销部";
delete from t1 where money > 4500;
select * from t1;
2. 约束练习
创建一个客户表,含有这些字段:id(主键) name(非空) sex(默认为不详) age(25-50) cu_num(唯一,且不能为空).
添加5个客户.
查询所有客户的信息
由于打字员的疏忽,错把cu_num=c1003的性别写为男,请改正.
由于工作关系,客户c1001逃跑了,请处理.
查询目前的客户信息.
create table kehu(
id int(10) primary key,
name varchar(10) not null,
sex char(4) default '不详',
age int(5) check(25<age<50),
cu_num int(5) unique);
insert into kehu value(1,"张三","男",18,1);
insert into kehu value(2,"王三","男",18,2);
insert into kehu value(3,"李三","男",18,3);
insert into kehu value(4,"马三","男",18,4);
insert into kehu value(5,"吕三","男",18,5);
select * from kehu;
update kehu set sex = "女" where cu_num = 3;
delete from kehu where id = 1;
创建一张学生表,含有id(主键,自增长),name(唯一),age(大于18小于60),sex(默认为男),班级
创建一张班级表,含有id(主键,自增长),班名
create table student(
id int primary key auto_increment,
name varchar(20) unique,
age int check(age>18&&age<60),
sex char(2) default '男',
class int
);
create table class(
id int primary key auto_increment,
classname varchar(20)
);
查询每个学生属于哪个班?
select name,className from student,class where class=class.id;
新来了一个学生,请班长将学生添加到学生表,请问该学生可以是哪些班的?
insert into student(name,sex,age,class) values(‘小三‘,‘男‘,22,5);
alter table 表名 add [constraint 约束匿名] foreign key(外键字段) references 主表名(被引用字段);
# 字段名 字段类型 约束,
.....
[constraint 约束匿名] foreign key(外键字段) references 主表名(被引用字段)
);
mysql> alter table student add foreign key(class) references class(id);
Query OK, 4 rows affected (0.25 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | UNI | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | 男 | |
| class | int(11) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+----------------+
student_ibfk_1
;https://blog.csdn.net/moxigandashu/article/details/63254901
标签:ase 单引号 mysql常用命令 arc match 双引号 from 用户操作 内存
原文地址:https://www.cnblogs.com/mengd/p/12051483.html