创库与建表

数据库内操作:

创建名为school的数据库:

mysql>  create database school;

查看当前所在库: 

 mysql> select database();

查看数据库:

mysql> show databases;

进入数据库:

mysql> use school;

创建表

mysql> create table teacher (

         -> id int auto_increment primary key

         -> name varchar (10) not null,

         -> age int not null,

         -> other varchar(10) null default ''

         ->  );

查看表:

mysql> show tables;

查看表结构:

 mysql> desc teacher;

创建相同表:

mysql> create table new1 select * from teacher;(无主键)

方法二:

mysql> create table新表名 like 旧表名。(复制结构)

Mysql> insert into新表名 select * from 旧表名。(复制数据)   


添加内容:

mysql> insert into teacher (name,age,other) values("www",30,"hhh");

添加一列

alter table teacher add class_id int null;

//增加 teacher 列表 class_id 列  里面可以为空

alter table teacher add id int auto_increment primary key;  (添加了主键)

  查询表内容:

      mysql> select * from teacher;   

      mysql> select * from school.teacher where name="www";

      mysql> select age from school.teacher where name="www";


删除表:

mysql> drop table 表名;

清空表:

      mysql>  delete from 表名;

删除表中数据:

    mysql> delete from 表名 where id > 4;

更新改数据: 

mysql>update 表名 set name=”xiaode” where id = 1;

表里面 把名字改成 xiaode ID是一的那个改


添加字段: mysql>alter table student add column class_id int;



添加外键:

mysql> alter table student add constraint FK_class_id foreign key student(class_id) references class(id);



添加外键:

mysql>alter table从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(从表的列名) references 主表(主键字段名);

删除外键:

mysql>alter table表名 drop foreign key 外键名称;

删除列:

mysql>alter table表名 drop column 列命;

修改列类型:

mysql>alter table表名 modify column 列名 类型;

修改列名类型:

mysql>alter table表名 modify column 原列名 新列名 类型;

分组查询:

mysql>select age from表名 group by age;

查看(一对多关系):

mysql>select * from student a,class b where a.class_id=b.id (and b.name like ‘%1809’);只显示有对应关系的内容。



查看(多对多关系):

mysql> select * from hostlocal a,user b,hostlocal2user c where c.hostlocal_id=a.id and c.user_id=b.id (and a.id=1);


查看sha开头:

mysql>select * from 表 where name like ‘sha%’;

查看id大于5小于9的:

mysql> select * from 表 where id between 5 and 9;

左表查询:

mysql> select * from class left join student on class.id=student.classs_id;

注:左右两表无对应关系的显示为空(左:class右:student)即1901无学生。

多表联合查询:

mysql> select * from class,teacher,student,class2teacher d where student.class_id=class.id and d.teacher_id=teacher.id and d.class_id=class.id;


普通索引:

mysql> create index索引名 on 表名(字段名);(无限制)

删除索引:

mysql>drop index索引名 on 表名(字段名);

唯一索引:

mysql> create unique index索引名 on 表名;(可含null)

删除唯一:

mysql>alter table表名 drop index 索引名;

主键索引:--->被赋予唯一索引属性(不可含null)

组合索引:create index索引名 on 表名(列,列);

查看索引:mysql>show index from表名;

注:where自己的第一个条件的列名必须是组合索引列的最左边的那个。


导入导出:

编辑配置my.cnf文件:在【mysqld】里添加 secure_file_priv = 本地目录(给读写权限)

验证是否生效:

mysql> select @@global.secure_file_priv;

或者:mysql> show variables like“secure_file_priv”;

导出:select * from 库.表 into outfile “导入文件路径”

导出:load data infile‘文件名(含路径)’into table表名 fields terminated by ‘域分隔符’ optionally enclosed by ‘域界定符’ignore 1 lines(忽略第一行)

你可能感兴趣的:(创库与建表)