mysql基础

1)数据库操作:

登陆mysql数据库

mysql -u root -p

展示所有数据库

show databases;

新建数据库

create database database_name;

删除数据库

drop database database_name;

2)数据库表操作

选择使用的数据库

use 数据库名

如: use company;

创建表

create table 

create table offices(      

  officeCode int(10) not null unique,      

  city varchar(50) not null,      

  primary key(officeCode)

);

展示该库中所有的表

show tables;

使用主键约束

id int primary key,

primary key(id)

使用外键约束

create tables tb_detp1(

  id int primary key,

  name varchar(22) not null,

  location varchar(50)

);

create table tb_emp5(

  id int primary key,

  name varchar(25),

  deptId int,

  salary float,

  constraint fk_emp_dept1 foreign key(deptId) references tb_dept1(id)

);

唯一约束

唯一约束要求该列唯一,允许为空,但只能出现一次空

字段名 数据类型 unique

例如:name varchar(22) unique

设置默认约束

字段名 数据类型 default 默认值

设置表的属性值自动增加

字段名 数据类型 auto_increment

查看表的数据类型

DESC 表名;

查看表的详细结构语句,

show create table 表名;

可在表名后加(G)现实效果明显

修改表名

alter table 旧表名 rename 新表名;

修改字段的数据类型

alter table 表名 modify 字段名 数据类型

修改字段名

alter table 表名 change 旧字段明 新字段明 新数据类型

添加字段

alter table 表名 add  新字段明 新数据类型

删除字段

alter table 表名 drop 字段名

删除外键约束

alter table 表名 drop foreign key 外键约束名

删除表

drop table 表名

3)数据库运算符

1. 算数运算符

+ - * / % 类似:

 2.比较运算符

3.逻辑运算符

NOT 或者 ! 逻辑非

AND 或者 && 逻辑与

OR 或者 || 逻辑或

XOR 逻辑异或

4.位运算符

|    位或

&   位与

^   位异或

~   取反

<< 左移

>> 右移

4)插入,更新,删除

1.将查询结果插入表

mysql> insert into tmp14(num)
    -> select avg(num) from tmp14;
Query OK, 1 row affected (0.06 sec)
Records: 1  Duplicates: 0  Warnings: 0

2.更新update

mysql> update tmp14 set num=15 where num=14;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

3.删除delete

mysql> delete from tmp14 where num=15;
Query OK, 1 row affected (0.00 sec)
原文地址:https://www.cnblogs.com/yankang/p/6399264.html