数据库大整合

Mysql

为了使用mysql,得先开启mysql的服务

找到bin/mysqld运行即可(win7以上系统,你要以管理员权限运行)

请使用cmd命令行登录mysql   输入命令:mysql -uroot -p 就可以root身份登录

常见面试题:CHAR、VARCHAR的区别

(今天出现在这份文档里的所有内容语句,都要全部背出来,一个字母都不能错)

1.创建数据库

create database aaa;

2.查看所有数据库

show databases;

3.删除数据库

drop database aaa;

4.选择数据库

use aaa;

5.创建表

create table students(

    id int,

    name varchar(20)

);

6.查看所有表

show tables;

7.查看表的结构

desc students;

8.删除表

drop table students;

9.Insert into插入数据

Insert into students(id,name) values(1,’zhangsan’);

Insert into students values(2,’xiaohong’);

10.查看表中所有数据

Select * from students;

Select id,name from students;

11.根据条件查找(where子句)

Select * from students where id=1;

12.Update set改

Update students set name=’miaomiao’ where id=1;

13.Delete 删除

Delete from students where id=1;

14.两张表的联合查询

select stu.id,name,age,score from students stu,Scores sc where stu.id=sc.id;

数据库的导入导出(比如质检时你如何将数据库给老师去批改呢?)

导出整个数据库

mysqldump -u 用户名 -p 数据库名 > 导出的文件名

mysqldump -u dbuser -p dbname > dbname.sql

数据库约束

 更改UTF8格式

案例:需要大伙创建两张表

students,有id(int),name(varchar(20))和age(int)字段

scores,有id(int),score(int)字段

向上面两张表中插入数据,得到结果

Drop table students;

Create table students(id int,name varchar(20),age int);

Create table scores(id int,score int);

Insert into students values(1,'xiaohong',15);

insert into students values(2,'xiaobao',17);

insert into students values(3,'xiaoai',16);

insert into scores values(1,80),(2,90),(3,70);

Students

Id    name     age

1     xiaohong  15

2     xiaobai   17

3     xiaoai    16

Scores

Id    score  

1     80

2     90

3     70

 mysql> select stu.id,name,age,score from students stu,scores sc where stu.id=sc.id;

要求这道题,给我查询出如下结果

Id    name    age   score

1     xiaohong  15    80

2     xiaobai   17    90

3     xiaoai    16    70

要求这道题,给我查询出如下结果

s_id    name    age   score

1     xiaohong  15    80

2     xiaobai   17    90

3     xiaoai    16    70

 Create table   test(

  Id  int primary   key   auto_increment

Name  varchar(20)

);

猜你喜欢

转载自www.cnblogs.com/yangshuyuan1009/p/10119790.html