操作库:
数据库的建立:Create database 数据库名;
显示库:Show databases;
删除库:Drop database 数据库名;
操作表:
在操作表之前使用需要先确定使用那个数据库:use 数据库名
表的建立:
Create table 表名(
列名1 列类型<列的完整性约束>,
列名2 列类型<列的完整性约束>,
……
)
查看所有表:show tables;
显示表结构操控:desc 表名
修改表名:rename table 原表名 to 新表名;
删除表:drop table 表名;
操作列:
追加列:alter table 表名 add 列名类型;
修改列类型:alter table 表名 modify 列名类型;
修改列:alter table 表名 changecolumn 原列名 新列名 类型;
删除列:alter table 表名 drop 列名;
操作数据(增删改)
插入:insert into 表名 (列名类型) values (值);
修改: update 表名 set 列名=值 where 条件语句;
删除:delete from 表名 where 条件语句;
操作数据库(查)
Distinct 为:过滤重复记录.
Select distinct 列名 from 表名
查询列名表达式:select 列名|表达式 from 表名;
表与表之间联系
即表与表之间通过主键与外键连接产生的联系
举例说明:
两个表,一个Student表,一个Teacher表
//创建学生表,定义学号,姓名
Create table student(
Id int(11) not nullauto_primary key, //将学号id设为主键
Name varchar(20)
);
//创建教师表,定义教师号,姓名
Createtable teacher(
Idint(11) not null auto_primary key, //教师号id设为主键
Namevarchar(20),
Student_idint, //建立外键,此时Student_id 就是Student表中的id 。
ConstraintStudent_id_fk foreign key(Student_id) references Student(id)m //将教师表中的Student_id 外键与Student表中的id主键联系起来
);