使用C语言访问MySQL数据库

本文详细介绍了如何使用C语言连接和操作MySQL数据库,包括创建数据库、表、插入数据、查询以及删除操作。通过示例展示了如何创建CD、artist和track表,以及如何进行多表查询和模糊查询。此外,还提供了C语言访问数据库的函数原型和实现,如add_cd、find_cds等。
摘要由CSDN通过智能技术生成

MySQL数据库(管理CD盘)

一、数据库设计

1、数据库E/R图(实体-关系图)应该是多对多,为了简化,一对多(把artistkey——Artist_ID放到CD中作为外键)

2、关系模式(Relation Schema)

Artist(artist_ID,  names)  简称为D1表

CD(CD_ID, Title, cataloge, Notes, artist_id(外键),track_id(磁道), song_name)

  其中:非主属性track_id, song_name,不能完全函数依赖于键CD_ID, 不符合第二范式,将其投影分解为:

CD(CD_ID,Title, cataloge, Notes, artist_id)    简称为D2表

Track(CD_ID,track_id, song_name)          简称为D3表

//甲骨文数据库可以分属性为多个属性,可以直接存图片或者MP3

二、创建数据库

1、进入MySQL数据库

%mysql –u root –p

Password:

2、生成数据库

mysql> create database ex1;

Query OK, 1 row affected (0.01 sec)

2、授权:

   用户的授权信息保存于mysql数据库中的user表中

   例:mysql>use mysql;

       mysql>select user, host, passwordfrom user;

授权语法:

 grant all on 数据库名.表名  to ‘用户名’@’客户登录计算机名(地址)’ identified by  ‘密码’;

   或

 grant allprivileges on数据库名.表名  to 用户名@客户登录计算机名(地址) identified by  ‘密码’;

   或

 grant allon  数据库名.表名 to 用户名@客户登录计算机名(地址) identified by  ‘密码’;

 

例:mysql>grant all on *.* to  test@localhost identified by ‘password’;

    mysql>use mysql;    打开MySQL数据库

    mysql>update user   更新user表

          >set password=OLD_PASSWORD('password')

          >where user='test';

     mysql>flush privileges;

           或

     mysql>grant all on ex110301.* tow12@localhost identified by ‘password’;

     mysql>use mysql;    打开MySQL数据库

     mysql>flush privileges;

           或

     mysql>grant all on ex110301.* tow12@'%' identified by 'password';

     mysql>use mysql;  打开MySQL数据库

     mysql>flush privileges;

     mysql>exit

       表示用户w12对cd110301具有所有权限,可从任何计算(‘%’)上连接该数据库

       说明:远程连接需要使用TCP端口:3306

 

3、改用你授权的用户名/密码登录

  %mysql –u 授权的用户名  -p

  Password:输入授权密码

 

4、创建表

4.1 生成表cd

create table cd (

        id INTEGER AUTO_INCREMENT NOT NULLPRIMARY KEY,

        title VARCHAR(70) NOT NULL,    

        artist_id INTEGER NOT NULL,

        catalogue VARCHAR(30) NOT NULL

);

4.2 生成表artist

create tableartist (

        id INTEGER AUTO_INCREMENT NOT NULLPRIMARY KEY,

        name VARCHAR(100) NOT NULL

);

 

4.3 生成表track

create table track(

        cd_id INTEGER NOT NULL,

        track_id INTEGER NOT NULL,

        title VARCHAR(70),

        PRIMARY KEY(cd_id, track_id)

);

 

三、插入数据

3.1 向表artist中插入数据

insert intoartist(id, name) values(1, 'Pink Floyd');

insert intoartist(id, name) values(2, 'Genesis');

insert intoartist(id, name) values(3, 'Einaudi');

insert intoartist(id, name) values(4, 'Melanie C');

 

3.2 向表cd中插入数据

insert into cd(id,title, artist_id, catalogue) values(1, 'Dark Side of the Moon', 1, 'B000024D4P');

insert into cd(id,title, artist_id, catalogue) values(2, 'Wish You Were Here', 1, 'B000024D4S');

insert into cd(id,title, artist_id, catalogue) values(3, 'A Trick of the Tail', 2, 'B000024EXM');

insert into cd(id,title, artist_id, catalogue) values(4, 'Selling England By the Pound', 2,'B000024E9M');

insert into cd(id,title, artist_id, catalogue) values(5, 'I Giorni', 3, 'B000071WEV');

insert into cd(id,title, artist_id, catalogue) values(6, 'Northern Star', 4, 'B00004YMST');

 

3.3 向表track中插入数据

insert intotrack(cd_id, track_id, title) values(1, 1, 'Speak to me');

insert intotrack(cd_id, track_id, title) values(1, 2, 'Breathe');

insert intotrack(cd_id, track_id, title) values(1, 3, 'On the run');

insert intotrack(cd_id, track_id, title) values(1, 4, 'Time');

insert intotrack(cd_id, track_id, title) values(1, 5, 'Great gig in the sky');

insert intotrack(cd_id, track_id, title) values(1, 6, 'Money');

insert intotrack(cd_id, track_id, title) values(1, 7, 'Us and them');

insert

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值