数据库的概述
数据库的基本概念
- mysql -u root -p密码//进入MySQL
show databases;//进入MySQL,这个地方得有分号,显示所有的数据库。
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.16 sec)
- use mysql;//进入相应的文件
- show tables;//显示所有列表
- desc/ t1/显示内容,格式
- \q//退出
- select *from user;/显示user里面的一堆数据-
- create database test01;//建造一个库
- create table data11(showtime time);//创建一个名为data11的文件,列名为showtime,类型为time
- insert into data11 values('11:11:11'),('11:11'),(111111);//向data11插入值,三组值
- insert into data12(f_data,f_time)values('1978-4-6',123212),(650503,'3:4:1');
向f_data,f_time两列中插入
在插入数据的时候加入一些条件:
- create table t2(num1 int not null,num2 int);//意思就是如果你么有在num1插入值,他就会给你默认值0;如果你没有在num2插入值,他就会默认为null。
- create table t3(num1 int default 100,num2 int);//如果没在num1插入值,那么就是100.
- create table t6(num1 int unique,num2 int);//num1的唯一不能重复
- create table t6(num1 int auto_increment unique not null,num2 int);//num1自增
主键和外键

- create table student(
-> sid int not null auto_increment,
-> name varchar(20) not null,
-> primary key(sid)
-> );//创建主键 - insert into student values(1,'alex');//这里的姓名需要单引号。
create table score(
-> cid int not null auto_increment primary key,
-> score int,
-> sid int,
-> foreign key(sid) references student(sid)
-> );//创建外键
删除

create table t (name char(20),id int) default charset=utf8;//后面这段话可以让你在插入表的时候输入中文
修改table
- alter table student add adress int afert name;//在table student里面加地址栏,它的类型是int,在name后面
- alter table student alter address set default 100 ;//把address的默认改成100.
- alter table student change adress address char(20);//把adress改名为address,并且其类型为int。
- alter table student modify address int;//modefy只能改变类型,不能改变名字
- alter table student rename as stu;//给表名重命名
- alter table jibenxinxi add id int primary key auto_increment;//新加一列名字为id;主键;自增
比较两个数据大小
mysql> select 'x'<>'X';//比较大小x是否为不同。如果binary,表示区分大小写
+----------+
| 'x'<>'X' |
+----------+
| 0 |
+----------+
判断x是否在a和z之间
mysql> select 'x' between 'a' and 'z';
+-------------------------+
| 'x' between 'a' and 'z' |
+-------------------------+
| 1 |
+-------------------------+
<=>安全比较
mysql> select 0 <=> null;
+------------+
| 0 <=> null |
+------------+
| 0 |
+------------+
网友评论