mysql 第一天

数据的定义语言

首先创建数据库,使用数据库

create database  test;

use test;
 1 #删除数据库
 2 
 3 drop database <数据库的名称>
 4 
 5 ##创建一个数据表
 6  create  tale emp(
 7 pid varchar(10),
 8 pname varchar(10),
 9 pnumber int
10 );
11 
12 ##如果创建好后要进行更改用   alter
13 
14 #添加主键
15 
16 alter table emp modily pid varchar(10) primary key unique;
17 
18 #查看数据表的类型
19 
20 desc emp;
21 
22 #查看已经建立了那些数据表用
23 
24 show tables;   #注意此时用tables
25 
26 #联合主键,,主键可以跟在创建表字段语句的后面也可以是在最后面,主键的类型,单个主键和复合主键

 

  查看所建立的数据库有哪些

show database;

  数据类型有以下几种:

- 唯一约束
-- 创建表的同时添加唯一约束:create table 表名(字段1 数据类型,字段2 数据类型 unique[,…]);

 

-- 默认约束
-- 创建表的同时添加默认约束:create table 表名(字段1 数据类型,字段2 数据类型 not null default '-' [,…]);

 

-- 自增字段
-- 创建表的同时添加自增字段:create table 表名(字段1 数据类型 primary key auto_increment,字段2 数据类型[,…]);只能对于整数类型设置自增字段

create table student1(
sno int primary key not null auto_increment,
sname char(20) ,
ssex char(6) not null
);

  创建好表以后进行更改

-- 修改表名:alter table 原表名 rename 新表名;
-- 将fruits表名修改为fruit

alter table fruits rname fruit;

-- 修改字段类型:alter table 表名 modify 字段名 新数据类型;
-- 将f_name的数据类型改为varchar(20)

alter table fruit modily f_name varchar(200);

-- 修改字段名:alter table 表名 change 原字段名 新字段名 数据类型;
-- 将s_id的字段名改为s_name

alter table fruit change s_id s_name char(9);

-- 添加字段:alter table 表名 add 新字段名 数据类型 约束条件;
-- 添加一个新字段f_num,数据类型为整数

alter table fruit
add f_num int;

-- 修改字段的排列位置:
-- alter table 表名 modify 字段名 数据类型 first;
-- 将s_name改为第一列

alter table fruit modify s_name char(9) first;

-- alter table 表名 modify 要排序的字段名 数据类型 after 参照字段;
-- 将f_num改到f_name后面

alter table fruit modify f_num int after f_name;


-- 删除字段:alter table 表名 drop 字段名;
-- 删除f_num字段

alter table fruit
drop f_num;

  向数据表中插入数据

-- 数据操作语言DML
-- 插入数据:字段名与字段值的数据类型、个数、顺序必须一一对应
-- 指定字段名插入:insert into 表名(字段名1[,字段名2,...]) values(字段值 1[,字段值 2,...]);
-- ('a1',101,'apple',5.2);
-- ('as1','orange');


insert into fruit(s_name,f_id,f_name,f_price)
values('a1',101,'apple',5.2);

insert into fruit (s_name,f_name) 
values('bs1','orange');


insert into fruit values
('bs2',105,'melon',8.2),
('a1',104,'banana',10.3),
('a2',106,'grape',5.3),
('a2',107,'coconut',9.2),
('b1',108,'cherry',3.2),
('a2',109,'apricot',25.2),
('b2',110,'lemon',6.4),
('b2',111,'berry',7.6),
('bs1',112,'mango',15.6),
('bs2',113,'abc',2.6),
('a1',114,'cda',3.6),
('bs2',115,'xxxx',3.6);

  

 

posted @ 2019-07-30 20:22  九友  阅读(139)  评论(0编辑  收藏  举报